TensorFlow可视化工具-TensorBoard上手

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

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

定义网络

inputs

# 定义输入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

layer

添加layer的函数add_layer()

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

l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)11

img

添加输出层prediction

prediction = add_layer(l1, 10, 1, activation_function=None)11

loss

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

img

train

# train使用梯度下降最小化损失loss
with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)123123

img

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

# 获得Session
sess = tf.Session()
# 将sess.graph写入到图中
writer = tf.summary.FileWriter("logs/", sess.graph)
# 初始化
init = tf.global_variables_initializer()
# 跑起来
sess.run(init)1234567812345678
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:

# direct to the local dir and run this in terminal:
# $ tensorboard --logdir='logs'1212

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

# 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

训练中loss下降曲线:

img

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

img

训练中layer的变化:

img

完整代码:tensorboard1.py

Reference

  1. 莫烦的代码
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百川AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值