您的位置:首页 > 其它

tensorflow tensorboard可视化

2017-12-08 23:44 447 查看
参考链接:

https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/4-1-tensorboard1/  莫烦tensorboard可视化教程

http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/summaries_and_tensorboard.html  tensorboard可视化学习

http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/graph_viz.html  tensorboard图表可视化

tensorBoard的输入是tensorflow保存summary data的日志文件。日志文件名的形式如:events.out.tfevents.1467809796.lei-All-Series 。

TensorBoard可读的summary data有scalar,images,audio,histogram和graph

1.对计算图进行可视化操作

(1)使用tf.name_scope()可以封装操作为一个结点,结点的名字即为参数名,结点打开可以看到具体操作

(2)使用name属性可以指定在计算图中的显示的名字

(3)需要使用 tf.summary.FileWriter()  将计算图保存到一个目录中,以方便后期在浏览器中可以浏览。 此方法中的第二个参数需要使用sess.graph , 因此我们需要把这句话放在获取session的后面。

(4)可视化全连接层:当选择用 tensorflow 中的激励函数(activation function)的时候,tensorflow会默认添加名称



全部代码:

import tensorflow as tf

def add_layer(X_data, in_size, out_size, activation_function=None):
with tf.name_scope('layer'):
with tf.name_scope('weights'):
W = tf.Variable(tf.random_normal([in_size, out_size], 0, 0.05), name='W')
with tf.name_scope('biases'):
b = tf.Variable(tf.zeros([out_size]), name='b')
with tf.name_scope('Wx_plus_b'):
wx_plus_b = tf.matmul(X_data, W) + b
if activation_function is None:
return wx_plus_b
else:
return activation_function(wx_plus_b)

sess = tf.InteractiveSession()

with tf.name_scope('inputs'):
X_data = tf.placeholder(tf.float32, [None, 784], name='x_in')
y_data = tf.placeholder(tf.float32, [None, 10], name='y_in')

y = add_layer(X_data, 784, 10, tf.nn.softmax)

with tf.name_scope('loss'):
J = tf.reduce_mean(-tf.reduce_sum(y_data * tf.log(y)), name='loss')
with tf.name_scope('train_step'):
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(J)

is_correct_prediction = tf.equal(tf.argmax(y, axis=1), tf.argmax(y_data, axis=1))
accuracy = tf.reduce_mean(tf.cast(is_correct_prediction, tf.float32))

tf.global_variables_initializer().run()
writer = tf.summary.FileWriter('winycg_logs/', sess.graph)


打开命令行界面:锁定到工程所在的目录,输入命令:tensorboard --logdir=winycg_logs(将计算图信息所存储的文件夹)



注:必须将命令行锁定到工程目录,如果直接采用命令:tensorboard --logdir=E:/PythonProject/winycg_logs,tensorboard将无法找到计算图信息

将显示的浏览器地址在谷歌浏览器上打开,打开GRAPHS栏目,显示生成的计算图



2.可视化训练过程中参数的变化

(1)使用tf.summary.histogram()绘制权重W,偏置b还有Wx+b等,因为这些参数都是多个值,可以绘制出参数值的分布

(2)使用tf.summary.scalar()绘制误差loss,学习速率learning_rate等,因为函数绘制的为折线图,且这些参数每次训练只有1个

如下为拟合一元二次方程的例子:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def add_layer(input, layer_name, in_size, out_size, activation_function=None):
with tf.name_scope(layer_name):
with tf.name_scope('weights'):
weights = tf.Variable(tf.random_normal([in_size, out_size]))
tf.summary.histogram(layer_name + '/weights', weights)
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([out_size]))
tf.summary.histogram(layer_name + '/biases', biases)
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.matmul(input, weights) + biases
if activation_function is None:
output = Wx_plus_b
else:
output = activation_function(Wx_plus_b)
tf.summary.histogram(layer_name + '/output', output)
return output

X_data = np.linspace(-1, 1, 100, dtype=np.float32)[:, np.newaxis]
noise = np.random.normal(0, 0.05, (X_data.shape[0], 1))
y_data = np.square(X_data) + 0.5 + noise

X = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, 1])

hidden_layer = add_layer(X, 'hidden_layer', 1, 10, activation_function=tf.nn.relu)
output_layer = add_layer(hidden_layer, 'output_layer', 10, 1, activation_function=None)

with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.square(y - output_layer))
tf.summary.scalar('loss', loss)
trainer = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
merged = tf.summary.merge_all() #汇总所有的操作
# 创建文件记录器,将计算图加入记录器
writer = tf.summary.FileWriter('logs/', sess.graph)

for i in range(1000):
sess.run(trainer, feed_dict={X: X_data, y: y_data})
if i % 50 == 0:
summary = sess.run(merged, feed_dict={X: X_data, y: y_data})
# 将汇总结果和循环步数写入日志文件
writer.add_summary(summary, i)


tf.summary.scalar()



tf.summary.histogram()



计算图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: