Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials AI Training a Model
AI Intermediate FREE

Training a Model

Lesson 2 of 17 Intermediate Interactive

Training involves forward pass, loss calculation, and backpropagation to adjust weights.

Syntax

AI
model.fit(X_train, y_train,
    epochs=10,
    batch_size=32,
    validation_split=0.2)
Training Loop Concept
PYTHON
# Training loop concept
def train(model, data, epochs, lr):
    for epoch in range(epochs):
        total_loss = 0
        for x, y_true in data:
            y_pred = model(x)
            loss = (y_pred - y_true) ** 2
            total_loss += loss

            # Update weights (simplified)
            gradient = 2 * (y_pred - y_true)
            model["w"] -= lr * gradient * x
            model["b"] -= lr * gradient

        if epoch % 100 == 0:
            print(f"Epoch {epoch}: Loss = {total_loss:.4f}")

model = {"w": 0.5, "b": 0.1}
data = [(1, 2), (2, 4), (3, 6), (4, 8)]
train(model, data, epochs=500, lr=0.01)
print(f"Learned: w={model[\"w\"]:.2f}, b={model[\"b\"]:.2f}")

Practice

1
Exercise

Practice this concept.

Answer
Write the code as shown above.

Quick Quiz

1

What did you learn?

This covers the basics.

Interview Questions

It is a fundamental AI feature.