Issue
I'm trying to plot learning curves for different metrics such as training and validation loss and accuracy.
for lr in learning_rates:
opt = SGD(learning_rate = lr)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
network = model.fit(x_train_subset, y_train_subset, epochs=iterations, batch_size=32, validation_data=(X_test, y_test), verbose=1)
train_loss_values = network.history['loss']
train_accuracy_values = network.history['accuracy']
val_loss_values = network.history['val_loss']
val_accuracy_values = network.history['val_accuracy']
plt.plot(epochs, train_accuracy_values, label=f'LR = {lr}')
plt.title('Training Accuracy over Epochs for Different Learning Rates')
plt.xlabel('Epochs')
plt.ylabel('Training Accuracy')
plt.legend()
plt.grid(True)
plt.show()
This is the code I have and it works exactly how I want it to for the one graph with training accuracy as the metric.
Output of the above code:
However, I want to replicate this four times for all the different metrics stored in the following code:
train_loss_values = network.history['loss']
train_accuracy_values = network.history['accuracy']
val_loss_values = network.history['val_loss']
val_accuracy_values = network.history['val_accuracy']
If I simply do the following, it does not create 4 separate graphs, it just adds all data to one.
for lr in learning_rates:
opt = SGD(learning_rate = lr)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
network = model.fit(x_train_subset, y_train_subset, epochs=iterations, batch_size=32, validation_data=(X_test, y_test), verbose=1)
train_loss_values = network.history['loss']
train_accuracy_values = network.history['accuracy']
val_loss_values = network.history['val_loss']
val_accuracy_values = network.history['val_accuracy']
plt.plot(epochs, train_accuracy_values, label=f'LR = {lr}')
plt.plot(epochs, train_loss_values, label=f'LR = {lr}')
plt.plot(epochs, val_accuracy_values, label=f'LR = {lr}')
plt.plot(epochs, val_loss_values, label=f'LR = {lr}')
plt.title('Training Accuracy over Epochs for Different Learning Rates')
plt.xlabel('Epochs')
plt.ylabel('Training Accuracy')
plt.legend()
plt.grid(True)
plt.show()
I asked ChatGPT and it always suggested running the entire neural network again 4 times, but I have already run it once and collected all the data I need in lists. I just need to create 4 separate plots.
Solution
You can use plt.subplot
in order to display multiple plots.
plt.subplot(2,2,i)
creates 4 plots in a 2 x 2 grid. i
is the index of the plot you are currently setting.
#Set the size of your plot
plt.subplots(2, 2, figsize=(15, 10))
for lr in learning_rates:
opt = SGD(learning_rate = lr)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
network = model.fit(x_train_subset, y_train_subset, epochs=iterations, batch_size=32, validation_data=(X_test, y_test), verbose=1)
train_loss_values = network.history['loss']
train_accuracy_values = network.history['accuracy']
val_loss_values = network.history['val_loss']
val_accuracy_values = network.history['val_accuracy']
plt.subplot(2,2,1)
plt.plot(epochs, train_accuracy_values, label=f'LR = {lr}')
plt.subplot(2,2,2)
plt.plot(epochs, train_loss_values, label=f'LR = {lr}')
plt.subplot(2,2,3)
plt.plot(epochs, val_accuracy_values, label=f'LR = {lr}')
plt.subplot(2,2,4)
plt.plot(epochs, val_loss_values, label=f'LR = {lr}')
# Formatting for each subplot
ylabel = ['Training Accuracy', 'Training Loss', 'Validation Accuracy', 'Validation Loss']
for i in range(4):
plt.subplot(2,2,i+1)
plt.title(f'{ylabel[i]} over Epochs for Different Learning Rates')
plt.xlabel('Epochs')
plt.ylabel(ylabel[i])
plt.legend()
plt.grid(True)
plt.show()
I do not have your data, but the output should look like this:
Answered By - Mattravel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.