TensorFlow learning record - 4.25

1. TensorBoard
In complex problems, the network is often very complex. In order to facilitate debugging parameters and adjust the network structure, we need to visualize the computational graph so that we can make better decisions in the next step. Tensorflow provides a TensorBoard tool that can meet the above requirements.

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()

The above is to store the structure diagram in the form of a log file in the image folder, and then cd to the path of the folder where the folder is placed in cmd

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

tensorboard –logdir=the folder where the files are placed After
execution , I get a URL, but it shows No graph definition files were found.
Finally, after trying various methods, I found that the file path cannot have Chinese, and the full English path is feasible.


Then there are scalars, histogram, etc.,

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也是一样的

After setting this up, use a merge to collect all the data

     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为取值步长

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886702&siteId=291194637