tensorboard可视化

​         为了更方便 TensorFlow 程序的理解、调试与优化,有一套叫做 TensorBoard 的可视化工具。你可以用 TensorBoard 来展现你的 TensorFlow 图像,绘制图像生成的定量指标图以及附加数据。TensorBoard 通过读取 TensorFlow 的事件文件来运行。TensorFlow 的事件文件包括了你会在 TensorFlow 运行中涉及到的主要数据。。显示的神经网络差不多是这样的:

1

        同时我们也可以展开看每个layer中的一些具体的结构:

1

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 优化器optimizer
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt


def add_layer(inputs,insize,outsize,activation_function=None):
with tf.name_scope('layer'):
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random_normal([insize,outsize]), name='W')
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1,outsize]) + 0.1, name='b')
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.matmul(inputs,Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)

return outputs

x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise
y = np.random.random(y_data.shape)
with tf.name_scope('inputs'):
xs = tf.placeholder(tf.float32, [None, 1], name='x_input')
ys = tf.placeholder(tf.float32, [None, 1], name='y_input')
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)
# 这里对矩阵按行求和没有什么作用,因为数据每行只有一个实例,还是它本身
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
writer = tf.summary.FileWriter("logs/", sess.graph)
sess.run(init)

        执行完成会在logs文件夹下生成一个事件文件。

        打开终端,切换到py文件的目录,输入:tensorboard –logdir=./logs

        注意:文件的路径一定要加“.” ,否则会找不到那个事件文件

在浏览器打开终端显示的链接:http://DESKTOP-RKATBUI:6006

即可看到tensorboard的界面,如下。

3

参考:

https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/4-1-tensorboard1/

本文标题:tensorboard可视化

文章作者:goingcoder

发布时间:2018年04月06日 - 21:04

最后更新:2018年04月06日 - 21:04

原始链接:https://goingcoder.github.io/2018/04/06/tf6/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------