![](/img/trans.png)
[英]how to make accuracy/loss plot in one plot if CNN model trained both on cifar10/100 in keras?
[英]How to plot the accuracy and and loss from this Keras CNN model?
下面的代碼適用於我的 CNN model,我想 plot 的准確性和損失,任何幫助將不勝感激。 我希望使用 matplotlib 繪制 output 所以需要任何建議,因為我不確定如何處理這個問題。 兩個具有訓練和驗證精度的圖和另一個具有訓練和驗證損失的 plot。
bin_labels = {1:'EOSINOPHIL',2:'LYMPHOCYTE',3:'MONOCYTE',4:'NEUTROPHIL'}
def CNN(imgs,img_labels,test_imgs,test_labels,stride):
#Number of classes (2)
num_classes = len(img_labels[0])
#Size of image
img_rows,img_cols=imgs.shape[1],imgs.shape[2]
input_shape = (img_rows, img_cols, 3)
#Creating the model
model = Sequential()
#First convolution layer
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape,
strides=stride))
#First maxpooling layer
model.add(MaxPooling2D(pool_size=(2, 2)))
#Second convolution layer
model.add(Conv2D(64, (3, 3), activation='relu'))
#Second maxpooling layer
model.add(MaxPooling2D(pool_size=(2, 2)))
#Third convolution layer
model.add(Conv2D(128, (3, 3), activation='relu'))
#Third maxpooling layer
model.add(MaxPooling2D(pool_size=(2, 2)))
#Convert the matrix to a fully connected layer
model.add(Flatten())
#Dense function to convert FCL to 128 values
model.add(Dense(128, activation='relu'))
#Final dense layer on which softmax function is performed
model.add(Dense(num_classes, activation='softmax'))
#Model parameters
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
#Evaluate the model on the test data before training your model
score = model.evaluate(test_imgs,test_labels, verbose=1)
print('\nKeras CNN binary accuracy:', score[1],'\n')
#The model details
history = model.fit(imgs,img_labels,
shuffle = True,
epochs=3,
validation_data = (test_imgs, test_labels))
#Evaluate the model on the test data after training your model
score = model.evaluate(test_imgs,test_labels, verbose=1)
print('\nKeras CNN binary accuracy:', score[1],'\n')
#Predict the labels from test data
y_pred = model.predict(test_imgs)
Y_pred_classes = np.argmax(y_pred,axis=1)
Y_true = np.argmax(test_labels,axis=1)
#Correct labels
for i in range(len(Y_true)):
if(Y_pred_classes[i] == Y_true[i]):
print("The predicted class is : " , Y_pred_classes[i])
print("The real class is : " , Y_true[i])
break
#The confusion matrix made from the real Y values and the predicted Y values
confusion_mtx = [Y_true, Y_pred_classes]
#Summary of the model
model.summary()
return model,confusion_mtx
model,conf_mat = CNN(X_train,y_trainHot,X_test,y_testHot,1);
在 CNN model 上工作時,這對我有用:
import matplotlib.pyplot as plt
# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.show()
你可以在這里看到 plot 的圖像
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.