tensorflow 一般是训练时增加log。测试时如何增加呢?
TensorFlow 读取events文件
import tensorflow as tf
# e,即event,代表某一个batch的日志记录
for e in tf.train.summary_iterator('./log/events.out.tfevents.1538814721.7226610225d8'):
# v,即value,代表这个batch的某个已记录的观测值,loss或者accuracy
for v in e.summary.value:
if v.tag == 'loss' or v.tag == 'accuracy':
print(v.simple_value)
由于不能开启 TensorFlow 网页查看log,于是手动python读取 TensorFlow 日志文件可视化
参考 https://blog.csdn.net/nima1994/article/details/82844988
from tensorboard.backend.event_processing import event_accumulator
#加载日志数据
ea=event_accumulator.EventAccumulator(r'E:\Code\lastExp\output\logs\pm_last\events.out.tfevents.1535713476.DESKTOP-KMHR70T')
ea.Reload()
print(ea.scalars.Keys())
val_acc=ea.scalars.Items('val_acc')
print(len(val_acc))
print([(i.step,i.value) for i in val_acc])
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(6,4))
ax1=fig.add_subplot(111)
val_acc=ea.scalars.Items('val_acc')
ax1.plot([i.step for i in val_acc],[i.value for i in val_acc],label='val_acc')
ax1.set_xlim(0)
acc=ea.scalars.Items('acc')
ax1.plot([i.step for i in acc],[i.value for i in acc],label='acc')
ax1.set_xlabel("step")
ax1.set_ylabel("")
plt.legend(loc='lower right')
plt.show()
测试时写入log 参考: https://www.cnblogs.com/hrlnw/p/8378576.html
使用此种方法不用sess.run 等于直接添加结构
testsummaries = tf.Summary()
lossval = testsummaries.value.add()
lossval.tag = 'loss'
lossval.simple_value = averageloss / batchnum
accval = testsummaries.value.add()
accval.tag = 'accuracy'
accval.simple_value = averageaccuracy / batchnum
# 每三个为一个字段 还可以添加各种loss和accuracy
testfilewriter.addsummary(testsummaries, step)