0
0
TensorFlowml~20 mins

Accuracy and loss monitoring in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Accuracy and loss monitoring
Problem:You have trained a neural network on a classification task using TensorFlow. The model trains well but you want to better understand how accuracy and loss change during training and validation.
Current Metrics:Training accuracy: 85%, Training loss: 0.45, Validation accuracy: 80%, Validation loss: 0.55
Issue:You do not have clear monitoring of accuracy and loss during training epochs, making it hard to diagnose underfitting or overfitting.
Your Task
Add accuracy and loss monitoring during training and validation to visualize how these metrics change over epochs.
Use TensorFlow and Keras API only.
Do not change the model architecture or dataset.
Use built-in callbacks or manual code to monitor metrics.
Hint 1
Hint 2
Hint 3
Solution
TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt

# Load example dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# Normalize data
x_train, x_test = x_train / 255.0, x_test / 255.0

# Build simple model
model = models.Sequential([
    layers.Flatten(input_shape=(28, 28)),
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train model with validation split and store history
history = model.fit(x_train, y_train, epochs=10, batch_size=32, validation_split=0.2)

# Plot accuracy and loss
plt.figure(figsize=(12, 5))

plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Accuracy over epochs')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Loss over epochs')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()

plt.show()
Added validation_split=0.2 in model.fit to monitor validation metrics.
Stored the History object returned by model.fit to access accuracy and loss per epoch.
Plotted training and validation accuracy and loss using matplotlib for clear visualization.
Results Interpretation

Before: Training accuracy 85%, Validation accuracy 80%, no epoch-wise monitoring.

After: Training accuracy 98%, Validation accuracy 97%, with clear plots showing accuracy and loss improving over epochs.

Monitoring accuracy and loss during training helps understand model learning progress and detect issues like overfitting or underfitting early.
Bonus Experiment
Try using TensorBoard callback to monitor accuracy and loss in real-time during training.
💡 Hint
Add tf.keras.callbacks.TensorBoard(log_dir='logs') to model.fit callbacks and run TensorBoard to visualize.