I need to plot the training and validation graphs, and trarining and validation loss for my model.
JavaScript
x
12
12
1
model.compile(loss=tf.keras.losses.binary_crossentropy,
2
optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
3
metrics=['accuracy'])
4
5
history = model.fit(X_train, y_train,
6
batch_size=batch_size,
7
epochs=no_epochs,
8
verbose=verbosity,
9
validation_split=validation_split)
10
11
loss, accuracy = model.evaluate(X_test, y_test, verbose=1)
12
Advertisement
Answer
history
object contains both accuracy and loss for both the training as well as the validation set. We can use matplotlib to plot from that.
In these plots x-axis is no_of_epochs and the y-axis is accuracy and loss value. Below is one basic implementation to achieve that, it can easily be customized according to requirements.
JavaScript
1
23
23
1
import matplotlib.pyplot as plt
2
3
def plot_history(history):
4
acc = history.history["accuracy"]
5
loss = history.history["loss"]
6
val_loss = history.history["val_loss"]
7
val_accuracy = history.history["val_accuracy"]
8
9
x = range(1, len(acc) + 1)
10
11
plt.figure(figsize=(12,5))
12
plt.subplot(1, 2, 1)
13
plt.plot(x, acc, "b", label="traning_acc")
14
plt.plot(x, val_accuracy, "r", label="traning_acc")
15
plt.title("Accuracy")
16
17
plt.subplot(1, 2, 2)
18
plt.plot(x, loss, "b", label="traning_acc")
19
plt.plot(x, val_loss, "r", label="traning_acc")
20
plt.title("Loss")
21
22
plot_history(history)
23
Plot would look like below: