DEEP LEARNING LESSON 9 BUILDING NEURAL NETWORKS WITH PYTHON

Training the Model

Training is the stage where a neural network actually learns from data. We give the model input data and correct answers, the model makes predictions, calculates its error, and updates its weights to improve future predictions.

What Does Training the Model Mean?

Training means repeatedly showing examples to the neural network so that it can adjust its weights and become better at making predictions.

The basic process is:

Input Data
    ↓
Model makes Prediction
    ↓
Calculate Loss
    ↓
Calculate Gradients
    ↓
Update Weights
    ↓
Better Prediction
    ↓
Repeat

The important word is repeat. One calculation is not enough for a neural network to learn a useful pattern.

How Training Works

Suppose we want a model to predict whether a student will pass an exam based on study hours.

Study Hours → Result

1 hour  → 0
2 hours → 0
4 hours → 1
6 hours → 1
8 hours → 1

The model receives the study hours as input and tries to predict the result.

Input
  ↓
Neural Network
  ↓
Prediction
  ↓
Compare with Correct Answer
  ↓
Calculate Loss
  ↓
Update Weights

The model repeats this process many times until its predictions become better.

Training Data

To train a model, we normally provide two things:

X
↓
Input features


y
↓
Correct answers / target values

For example:

X = Study Hours

[1]
[2]
[4]
[6]
[8]


y = Exam Result

[0]
[0]
[1]
[1]
[1]

The model learns the relationship between X and y.

Using model.fit()

In Keras, we train a model using the fit() method.

model.fit(
    X_train,
    y_train
)

This tells Keras:

X_train
↓
Input data


y_train
↓
Correct answers

Keras then runs the training process automatically.

Simple Training Example

import tensorflow as tf
import numpy as np

# Training data
X_train = np.array([
    [1],
    [2],
    [4],
    [6],
    [8]
])

y_train = np.array([
    0,
    0,
    1,
    1,
    1
])


# Create model
model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(1,)),

    tf.keras.layers.Dense(
        4,
        activation="relu"
    ),

    tf.keras.layers.Dense(
        1,
        activation="sigmoid"
    )
])


# Compile model
model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)


# Train model
model.fit(
    X_train,
    y_train,
    epochs=10
)

The most important line for this topic is:

model.fit(
    X_train,
    y_train,
    epochs=10
)

This starts the actual learning process.

What Is an Epoch?

An epoch means the model has gone through the entire training dataset once.

For example, if we have 100 training examples:

1 Epoch
↓
Model sees all 100 examples once

If we train for 10 epochs:

10 Epochs
↓
Model sees the training dataset 10 times

In Python:

model.fit(
    X_train,
    y_train,
    epochs=10
)

means Keras will perform 10 passes through the training data, subject to the batch configuration.

Batch Size

The batch size determines how many training examples are processed before the model updates its weights.

For example:

batch_size=2

means the model processes two examples at a time before performing a weight update.

100 Training Examples

Batch 1 → Examples 1-2
Batch 2 → Examples 3-4
Batch 3 → Examples 5-6
...
Batch 50 → Examples 99-100

You can specify it in:

model.fit(
    X_train,
    y_train,
    epochs=10,
    batch_size=2
)

Epoch vs Batch Size

These two concepts are easy to confuse.

Epoch
↓
How many times the model sees the entire dataset


Batch Size
↓
How many examples are processed before a weight update

Example:

Dataset = 100 examples
Batch size = 10
Epochs = 5

During each epoch:

100 examples
÷
10 examples per batch
=
10 batches per epoch

Across 5 epochs:

10 batches × 5 epochs
=
50 training steps

What Happens to the Loss?

During training, the model tries to reduce its loss.

For example:

Epoch 1 → Loss: 0.80
Epoch 2 → Loss: 0.60
Epoch 3 → Loss: 0.42
Epoch 4 → Loss: 0.30
Epoch 5 → Loss: 0.21

This generally indicates that the model is learning the patterns in the training data.

But don't make the mistake of thinking "lower training loss always means a better model."

A model can become very good at the training data while performing badly on new, unseen data. That is overfitting.

What Does model.fit() Show?

When you train a model:

history = model.fit(
    X_train,
    y_train,
    epochs=5
)

Keras usually displays information such as:

Epoch 1/5
loss: 0.72
accuracy: 0.60

Epoch 2/5
loss: 0.58
accuracy: 0.70

Epoch 3/5
loss: 0.43
accuracy: 0.80

...

This lets us monitor what happens during training.

The History Object

The result returned by model.fit() contains information about the training process.

history = model.fit(
    X_train,
    y_train,
    epochs=10
)

We can inspect the recorded loss:

print(history.history["loss"])

And accuracy:

print(history.history["accuracy"])

For example:

Loss:

[
    0.80,
    0.61,
    0.44,
    0.32,
    0.24
]

Each value corresponds to an epoch.

Training With Validation Data

We usually don't want to judge a model only by how well it performs on the training data.

We can provide validation data:

model.fit(
    X_train,
    y_train,
    epochs=10,
    validation_data=(X_val, y_val)
)

Keras will train using the training data and evaluate the model on the validation data after each epoch.

Training Data
     ↓
Model learns


Validation Data
     ↓
Model performance is checked

This helps us detect problems such as overfitting.

Example With Validation

history = model.fit(
    X_train,
    y_train,
    epochs=20,
    batch_size=32,
    validation_data=(X_val, y_val)
)

Here:

X_train, y_train
→ Used to learn


X_val, y_val
→ Used to check performance


epochs=20
→ Train for 20 epochs


batch_size=32
→ Process 32 examples per batch

Complete Training Example

import tensorflow as tf
import numpy as np


# -----------------------------
# Training data
# -----------------------------

X_train = np.array([
    [1],
    [2],
    [3],
    [4],
    [5],
    [6],
    [7],
    [8]
])

y_train = np.array([
    0,
    0,
    0,
    1,
    1,
    1,
    1,
    1
])


# -----------------------------
# Create model
# -----------------------------

model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(1,)),

    tf.keras.layers.Dense(
        8,
        activation="relu"
    ),

    tf.keras.layers.Dense(
        1,
        activation="sigmoid"
    )
])


# -----------------------------
# Compile model
# -----------------------------

model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)


# -----------------------------
# Train model
# -----------------------------

history = model.fit(
    X_train,
    y_train,
    epochs=20,
    batch_size=2
)


# -----------------------------
# Display final training loss
# -----------------------------

print(
    "Final Loss:",
    history.history["loss"][-1]
)

Understand the Python Code

history = model.fit(

Starts the model training process and stores the training history in the history variable.

X_train

Contains the input features that the model learns from.

y_train

Contains the correct answers that the model tries to predict.

epochs=20

Tells Keras to train through the training dataset 20 times.

batch_size=2

Tells Keras to process two training examples per batch before updating the weights.

history.history["loss"]

Gives us the recorded loss value for each epoch.

history.history["loss"][-1]

Gets the loss from the final epoch.

What Actually Changes During Training?

The model architecture does not keep changing during normal training.

The important values that change are the model's weights and biases.

Before Training

Weight = 0.20


After Training Step

Weight = 0.17


After Another Step

Weight = 0.14

The optimizer keeps adjusting these values based on the gradients calculated from the loss.

Prediction
    ↓
Loss
    ↓
Gradients
    ↓
Optimizer
    ↓
New Weights
    ↓
New Prediction

Training Is Not Simply Memorizing Answers

Ideally, the neural network learns patterns that allow it to make predictions for data it has never seen before.

Training Examples
       ↓
Learn Patterns
       ↓
New Unseen Example
       ↓
Prediction

If the model only memorizes the training examples and fails on new examples, the model has not generalized well.

The Main Idea

model.fit(
    X_train,
    y_train,
    epochs=10,
    batch_size=32
)

Read this as:

"Take my training inputs and their
correct answers, process them in batches,
repeat the dataset 10 times, and update
the model's weights so its predictions
become better."

That is the core idea behind training a neural network.

Complete Neural Network Workflow

1. Create Model
       ↓
2. Add Layers
       ↓
3. Compile Model
       ↓
4. Train Model
       ↓
5. Evaluate Model
       ↓
6. Make Predictions

So far we have reached step four.

Create
  ↓
Layers
  ↓
Compile
  ↓
TRAIN ← We are here
  ↓
Evaluate
  ↓
Predict

In the next topic, we will learn how to use the trained model to make predictions.

Remember This

model.fit()
↓
Starts training


X_train
↓
Input data


y_train
↓
Correct answers


epochs
↓
Number of complete passes through the dataset


batch_size
↓
Number of examples processed before a weight update


Training
↓
Prediction → Loss → Gradients → Weight Update → Repeat

The most important line to remember is:

model.fit(
    X_train,
    y_train,
    epochs=10
)

Training is where the model actually learns by repeatedly adjusting its weights based on the errors it makes.

QUICK CHECK

Check Your Understanding

Which function starts training?
model.fit().

What is X_train?
The input features used to train the model.

What is y_train?
The correct target values corresponding to the training inputs.

What does epochs=10 mean?
The model goes through the complete training dataset 10 times.

What does batch_size=32 mean?
The model processes 32 examples per batch before a weight update.

What changes during training?
The model's weights and biases are updated to reduce prediction error.

Does lower training loss automatically mean the model is good?
No. The model can overfit the training data, so validation or test performance also matters.