Tensorboard實現神經網路的視覺化

Tensorboard實現神經網路的視覺化

Tensorboard實現神經網路的視覺化

 本篇部落格介紹使用Tensorboard實現神經網路的視覺化,首先是實現視覺化的程式碼:

# encoding:utf-8
import tensorflow as tf
# 新增層
def add_layer(inputs, 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]), name='W')
with tf.name_scope('bias'):
b = tf.Variable(tf.zeros([1, out_size])   0.1, name='b')
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.matmul(inputs, W)   b
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
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')
# 隱藏層和輸出層
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
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]))
# 用梯度下降更新loss
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# 初始化所有引數
init = tf.initialize_all_variables()
sess = tf.Session()
weiter = tf.summary.FileWriter("logs/", sess.graph)
sess.run(init)

注:該方法可能只適用於win10系統。

這段程式碼會在logs資料夾下生成 events.out.tfevents.1530933559.CC (示例)。

然後進入命令列下,cd到logs的上一級目錄下,如我的logs在

D:\Python27\Lib\site-packages\django\bin\pylearn\deeplearning\tensorflow\logs

目錄下,只需在命令列下cd到

D:\Python27\Lib\site-packages\django\bin\pylearn\deeplearning\tensorflow

目錄即可。

然後執行

tensorboard –logdir=logs

最後,根據提示在瀏覽器中輸入相關網址(如我的網址為:http://cc:6006/#graphs),在Graphs標籤下即可看到建立的圖