Accuracy and loss monitoring helps you see how well your machine learning model is learning. It shows if the model is improving or needs changes.
Accuracy and loss monitoring in TensorFlow
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
TensorFlow
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) history = model.fit(x_train, y_train, epochs=5, validation_data=(x_val, y_val))
model.compile sets how the model learns and what to watch.
metrics=['accuracy'] tells TensorFlow to track accuracy during training.
Examples
TensorFlow
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
TensorFlow
history = model.fit(x_train, y_train, epochs=10, validation_split=0.2)
TensorFlow
print(history.history['accuracy']) print(history.history['loss'])
Sample Model
This example creates a simple model, trains it on random data for 2 epochs, and prints the accuracy and loss values recorded during training.
TensorFlow
import tensorflow as tf # Prepare dummy data x_train = tf.random.normal([100, 28, 28]) y_train = tf.random.uniform([100], maxval=10, dtype=tf.int32) # Build a simple model model = tf.keras.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(10) ]) # Compile model with accuracy metric model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # Train model history = model.fit(x_train, y_train, epochs=2) # Print accuracy and loss from history print('Accuracy:', history.history['accuracy']) print('Loss:', history.history['loss'])
Important Notes
Accuracy shows the percentage of correct predictions.
Loss shows how far the model's predictions are from the true answers; lower is better.
Validation data helps check if the model works well on new data.
Summary
Accuracy and loss monitoring helps track model learning progress.
Use metrics=['accuracy'] in model.compile to monitor accuracy.
Access training results from history.history after model.fit.
Practice
1. What is the main purpose of monitoring accuracy and loss during TensorFlow model training?
easy
Solution
Step 1: Understand accuracy and loss roles
Accuracy shows how many predictions are correct, loss shows error size.Step 2: Purpose of monitoring during training
Tracking these helps see if the model is learning or needs adjustment.Final Answer:
To track how well the model is learning and improving -> Option BQuick Check:
Accuracy and loss track learning progress = C [OK]
Hint: Accuracy and loss show model learning quality [OK]
Common Mistakes:
- Thinking accuracy changes dataset size
- Believing monitoring changes model structure
- Assuming monitoring speeds training automatically
2. Which is the correct way to include accuracy monitoring when compiling a TensorFlow model?
easy
Solution
Step 1: Check required compile parameters
Optimizer and loss are required; metrics is optional for monitoring.Step 2: Correct syntax for metrics
metrics must be a list like ['accuracy'], not a string alone.Final Answer:
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) -> Option AQuick Check:
metrics=['accuracy'] in compile = B [OK]
Hint: Use metrics=['accuracy'] inside model.compile [OK]
Common Mistakes:
- Omitting metrics parameter
- Passing metrics as a string instead of list
- Leaving out loss or optimizer
3. Given this code snippet, what will
print(history.history['accuracy']) output?
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) history = model.fit(x_train, y_train, epochs=2) print(history.history['accuracy'])
medium
Solution
Step 1: Understand history.history content
It stores lists of metric values per epoch, including accuracy if monitored.Step 2: What history.history['accuracy'] returns
It returns a list of accuracy values, one per epoch, not a single value or error.Final Answer:
A list of accuracy values for each epoch, e.g. [0.85, 0.90] -> Option DQuick Check:
history.history['accuracy'] = list per epoch [OK]
Hint: history.history['accuracy'] holds accuracy per epoch list [OK]
Common Mistakes:
- Expecting a single float instead of list
- Confusing accuracy with loss values
- Assuming key 'accuracy' is missing
4. You run this code but get a KeyError when accessing
history.history['accuracy']. What is the likely cause?
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy') history = model.fit(x_train, y_train, epochs=3) print(history.history['accuracy'])
medium
Solution
Step 1: Check model.compile parameters
Accuracy monitoring requires metrics=['accuracy'] in compile, missing here.Step 2: Effect on history.history keys
Without metrics=['accuracy'], history.history has no 'accuracy' key, causing KeyError.Final Answer:
Accuracy was not included in metrics during model.compile -> Option AQuick Check:
Missing metrics=['accuracy'] causes KeyError [OK]
Hint: Always add metrics=['accuracy'] to compile to track accuracy [OK]
Common Mistakes:
- Forgetting to add metrics=['accuracy']
- Assuming loss function controls accuracy keys
- Thinking epochs parameter affects history keys
5. You want to monitor both accuracy and loss during training and plot their progress after training. Which code snippet correctly compiles the model and accesses the data for plotting?
hard
Solution
Step 1: Check model.compile metrics syntax
metrics must be a list like ['accuracy']. B omits it, C uses string 'accuracy'.Step 2: Check history access for plotting
history.history['accuracy'] and history.history['loss'] are correct; history['accuracy'] fails as history object lacks these attributes.Final Answer:
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) history = model.fit(x_train, y_train, epochs=5) plt.plot(history.history['accuracy']) plt.plot(history.history['loss']) -> Option CQuick Check:
metrics list + history.history keys = A [OK]
Hint: Use metrics=['accuracy'] and history.history for plotting [OK]
Common Mistakes:
- Passing metrics as string instead of list
- Accessing history keys directly on history object
- Omitting metrics parameter
