TensorFlow学习记录——4.25

1.TensorBoard
在复杂的问题中,网络往往都是很复杂的。为了方便调试参数以及调整网络结构,我们需要将计算图可视化出来,以便能够更好的进行下一步的决策。Tensorflow提供了一个TensorBoard工具,可以满足上面的需求。

import tensorflow as tf
a = tf.constant([1.0,2.0,3.0],name='input1')
b = tf.Variable(tf.random_uniform([3]),name='input2')
# uniform是均匀分布的函数tf.random_uniform([5],minval=-1,maxval=2),当需要二维是shape换成(1,5)即可
add = tf.add_n([a,b],name='addOP')
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    writer = tf.summary.FileWriter("image/",sess.graph)
    print(sess.run(add))
writer.close()

上面就是把结构图以日志文件的形式存在了image文件夹里,然后cmd里cd到放文件夹的那个文件夹的路径

F:\Python\pycharm\神经网络\TensorFlow>tensorboard --logdir=image

tensorboard –logdir=放文件的文件夹
执行后得到一个网址,但是显示No graph definition files were found.
最后尝试多种方法后,发现是文件路径不能有中文,亲测全英文路径可行


然后就是scalars,histogram等,

with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=1))
    tf.summary.scalar('loss',loss)#创建一个scalar,histogram也是一样的

设置好了这些以后,用一个merge收集所有数据

     merged = tf.summary.merge_all()

    for i in range(1000):
        sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
        if i%50==0:
            result = sess.run(merged,feed_dict={xs:x_data,ys:y_data})
            writer.add_summary(result,i)#i为取值步长

猜你喜欢

转载自blog.csdn.net/weixin_40567315/article/details/80086414