Classes and Objects
Object‑Oriented Programming (OOP) bundles data and the functions that operate on that data into classes and objects. This helps organize complex AI projects like training pipelines or custom model architectures.
A class is a blueprint; an object is an instance created from that blueprint.
Defining a Class
class Model:
def __init__(self, name):
self.name = name
self.accuracy = 0.0
def train(self):
print(f"Training {self.name}...")
self.accuracy = 0.95
def evaluate(self):
print(f"Accuracy: {self.accuracy}")Creating and Using Objects
my_model = Model("NeuralNet")
my_model.train()
my_model.evaluate()Why OOP in AI?
- Encapsulation: Keep data (weights) and methods (forward, backward) together.
- Inheritance: Create specialized model classes from a base class.
- Frameworks: TensorFlow and PyTorch use classes for layers, optimizers, and datasets.
Example: Custom Layer Class
class DenseLayer:
def __init__(self, input_size, output_size):
self.weights = [[0]*output_size for _ in range(input_size)]
def forward(self, inputs):
# simplified matrix multiplication
return inputsTwo Minute Drill
- Class = blueprint; object = actual instance.
__init__is the constructor (sets up initial state).- Methods are functions inside a class.
- OOP helps manage complexity in AI projects.
Two Minute Drill
- Class = blueprint; object = actual instance.
__init__is the constructor (sets up initial state).- Methods are functions inside a class.
- OOP helps manage complexity in AI projects.
Need more clarification?
Drop us an email at career@quipoinfotech.com
