美文网首页
Tensorboard进行可视化 1

Tensorboard进行可视化 1

作者: 马淑 | 来源:发表于2018-07-15 16:09 被阅读10次

我们之前已经构建了一个简单的神经网络算法。如果想在Tensorboard中可视化这个神经网络,可以用Tensorboard。还需要做的事情是:
1、指明变量名称:

with tf.name_scope('inputs'):

2、将上面‘绘画’出的图保存到"logs/"目录中,以方便后期在浏览器中可以浏览

writer = tf.summary.FileWriter("logs/", sess.graph)

3、在你的cmd终端中,找到logs文件所在的目录 ,使用以下命令:

tensorboard --logdir logs
cmd

4、将终端中显示的链接(比如我的是:http://pc0cvaz8-x7:6006)输入到浏览器中,就可以看到视图框架。

Scalar

Tensorboard中的graph可以很直观的告诉你神经网络长啥样。


Graph

对于Tensorboard的其他参数可以查看:tensorboard --help

全部代码:

import tensorflow as tf
import numpy as np

def add_layer(inputs, in_size, out_size, activation_function=None):
        with tf.name_scope('layer'):
                with tf.name_scope('weights'):
                        Weights = tf.Variable(tf.random_normal([in_size,out_size])) # Weight matrix 
                with tf.name_scope('biases'):
                        biases = tf.Variable(tf.zeros([1, out_size])+0.1) #Biases is not suggested to be zero, so set +0.1 here
                with tf.name_scope('biases'):
                        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

# Input Observed Data     
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

# Store Observed Data  with placeholder
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')

# create layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
prediction = add_layer(l1, 10, 1, activation_function = None)

# define loss function,
with tf.name_scope('loss'):
        loss = tf.reduce_mean(tf.square(ys-prediction))

# use Gradient Descent Optimizer to minimize loss
with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

sess = tf.Session()
writer = tf.summary.FileWriter('logs/',sess.graph)

# initiation
init = tf.global_variables_initializer()
sess.run(init)

for i in range(1000):
        sess.run(train_step,feed_dict={xs:x_data, ys:y_data})  # learn 1000 times
        if i%50==0:
                print(sess.run(loss, feed_dict={xs:x_data, ys:y_data})) # print loss

相关文章

网友评论

      本文标题:Tensorboard进行可视化 1

      本文链接:https://www.haomeiwen.com/subject/mumppftx.html