前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TensorFlow可视化工具-TensorBoard上手

TensorFlow可视化工具-TensorBoard上手

作者头像
百川AI
发布2021-10-19 16:35:46
7120
发布2021-10-19 16:35:46
举报
文章被收录于专栏:我还不懂对话我还不懂对话

TensorBoard是TensorFlow自带的神经网络可视化工具,用直观的流程图显示神经网络,更好的理解学习。

与 tensorboard 兼容的浏览器是 “Google Chrome”.

定义网络

inputs

代码语言:javascript
复制
# 定义输入inputs
with tf.name_scope('inputs'):
    # 定义inputs中的x_input,y_input
    xs = tf.placeholder(tf.float32, [None, 1], name='x_input')
    ys = tf.placeholder(tf.float32, [None, 1], name='y_input')
img
img

layer

添加layer的函数add_layer()

代码语言:javascript
复制
def add_layer(inputs, in_size, out_size, activation_function=None):
    # 添加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 outputs123456789101112131415123456789101112131415

添加中间层l1

代码语言:javascript
复制
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)11
img
img

添加输出层prediction

代码语言:javascript
复制
prediction = add_layer(l1, 10, 1, activation_function=None)11

loss

代码语言:javascript
复制
# 计算真实数据和预测的误差
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
                                        reduction_indices=[1]))12341234
img
img

train

代码语言:javascript
复制
# train使用梯度下降最小化损失loss
with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)123123
img
img

将sess.graph写入到图中,方便

代码语言:javascript
复制
# 获得Session
sess = tf.Session()
# 将sess.graph写入到图中
writer = tf.summary.FileWriter("logs/", sess.graph)
# 初始化
init = tf.global_variables_initializer()
# 跑起来
sess.run(init)1234567812345678
代码语言:javascript
复制
for i in range(1000):
    # 每一步放入数据训练。
    sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
    if i % 50 == 0:
        # 每50记录所有的summary
        result = sess.run(merged,
                          feed_dict={xs: x_data, ys: y_data})
        writer.add_summary(result, i)1234567812345678

对于Linux和mac os:

代码语言:javascript
复制
# direct to the local dir and run this in terminal:
# $ tensorboard --logdir='logs'1212

对于windows,必须填写完整的路径,并且不要加”,摸索了半天/(ㄒoㄒ)/~~:

代码语言:javascript
复制
# direct to the local dir and run this in terminal:
# $ tensorboard --logdir=dir
# 例如
tensorboard --logdir=D:\project\python3code\mofan\logs12341234

然后使用Chrome中,输入http://localhost:6006,然后切换到GRAPHS就可以看到定义的图了。

总体结构图:

img
img

训练中loss下降曲线:

img
img

训练中layer的权重和biases变化情况。(表征其值的概率分布)

img
img

训练中layer的变化:

img
img

完整代码:tensorboard1.py

Reference

  1. 莫烦的代码
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-10-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 定义网络
    • inputs
      • layer
        • loss
          • train
          • Reference
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档