Example 2: Training the Neural Network Model

In this step, we will train the neural network built in the previous section using our prepared data. Training involves optimizing the model weights to minimize the loss function, allowing the model to make accurate predictions on unseen data.

Key Objectives

  1. Train the model using training data (X_train and y_train).
  2. Monitor training performance with metrics like accuracy.
  3. Evaluate the model on testing data (X_test and y_test).

Step 1: Train the Model

We will train the model using the fit method, which accepts the following parameters:

  • Training Data: Features (X_train_tf) and labels (y_train_tf).
  • Epochs: The number of complete passes through the training dataset.
  • Batch Size: The number of samples per gradient update.
  • Validation Data: Data used to monitor model performance during training.
# Train the model
history = model.fit(
    X_train_tf, y_train_tf,
    epochs=50,  # Train for 50 epochs
    batch_size=32,  # Use a batch size of 32
    validation_split=0.2,  # Use 20% of training data for validation
    verbose=1  # Print progress during training
)

Step 2: Visualize Training Progress

Plot the loss and accuracy over epochs to analyze the training process.

import matplotlib.pyplot as plt

# Plot training and validation loss
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.title('Training and Validation Loss')
plt.show()

# Plot training and validation accuracy
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Training and Validation Accuracy')
plt.show()

Step 3: Evaluate the Model on Test Data

After training, evaluate the model using the test dataset to measure its generalization performance.

# Evaluate the model on test data
test_loss, test_accuracy = model.evaluate(X_test_tf, y_test_tf, verbose=0)

print(f"Test Loss: {test_loss:.4f}")
print(f"Test Accuracy: {test_accuracy:.4f}")

Key Insights

  1. Overfitting: If validation loss increases while training loss decreases, the model may be overfitting.
  2. Learning Rate: Adjust learning rates if the model does not converge or overfits.
  3. Batch Size: Smaller batch sizes might improve generalization but could increase training time.

Full Training Code

# Train the model
history = model.fit(
    X_train_tf, y_train_tf,
    epochs=50,
    batch_size=32,
    validation_split=0.2,
    verbose=1
)

# Plot training progress
import matplotlib.pyplot as plt

plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.title('Training and Validation Loss')
plt.show()

plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Training and Validation Accuracy')
plt.show()

# Evaluate the model
test_loss, test_accuracy = model.evaluate(X_test_tf, y_test_tf, verbose=0)

print(f"Test Loss: {test_loss:.4f}")
print(f"Test Accuracy: {test_accuracy:.4f}")

Expected Results

  • Training Accuracy: Increases over epochs as the model learns.
  • Validation Accuracy: Should improve without overfitting.
  • Test Accuracy: Measures the model’s performance on unseen data.

Leave a Comment