时间:2020-11-03 13:57:26 | 栏目:Python代码 | 点击:次
记录训练过程
history=model.fit(X_train, Y_train, epochs=epochs,batch_size=batch_size,validation_split=0.1)
将训练过程记录在history中
利用时间记录模型
import time model_id = np.int64(time.strftime('%Y%m%d%H%M', time.localtime(time.time()))) model.save('./VGG16'+str(model_id)+'.h5')
保存模型及结构图
from keras.utils import plot_model model.save('/opt/Data1/lixiang/letter_recognition/models/VGG16'+str(model_id)+'.h5') plot_model(model, to_file='/opt/Data1/lixiang/letter_recognition/models/VGG16'+str(model_id)+'.png')
绘制训练过程曲线
import matplotlib.pyplot as plt fig = plt.figure()#新建一张图 plt.plot(history.history['acc'],label='training acc') plt.plot(history.history['val_acc'],label='val acc') plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(loc='lower right') fig.savefig('VGG16'+str(model_id)+'acc.png') fig = plt.figure() plt.plot(history.history['loss'],label='training loss') plt.plot(history.history['val_loss'], label='val loss') plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(loc='upper right') fig.savefig('VGG16'+str(model_id)+'loss.png')
文件记录最终训练结果
logFilePath = './log.txt' fobj = open(logFilePath, 'a') fobj.write('model id: ' + str(model_id)+'\n') fobj.write('epoch: '+ str(epochs) +'\n') fobj.write('x_train shape: ' + str(X_train.shape) + '\n') fobj.write('x_test shape: ' + str(X_test.shape)+'\n') fobj.write('training accuracy: ' + str(history.history['acc'][-1]) + '\n') fobj.write('model evaluation results: ' + str(score[0]) + ' ' +str(score[-1])+'\n') fobj.write('---------------------------------------------------------------------------\n') fobj.write('\n') fobj.close()
以字典格式保存训练中间过程
import pickle file = open('./models/history.pkl', 'wb') pickle.dump(history.history, file) file.close()