Bird
Raised Fist0
TensorFlowml~20 mins

Training history and visualization in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Training History Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the final training accuracy after 3 epochs?
Consider this TensorFlow training code snippet. What is the final training accuracy printed after training for 3 epochs?
TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models

model = models.Sequential([
    layers.Dense(10, activation='relu', input_shape=(5,)),
    layers.Dense(2, activation='softmax')
])

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

import numpy as np
x_train = np.random.random((100, 5))
y_train = np.random.randint(0, 2, 100)

history = model.fit(x_train, y_train, epochs=3, verbose=0)

final_acc = history.history['accuracy'][-1]
print(round(final_acc, 2))
A0.75
B0.99
C0.50
D0.85
Attempts:
2 left
💡 Hint
Look at the last value in the 'accuracy' list from the history object.
🧠 Conceptual
intermediate
1:30remaining
What does the 'history.history' dictionary contain?
After training a TensorFlow model, the 'history' object has an attribute 'history'. What does this dictionary store?
AThe compiled model configuration
BThe model's weights after training
CLists of metric values and loss for each epoch during training
DThe training dataset used for fitting the model
Attempts:
2 left
💡 Hint
Think about what changes after each epoch during training.
Metrics
advanced
1:30remaining
Which metric is best to monitor for classification accuracy during training?
You want to track how well your classification model predicts correct labels during training. Which metric should you monitor?
Aaccuracy
Bmean_squared_error
Ccategorical_crossentropy
Dmean_absolute_error
Attempts:
2 left
💡 Hint
Accuracy measures correct predictions as a percentage.
🔧 Debug
advanced
2:30remaining
Why does this training history plot show a flat line?
You run this code to plot training accuracy but the graph is a flat line at 1.0. What is the likely cause? import matplotlib.pyplot as plt plt.plot(history.history['accuracy']) plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.show()
TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np

model = models.Sequential([
    layers.Dense(10, activation='relu', input_shape=(5,)),
    layers.Dense(2, activation='softmax')
])

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

x_train = np.random.random((100, 5))
y_train = np.zeros(100, dtype=int)  # All labels are zero

history = model.fit(x_train, y_train, epochs=5, verbose=0)

import matplotlib.pyplot as plt
plt.plot(history.history['accuracy'])
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
AThe model did not compile correctly
BThe training labels have no variation, so accuracy stays constant
CThe plot command is missing plt.show()
DThe accuracy metric is not supported for this model
Attempts:
2 left
💡 Hint
Check the training labels and their diversity.
Model Choice
expert
3:00remaining
Which model architecture is best for visualizing training history of a time series prediction?
You want to train a model on time series data and visualize its training loss and accuracy. Which model architecture is most suitable?
AA recurrent neural network (RNN) with LSTM layers
BA simple dense neural network with one hidden layer
CA convolutional neural network (CNN) designed for image data
DA linear regression model without epochs
Attempts:
2 left
💡 Hint
Time series data has order and sequence information.

Practice

(1/5)
1. What does the history.history object store after training a TensorFlow model?
easy
A. The dataset used for training
B. The model's architecture details
C. Loss and accuracy values for each epoch during training
D. The optimizer's internal state

Solution

  1. Step 1: Understand what history.history contains

    After training, TensorFlow's model.fit() returns a history object that stores metrics like loss and accuracy for each epoch.
  2. Step 2: Identify the correct stored data

    The history.history dictionary holds lists of loss and accuracy values recorded at each epoch for training and validation.
  3. Final Answer:

    Loss and accuracy values for each epoch during training -> Option C
  4. Quick Check:

    Training metrics stored in history.history = Loss and accuracy values for each epoch during training [OK]
Hint: Remember: history stores metrics per epoch, not model or data [OK]
Common Mistakes:
  • Confusing history with model architecture
  • Thinking history stores the dataset
  • Assuming history holds optimizer state
2. Which of the following is the correct way to plot training and validation accuracy from a TensorFlow history object using matplotlib?
easy
A. plt.plot(history.history['accuracy']); plt.plot(history.history['val_accuracy'])
B. plt.plot(history['accuracy']); plt.plot(history['val_accuracy'])
C. plt.plot(history.accuracy); plt.plot(history.val_accuracy)
D. plt.plot(history.accuracy()); plt.plot(history.val_accuracy())

Solution

  1. Step 1: Recall how to access metrics in history object

    The history object stores metrics in a dictionary under history.history. Access keys like 'accuracy' and 'val_accuracy' as dictionary keys.
  2. Step 2: Use matplotlib to plot lists from the dictionary

    Use plt.plot() with history.history['accuracy'] and history.history['val_accuracy'] to plot training and validation accuracy.
  3. Final Answer:

    plt.plot(history.history['accuracy']); plt.plot(history.history['val_accuracy']) -> Option A
  4. Quick Check:

    Access metrics via history.history['key'] for plotting [OK]
Hint: Always access metrics with history.history['metric_name'] [OK]
Common Mistakes:
  • Using dot notation instead of dictionary keys
  • Calling metrics as functions
  • Accessing history directly without .history
3. Given the following code snippet, what will be the output of print(history.history['loss']) after training for 3 epochs?
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=3, validation_data=(x_val, y_val))
print(history.history['loss'])
medium
A. A list of 3 loss values, one per epoch
B. An error because 'loss' key does not exist
C. A single float value of final loss
D. [0.8, 0.6, 0.4]

Solution

  1. Step 1: Understand what history.history['loss'] contains

    It stores the loss values recorded at the end of each epoch during training as a list.
  2. Step 2: Predict the output after 3 epochs

    Since training runs for 3 epochs, the list will have 3 float values representing loss per epoch, not just one or a fixed list.
  3. Final Answer:

    A list of 3 loss values, one per epoch -> Option A
  4. Quick Check:

    Loss per epoch stored as list = A list of 3 loss values, one per epoch [OK]
Hint: Loss history is a list with one value per epoch [OK]
Common Mistakes:
  • Expecting a single float instead of a list
  • Assuming fixed loss values without training
  • Thinking 'loss' key is missing
4. Identify the error in this code snippet that tries to plot training and validation loss:
import matplotlib.pyplot as plt
plt.plot(history['loss'])
plt.plot(history['val_loss'])
plt.show()
medium
A. plt.plot() cannot plot lists
B. history should be accessed as history.history, not directly
C. Missing plt.title() causes error
D. No error, code runs fine

Solution

  1. Step 1: Check how history metrics are accessed

    The history object stores metrics inside the history attribute, so direct access like history['loss'] is incorrect.
  2. Step 2: Correct the access to history.history['loss']

    To fix, use history.history['loss'] and history.history['val_loss'] for plotting.
  3. Final Answer:

    history should be accessed as history.history, not directly -> Option B
  4. Quick Check:

    Access metrics via history.history, not history [OK]
Hint: Use history.history to access metrics, not history alone [OK]
Common Mistakes:
  • Accessing history metrics directly
  • Assuming plt.plot can't plot lists
  • Thinking missing title causes error
5. You trained a model for 10 epochs but notice the validation loss increases after epoch 5 while training loss decreases. How can visualizing the training history help you decide the next step?
hard
A. It suggests increasing the learning rate to fix validation loss
B. It confirms the model is perfect, so no changes needed
C. It means the training data is incorrect and should be discarded
D. It shows overfitting, so you might stop training early or add regularization

Solution

  1. Step 1: Interpret the training and validation loss curves

    When training loss decreases but validation loss increases, it indicates the model is overfitting the training data.
  2. Step 2: Decide actions based on visualization

    Visualizing history helps identify overfitting, suggesting to stop early, add dropout, or use regularization to improve generalization.
  3. Final Answer:

    It shows overfitting, so you might stop training early or add regularization -> Option D
  4. Quick Check:

    Increasing validation loss with decreasing training loss = overfitting [OK]
Hint: Watch for validation loss rising while training loss falls [OK]
Common Mistakes:
  • Ignoring validation loss trends
  • Increasing learning rate without reason
  • Assuming data is wrong without checking