首先定义画图函数:
train_prompt = "Train cost"
cost_ploter = Ploter(train_prompt)
def event_handler_plot(ploter_title, step, cost):
cost_ploter.append(ploter_title, step, cost)
cost_ploter.plot()
在训练时如下方式使用:
EPOCH_NUM = 8
# 开始训练
lists = []
step = 0
for epochs in range(EPOCH_NUM):
# 开始训练
for batch_id, train_data in enumerate(train_reader()): #遍历train_reader的迭代器,并为数据加上索引batch_id
train_cost,sult,lab,vgg = exe.run(program=main_program, #运行主程序
feed=feeder.feed(train_data), #喂入一个batch的数据
fetch_list=[avg_cost,predict,label,VGG]) #fetch均方误差和准确率
if step % 10 == 0:
event_handler_plot(train_prompt,step,train_cost[0])
# print(batch_id)
if batch_id % 10 == 0: #每100次batch打印一次训练、进行一次测试
p = [np.sum(pre) for pre in sult]
l = [np.sum(pre) for pre in lab]
print(p,l,np.sum(sult),np.sum(lab))
print('Pass:%d, Batch:%d, Cost:%0.5f' % (epochs, batch_id, train_cost[0]))
step += 1
# 保存模型
if model_save_dir is not None:
fluid.io.save_inference_model(model_save_dir, ['images'], [predict], exe)
print('训练模型保存完成!')
end = time.time()
print(time.strftime('V100训练用时:%M分%S秒',time.localtime(end-start)))
实时显示准确率用同样的方法