使用Tensorboard查看训练过程(tf.summary.FileWriter)

 

元学习论文总结||小样本学习论文总结

2017-2019年计算机视觉顶会文章收录 AAAI2017-2019 CVPR2017-2019 ECCV2018 ICCV2017-2019 ICLR2017-2019 NIPS2017-2019

 


源码:

import tensorflow as tf
import numpy as np

#输入数据
x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0,0.05, x_data.shape)
y_data = np.square(x_data)-0.5+noise

#输入层
with tf.name_scope('input_layer'): #输入层。将这两个变量放到input_layer作用域下,tensorboard会把他们放在一个图形里面
    xs = tf.placeholder(tf.float32, [None, 1], name = 'x_input') # xs起名x_input,会在图形上显示
    ys = tf.placeholder(tf.float32, [None, 1], name = 'y_input') # ys起名y_input,会在图形上显示

#隐层
with tf.name_scope('hidden_layer'): #隐层。将隐层权重、偏置、净输入放在一起
    with tf.name_scope('weight'): #权重
        W1 = tf.Variable(tf.random_normal([1,10]))
        tf.summary.histogram('hidden_layer/weight', W1)
    with tf.name_scope('bias'): #偏置
        b1 = tf.Variable(tf.zeros([1,10])+0.1)
        tf.summary.histogram('hidden_layer/bias', b1)
    with tf.name_scope('Wx_plus_b'): #净输入
        Wx_plus_b1 = tf.matmul(xs,W1) + b1
        tf.summary.histogram('hidden_layer/Wx_plus_b',Wx_plus_b1)
output1 = tf.nn.relu(Wx_plus_b1)

#输出层
with tf.name_scope('output_layer'): #输出层。将输出层权重、偏置、净输入放在一起
    with tf.name_scope('weight'): #权重
        W2 = tf.Variable(tf.random_normal([10,1]))
        tf.summary.histogram('output_layer/weight', W2)
    with tf.name_scope('bias'): #偏置
        b2 = tf.Variable(tf.zeros([1,1])+0.1)
        tf.summary.histogram('output_layer/bias', b2)
    with tf.name_scope('Wx_plus_b'): #净输入
        Wx_plus_b2 = tf.matmul(output1,W2) + b2
        tf.summary.histogram('output_layer/Wx_plus_b',Wx_plus_b2)
output2 = Wx_plus_b2

#损失
with tf.name_scope('loss'): #损失
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-output2),reduction_indices=[1]))
    tf.summary.scalar('loss',loss)
with tf.name_scope('train'): #训练过程
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

#初始化
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
merged = tf.summary.merge_all() #将图形、训练过程等数据合并在一起
writer = tf.summary.FileWriter('logs',sess.graph) #将训练日志写入到logs文件夹下

#训练
for i in range(1000):
    sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
    if(i%50==0): #每50次写一次日志
        result = sess.run(merged,feed_dict={xs:x_data,ys:y_data}) #计算需要写入的日志数据
        writer.add_summary(result,i) #将日志数据写入文件

注意:

writer = tf.summary.FileWriter('logs',sess.graph) #将训练日志写入到logs文件夹下

 执行上述代码,会在“当前路径/logs”目录下生成一个events.out.tfevents.{time}.{machine-name}的文件。在当前目录新建“查看训练过程.bat”,里面输入。

tensorboard --logdir=logs

执行上述bat文件,打开浏览器,输入地址:http://localhost:6006,就可以查看训练过程中的各种图形。

重要提示:请不要用中文命名目录,中文目录中看不到任何图形。 

转载:https://www.cnblogs.com/tengge/p/6376073.html

  • 9
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
`tf.summary.FileWriter`是TensorFlow中用于将事件写入事件文件的类。它的作用是将TensorFlow中运行的计算图、变量值、梯度等信息写入到事件文件中,以便在TensorBoard中可视化和分析。 使用`tf.summary.FileWriter`的步骤如下: 1. 创建`tf.summary.FileWriter`对象,指定事件文件保存的路径。 ```python writer = tf.summary.FileWriter("/path/to/logdir", sess.graph) ``` 其中,`/path/to/logdir`是事件文件保存的路径,`sess.graph`是TensorFlow计算图。 2. 在训练过程中,使用`tf.summary.FileWriter`对象的`add_summary()`方法将事件写入事件文件。 ```python summary = sess.run(merged_summary, feed_dict={x: batch_xs, y: batch_ys}) writer.add_summary(summary, step) ``` 其中,`merged_summary`是汇总的事件,`step`是步骤数。 3. 训练结束后,关闭`tf.summary.FileWriter`对象。 ```python writer.close() ``` 在使用`tf.summary.FileWriter`之前,需要先创建汇总操作。例如,如果要记录变量的值,需要创建一个汇总操作: ```python tf.summary.scalar('loss', loss) ``` 然后,使用`tf.summary.merge_all()`将所有汇总操作合并成一个操作: ```python merged_summary = tf.summary.merge_all() ``` 最后,在训练过程中,运行`merged_summary`操作,将汇总结果写入事件文件。 总而言之,`tf.summary.FileWriter`是TensorFlow中非常重要的一个工具,可以方便地记录和可视化模型训练过程中的各种信息,对于模型调试和优化非常有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值