【TensorFlow】TensorFlow实战Google深度学习框架第2版参考代码(11-TensorBoard可视化)

《TensorFlow实战Google深度学习框架第2版》教材中的样例代码,由于tensorflow版本、运行过程的修改、敲码过程中的失误、教材样例自带的错误等原因,可能会导致代码与教材不一致或者不能运行成功,仅供参考。

第11章 TensorBoard可视化

  • 11.1 一个简单的向量相加程序,完成了TensorBoard日志输出功能的TensorFlow程序
#!/usr/bin/python
# -*- coding: utf-8 -*-

import tensorflow as tf

# 定义一个简单的计算图,实现向量加法的操作
input1 = tf.constant([1.0, 2.0, 3.0], name="input1")
input2 = tf.Variable(tf.random_uniform([3]), name="input2")
output = tf.add_n([input1, input2], name="add")

# 生成一个写日志的writer,并将当前的TensorFlow计算图写入门志,TensorFlow提供了多种写日志文件的API
writer = tf.summary.FileWriter("/path/to/tensorboard/log", tf.get_default_graph())
writer.close()
  • 11.1 TensorBoard启动命令
#运行TensorBoard,并将日志的地址指向上面程序日志输出的地址
tensorboard --logdir=/path/to/log
  • 11.2 命名空间
#!/usr/bin/python
# -*- coding: utf-8 -*-

import tensorflow as tf

with tf.variable_scope("foo"):
    # 在命名空间foo下获取变量"bar",于是得到的变量名称为"foo/bar"
    a = tf.get_variable("bar", [1])
    print(a.name)  # 输出: foo/bar:O

with tf.variable_scope("bar"):
    # 在命名空间bar下获取变量"bar",于是得到的变量名称为"bar/bar",此时变量"bar/bar和变量"foo/bar"并不冲突,于是可以正常运行
    b = tf.get_variable("bar", [1])
    print(b.name)  # 输出: bar/bar:O

with tf.name_scope("a"):
    # 使用tf.Variable函数生成变量会受tf.name_scope影响,于是这个变量的名称为"a/Variable"
    a = tf.Variable([1])
    print(a.name)  # 输出: a/Variable:O
    # tf.get_variable函数不受tf.name_scope函数的影响,于是变量并不在a这个命名空间中
    a = tf.get_variable("b", [1])
    print(a.name)  # 输出: b:O
  • 11.2 引用了命名空间管理的向量相加程序
#!/usr/bin/python
# -*- coding: utf-8 -*-

import tensorflow as tf

# 将输入定义放入各自的命名空间中,从而使得TensorBoard可以根据命名空间来整理可视化效果图上的节点
with tf.name_scope("input1"):
    input1 = tf.constant([1.0, 2.0, 3.0], name="input1")

with tf.name_scope("input2"):
    input2 = tf.Variable(tf.random_uniform([3]), name="input2")
output = tf.add_n([input1, input2], name="add")

writer = tf.summary.FileWriter("/path/to/tensorboard/log", tf.get_default_graph())
writer.close()
  • 11.2 mnist_train.py程序
#!/usr/bin/python
# -*- coding: utf-8 -*-

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from chapter05 import mnist_inference  # 注意这里的import包名

BATCH_SIZE = 100
LEARNING_RATE_BASE = 0.8
LEARNING_RATE_DECAY = 0.99
REGULARIZATION_RATE = 0.0001
TRAINING_STEPS = 30000
MOVING_AVERAGE_DECAY = 0.99

INPUT_NODE = 784
OUTPUT_NODE = 10
LAYER1_NODE = 500


def train(mnist):
    # 将处理输入数据的计算都放在名字为"input"的命名空间下
    with tf.name_scope('input'):
        x = tf.placeholder(
            tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
        y_ = tf.placeholder(
            tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-cinput')

    regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
    y = mnist_inference.inference(x, regularizer)
    global_step = tf.Variable(0, trainable=False)

    # 将处理滑动平均相关的计算都放在名为moving_average的命名空间下
    with tf.name_scope("moving_average
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值