I tried plotting the loss terms of my model, while using keras. I got the plot for 'loss' but 'val_loss' throws up a keyword error: I tried searching the internet and got this link : link to a previous post. But in this post they are using checkpoints and callbacks. While I have not implemented such features (It was not included in the tutorial I am following). Can someone help me getting around these errors! Thanks.
KeyError: 'val_loss'
Code:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import math
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers import LSTM
model = Sequential()
model.add(LSTM(128, input_shape=(1, step_size)))
model.add(Dropout(0.1)) # randomly select neurons to be ignored during training.
model.add(Dense(64))
model.add(Dense(1))
model.add(Activation('linear'))
model.summary()
model.compile(loss='mean_squared_error', optimizer='adam')
history = model.fit(trainX, trainY, epochs=1000, batch_size=25, verbose=2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
And also the same for accuracy: Note I tried changing the keyword 'acc' to 'accuracy' as mentioned in the previous post. But the same error pops up for that too: link
Error: KeyError: 'acc'
acc = history.history['acc']
val_acc = history.history['val_accuracy']
plt.plot(history.history['acc'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
CodePudding user response:
In order to plot validation data you need to have a validation data set which you do not have.You can use sklearn's train_test_split to create a validation set. Then in model.fit be sure to include validation_data(valx, valy) and set the validation batch_size. The code below makes a very nice plot of the train and validation model performance
def tr_plot(tr_data, start_epoch):
#Plot the training and validation data
tacc=tr_data.history['accuracy']
tloss=tr_data.history['loss']
vacc=tr_data.history['val_accuracy']
vloss=tr_data.history['val_loss']
Epoch_count=len(tacc) start_epoch
Epochs=[]
for i in range (start_epoch ,Epoch_count):
Epochs.append(i 1)
index_loss=np.argmin(vloss)# this is the epoch with the lowest validation loss
val_lowest=vloss[index_loss]
index_acc=np.argmax(vacc)
acc_highest=vacc[index_acc]
plt.style.use('fivethirtyeight')
sc_label='best epoch= ' str(index_loss 1 start_epoch)
vc_label='best epoch= ' str(index_acc 1 start_epoch)
fig,axes=plt.subplots(nrows=1, ncols=2, figsize=(25,10))
axes[0].plot(Epochs,tloss, 'r', label='Training loss')
axes[0].plot(Epochs,vloss,'g',label='Validation loss' )
axes[0].scatter(index_loss 1 start_epoch,val_lowest, s=150, c= 'blue', label=sc_label)
axes[0].scatter(Epochs, tloss, s=100, c='red')
axes[0].set_title('Training and Validation Loss')
axes[0].set_xlabel('Epochs', fontsize=18)
axes[0].set_ylabel('Loss', fontsize=18)
axes[0].legend()
axes[1].plot (Epochs,tacc,'r',label= 'Training Accuracy')
axes[1].scatter(Epochs, tacc, s=100, c='red')
axes[1].plot (Epochs,vacc,'g',label= 'Validation Accuracy')
axes[1].scatter(index_acc 1 start_epoch,acc_highest, s=150, c= 'blue', label=vc_label)
axes[1].set_title('Training and Validation Accuracy')
axes[1].set_xlabel('Epochs', fontsize=18)
axes[1].set_ylabel('Accuracy', fontsize=18)
axes[1].legend()
plt.tight_layout
plt.show()
return index_loss
loss_index=tr_plot(history,0)
CodePudding user response:
In order to plot validation data you need to have a validation data set which you do not have.You can use sklearn's train_test_split to create a validation set. Then in model.fit be sure to include validation_data(valx, valy) and set the validation batch_size. The code below makes a very nice plot of the train and validation model performance
import seaborn as sns
sns.set_style('darkgrid')
def tr_plot(tr_data, start_epoch):
#Plot the training and validation data
tacc=tr_data.history['accuracy']
tloss=tr_data.history['loss']
vacc=tr_data.history['val_accuracy']
vloss=tr_data.history['val_loss']
Epoch_count=len(tacc) start_epoch
Epochs=[]
for i in range (start_epoch ,Epoch_count):
Epochs.append(i 1)
index_loss=np.argmin(vloss)# this is the epoch with the lowest validation loss
val_lowest=vloss[index_loss]
index_acc=np.argmax(vacc)
acc_highest=vacc[index_acc]
plt.style.use('fivethirtyeight')
sc_label='best epoch= ' str(index_loss 1 start_epoch)
vc_label='best epoch= ' str(index_acc 1 start_epoch)
fig,axes=plt.subplots(nrows=1, ncols=2, figsize=(25,10))
axes[0].plot(Epochs,tloss, 'r', label='Training loss')
axes[0].plot(Epochs,vloss,'g',label='Validation loss' )
axes[0].scatter(index_loss 1 start_epoch,val_lowest, s=150, c= 'blue', label=sc_label)
axes[0].scatter(Epochs, tloss, s=100, c='red')
axes[0].set_title('Training and Validation Loss')
axes[0].set_xlabel('Epochs', fontsize=18)
axes[0].set_ylabel('Loss', fontsize=18)
axes[0].legend()
axes[1].plot (Epochs,tacc,'r',label= 'Training Accuracy')
axes[1].scatter(Epochs, tacc, s=100, c='red')
axes[1].plot (Epochs,vacc,'g',label= 'Validation Accuracy')
axes[1].scatter(index_acc 1 start_epoch,acc_highest, s=150, c= 'blue', label=vc_label)
axes[1].set_title('Training and Validation Accuracy')
axes[1].set_xlabel('Epochs', fontsize=18)
axes[1].set_ylabel('Accuracy', fontsize=18)
axes[1].legend()
plt.tight_layout
plt.show()
return index_loss
loss_index=tr_plot(history,0)