tensorboard可视化
Posted lazycat1206
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tensorboard可视化相关的知识,希望对你有一定的参考价值。
1 import tensorflow as tf 2 import numpy as np 3 import matplotlib.pyplot as plt 4 def add_layer(inputs, in_size, out_size,n_layer,activation_function=None): 5 layer_name=‘layer%s‘%n_layer 6 with tf.name_scope(‘layer‘): 7 with tf.name_scope(‘weights‘): 8 Weights = tf.Variable(tf.random_normal([in_size, out_size]),name=‘W‘) 9 tf.summary.histogram(layer_name+‘/weights‘,Weights)
#tensorflow中提供了tf.summary.histogram()
方法,用来绘制图片, 第一个参数是图表的名称, 第二个参数是图表要记录的变量
10 with tf.name_scope(‘biases‘):
11 biases = tf.Variable(tf.zeros([1, out_size]) + 0.1,name=‘b‘)
12 tf.summary.histogram(layer_name+‘/biases‘,biases)
13 with tf.name_scope(‘Wx_plus_b‘):
14 Wx_plus_b = tf.matmul(inputs, Weights) + biases
15 if activation_function is None:
16 outputs = Wx_plus_b
17 else:
18 outputs = activation_function(Wx_plus_b)
19 tf.summary.histogram(layer_name+‘/outputs‘,outputs)
20 return outputs
22 x_data=np.linspace(-1,1,300,dtype=np.float32)[:,np.newaxis]
23 noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32)
24 y_data=np.square(x_data)-0.5+noise 25 with tf.name_scope(‘inputs‘):
26 xs=tf.placeholder(tf.float32,[None,1],name=‘x_input‘)
27 ys=tf.placeholder(tf.float32,[None,1],name=‘y_input‘)
29 l1=add_layer(xs,1,10,n_layer=1,activation_function=tf.nn.relu) #隐藏层
30 prediction=add_layer(l1,10,1,n_layer=2,activation_function=None) #输出层
31 with tf.name_scope(‘loss‘):
32 loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),
33 reduction_indices=[1]))
34 tf.summary.scalar(‘loss‘,loss)
#Loss
的变化图和之前设置的方法略有不同.loss是在tesnorBorad 的scalars下面的, 这是由于我们使用的是tf.summary.scalar()方法.
35 with tf.name_scope(‘train‘):
36 train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
37 init = tf.global_variables_initializer()
38 sess = tf.Session()
39 merged=tf.summary.merge_all()
#tf.summary.merge_all()方法会对我们所有的summary合并到一起.
40 writer=tf.summary.FileWriter(‘logs/‘,sess.graph)
41 sess.run(init)
42 for i in range(1000):
43 sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
44 if i%50==0:
45 result = sess.run(merged,feed_dict={xs:x_data,ys:y_data})
46 writer.add_summary(result,i)
以上是关于tensorboard可视化的主要内容,如果未能解决你的问题,请参考以下文章
深度学习pytorch使用tensorboard可视化实验数据
TensorFlow篇--Tensorflow框架可视化之Tensorboard
tensorboard-理解tensorboard IMAGE标签