0
0
TensorFlowml~5 mins

Accuracy and loss monitoring in TensorFlow

Choose your learning style9 modes available
Introduction

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.

When training a model to check if it is learning correctly.
When comparing different models to pick the best one.
When tuning model settings to improve performance.
When spotting if the model is overfitting or underfitting.
When sharing training progress with your team or stakeholders.
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
Using SGD optimizer and categorical crossentropy loss with accuracy metric.
TensorFlow
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
Train for 10 epochs and use 20% of data for validation to monitor accuracy and loss.
TensorFlow
history = model.fit(x_train, y_train, epochs=10, validation_split=0.2)
Access and print accuracy and loss values recorded during training.
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'])
OutputSuccess
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.