Introduction
Training history helps us see how well a model learns over time. Visualization makes it easy to understand the model's progress and spot problems.
Jump into concepts and practice - no test required
history = model.fit(x_train, y_train, epochs=10, validation_data=(x_val, y_val)) import matplotlib.pyplot as plt plt.plot(history.history['loss'], label='train loss') plt.plot(history.history['val_loss'], label='val loss') plt.legend() plt.show()
history = model.fit(x_train, y_train, epochs=5)history = model.fit(x_train, y_train, epochs=10, validation_split=0.2)
plt.plot(history.history['accuracy'], label='train accuracy') plt.plot(history.history['val_accuracy'], label='val accuracy') plt.legend() plt.show()
import tensorflow as tf from tensorflow.keras import layers, models import matplotlib.pyplot as plt # Prepare simple data: XOR problem x_train = [[0,0],[0,1],[1,0],[1,1]] y_train = [0,1,1,0] # Build a small model model = models.Sequential([ layers.Dense(4, activation='relu', input_shape=(2,)), layers.Dense(1, activation='sigmoid') ]) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # Train model and save history history = model.fit(x_train, y_train, epochs=20, verbose=0) # Plot loss and accuracy plt.figure(figsize=(10,4)) plt.subplot(1,2,1) plt.plot(history.history['loss'], label='loss') plt.title('Training Loss') plt.xlabel('Epoch') plt.ylabel('Loss') plt.legend() plt.subplot(1,2,2) plt.plot(history.history['accuracy'], label='accuracy') plt.title('Training Accuracy') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.legend() plt.tight_layout() plt.show() # Print final accuracy final_acc = history.history['accuracy'][-1] print(f'Final training accuracy: {final_acc:.2f}')
history.history object store after training a TensorFlow model?history.history containsmodel.fit() returns a history object that stores metrics like loss and accuracy for each epoch.history.history dictionary holds lists of loss and accuracy values recorded at each epoch for training and validation.history.history = Loss and accuracy values for each epoch during training [OK]history.history. Access keys like 'accuracy' and 'val_accuracy' as dictionary keys.plt.plot() with history.history['accuracy'] and history.history['val_accuracy'] to plot training and validation accuracy.history.history['key'] for plotting [OK]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'])
history.history['loss'] containsimport matplotlib.pyplot as plt plt.plot(history['loss']) plt.plot(history['val_loss']) plt.show()
history attribute, so direct access like history['loss'] is incorrect.history.history['loss']history.history['loss'] and history.history['val_loss'] for plotting.