Saaransh Menon
Saaransh Menon

Reputation: 416

Dimension error trying to plot loss vs. epochs

This is my model.fit_generator (Trained for 40 epochs):

history = model.fit_generator(
 train_data_gen,
 steps_per_epoch=total_train // batch_size,
 epochs=epochs,
 validation_data=val_data_gen,
 validation_steps=total_val // batch_size
)

This is the code for plotting :

import matplotlib as plt
loss = history.history['loss']
plt.plot(epochs,loss, 'bo', label='Training Loss')
plt.show()

This is the error I am getting :

ValueError: x and y must have same first dimension, but have shapes (1,) and (40,)

I don't understand what I am doing wrong. Can someone please help?

Upvotes: 1

Views: 351

Answers (1)

desertnaut
desertnaut

Reputation: 60319

You should not try to plot against epochs (the variable), which is just a single number (here 40), hence the dimension error. Your question essentially is how to plot a list (loss); the number of epochs is implicitly included in the list (it's the list length). Assuming for simplicity only 10 epochs and

loss = [0.7251979386058971,
  0.6640552306833333,
  0.6190941931069023,
  0.5602273066015956,
  0.48771809028534785,
  0.40796665995284814,
  0.33154681897220617,
  0.2698465999525444,
  0.227492357244586,
  0.1998490962115201]

then simply

plt.plot(loss, 'bo')
plt.title('Training Loss')
plt.show()

gives

enter image description here

Upvotes: 2

Related Questions