I am running many iterations of a train so I can smooth out the loss curves. I would like an elegant way to average all the losses from history.history['loss']
but haven’t found an easy way to do it. Here’s a minimal example:
JavaScript
x
23
23
1
import tensorflow as tf
2
from tensorflow.keras.utils import to_categorical
3
from matplotlib import pyplot as plt
4
5
(x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
6
x_train = x_train.reshape(60000, 784).astype('float32')/255
7
y_train = to_categorical(y_train, num_classes=10)
8
9
def get_model():
10
model = tf.keras.Sequential()
11
model.add(tf.keras.layers.Dense(10, activation='sigmoid',
12
input_shape=(784,)))
13
model.add(tf.keras.layers.Dense(10, activation='softmax'))
14
model.compile(loss="categorical_crossentropy", optimizer="sgd",
15
metrics = ['accuracy'])
16
return model
17
18
all_trains = []
19
for i in range(3):
20
model = get_model()
21
history = model.fit(x_train, y_train, epochs=2)
22
all_trains.append(history)
23
If I wanted to plot just one example, I would do this:
JavaScript
1
3
1
plt.plot(history.epoch, history.history['loss'])
2
plt.show()
3
But instead, I want to average the loss from each train in all_trains
and plot them. I can think of many clunky ways to do it but would like to find a clean way.
Advertisement
Answer
You could simply do:
JavaScript
1
13
13
1
import numpy as np
2
import matplotlib.pyplot as plt
3
4
losses = [h.history['loss'] for h in all_trains]
5
mean_loss = np.mean(losses, axis=0)
6
std = np.std(losses, axis=0)
7
8
plt.errorbar(range(len(mean_loss)), mean_loss, yerr=std, capsize=5, marker='o')
9
plt.title('Average loss per epoch (± std)')
10
plt.xlabel('Epoch')
11
plt.ylabel('Categorical crossentropy')
12
plt.show()
13
I also added the standard deviation in this case.