[英]How to plot loss terms and accuracy in keras?
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 inte.net and got this link: link to a previous post.我尝试绘制我的 model 的损失项,同时使用 keras。我得到了“损失”的 plot,但“val_loss”引发了关键字错误:我尝试搜索 inte.net 并获得此链接: 链接到上一篇文章。 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'
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.准确性也一样:请注意,我尝试将关键字“acc”更改为“accuracy”,如前一篇文章中所述。 But the same error pops up for that too: link
但是同样的错误也会弹出: 链接
Error: KeyError: 'acc'
错误: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()
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.为了 plot 验证数据,您需要有一个您没有的验证数据集。您可以使用 sklearn 的 train_test_split 创建一个验证集。 Then in model.fit be sure to include validation_data(valx, valy) and set the validation batch_size.
然后在 model.fit 中确保包含 validation_data(valx, valy) 并设置验证 batch_size。 The code below makes a very nice plot of the train and validation model performance
下面的代码使训练和验证 model 性能非常好 plot
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.