本篇内容介绍利用 tensorboard 可视化整个网络的建造过程以及训练过程
具体步骤如下:
1 利用 tf.summary.histogram()函数输出一个直方图的Summary protocol buffer
选择观看Weights,biases,outputs的变化
原代码
def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
with tf.name_scope('layer'):
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W')
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b')
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b, )
return outputs
加入 tf.summary.histogram()之后代码:
def add_layer(inputs, in_size, out_size, n_layer, activation_function=None):
# add one more layer and return the output of this layer
layer_name = 'layer%s' % n_layer
with tf.name_scope(layer_name):
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W')
tf.summary.histogram(layer_name + '/weights', Weights)
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b')
tf.summary.histogram(layer_name + '/biases', biases)
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b, )
tf.summary.histogram(layer_name + '/outputs', outputs)
return outputs
选择观看loss的变化
原代码:
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
修改之后代码:
注意loss的变化显示语句不同,为 tf.summary.scalar(‘loss’, loss)
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
tf.summary.scalar('loss', loss) #命名显示的名称以及传递值
2 将所有的summary合并在一起打包,放在SummaryWriter上面
添加命令:
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("logs/", sess.graph)
3 加入代码
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})#run一下merge,返回summary存入result
writer.add_summary(result, i) #将result放进writer里面
4 运行程序,生成文件
5 打开cmd,运行如下命令
tensorboard --logdir = '上面生成文件的路径'
有如下结果:
6 复制以上网址在浏览器打开(谷歌最好)
显示结果如下:
之后便可对图像进行分析,进一步了解神经网络的建造过程和训练过程。
注:以上代码来源网络,只是对其分析理解。