TensorFlow 训练测试 log单独写

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)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值