|
keras中深度模型训练的示例分析
发表时间:2021-08-20 10:29作者:小新
记录训练过程 history=model.fit(X_train, Y_train, epochs=epochs,batch_size=batch_size,validation_split=0.1)
将训练过程记录在history中 利用时间记录模型 import timemodel_id = np.int64(time.strftime('%Y%m%d%H%M', time.localtime(time.time())))
model.save('./VGG16'+str(model_id)+'.h6')
保存模型及结构图 from keras.utils import plot_model
model.save('/opt/Data1/lixiang/letter_recognition/models/VGG16'+str(model_id)+'.h6')
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()
以上是“keras中深度模型训练的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道! 免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:1953179025@qq.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。 |