tensorFlow九:可视化工具

 

(可视化)生成日志文件的代码:

复制代码
import tensorflow as tf
import os
# 屏蔽info
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'

# 可视化的代码
with tf.device("/cpu:0"):
    with tf.variable_scope(name_or_scope="foo"):
        x_init1 = tf.get_variable(name="x_init1", shape=[10], initializer=tf.random_normal_initializer())
        x = tf.Variable(initial_value=x_init1, name='x')
        y = tf.placeholder(dtype=tf.float32, name='y')
        z = tf.add(x, y, name='z')
    with tf.variable_scope(name_or_scope="bar"):
        a = tf.constant(3.0) + 4.0
    w = z * a
# 开始添加、记录信息
tf.summary.scalar(name='scalar_x_init1', tensor=x_init1)
tf.summary.scalar(name='scalar_x', tensor=x)
tf.summary.scalar(name='scalar_y', tensor=y)
tf.summary.scalar(name='scalar_z', tensor=z)
tf.summary.scalar(name='scalar_w', tensor=w)

# update x
assign_op = tf.assign(x, x+1)
# 控制依赖:控制assign_op的更新操作
with tf.control_dependencies([assign_op]):
    with tf.device("/gpu:0"):
        out = x * y
tf.summary.scalar(name='scalar_out', tensor=out)

# merge all summary
merge_summary = tf.summary.merge_all()

# 构建变量初始化
init_op = tf.global_variables_initializer()

# 图的执行阶段:
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) as sess:
    sess.run(init_op)
    # 得到输出到文件的对象
    writer = tf.summary.FileWriter(logdir="./result", graph=sess.graph)
    for i in range(1, 5):
        summary, r_out, r_x, r_w = sess.run([merge_summary, out, x, w], feed_dict={y: i})
        writer.add_summary(summary=summary, global_step=i)
        print("\n{}==={}======{}====={}".format(i, r_out, r_x, r_w))
    # 将writer对象关闭
    writer.close()
复制代码

 

 

访问tensorBoard:http://localhost:6006

 

 

posted on 2019-02-11 22:30  myworldworld  阅读(164)  评论(0)    收藏  举报

导航

< 2025年8月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6
点击右上角即可分享
微信分享提示