TensorFlow——可视化(1)

TensorFlow可视化

参考文章链接

https://blog.csdn.net/fa928464158/article/details/77935539
https://www.jianshu.com/p/63672bb761cf

过程

1 可视化实现代码

'''
实现tensorboard可视化
'''
import tensorflow as tf
# 构造grah
graph = tf.Graph()
with graph.as_default():
    #全局变量
    with tf.name_scope("variables"):
        # 记录数据流图运行次数的Variables的对象
        global_step = tf.Variable(0,dtype=tf.int32,trainable=False,name="global_step")
        # 记录所有输出随时间累加和的Variables对象
        total_output = tf.Variable(0.0,dtype=tf.float32,trainable=False,name="total_output")

        # 主要变换的op
    with tf.name_scope("transformation"):
            # 独立的输入层
        with tf.name_scope("input"):
            a=tf.placeholder(tf.float32,shape=[None],name="input_placehonder_a")

        with tf.name_scope("intermediate_layter"):
            b = tf.reduce_prod(a,name="product_b")
            c = tf.reduce_sum(a,name="sum_c")

        with tf.name_scope("output"):
            output = tf.add(b,c,name="output")

    with tf.name_scope("update"):
        updata_total = total_output.assign_add(output)
        increment_step = global_step.assign_add(1)

    # 汇总op
    with tf.name_scope("summaries"):
        avg = tf.div(updata_total,tf.cast(increment_step,tf.float32),name="average")
        # 为输出节点创造汇总数据
        # tf.summary.scalar('output',output,collections=None,name="output_summary")
        # tf.summary.scalar('Sum of output over time',updata_total,collections=None,name="total_summary")
        # tf.summary.scalar('Average of output over time',avg,collections=None,name='average_summary')

        tf.summary.scalar('output',output,collections=None)
        tf.summary.scalar('Sum of output over time',updata_total)
        tf.summary.scalar('Average of output over time',avg)

        merged_sumaries = tf.summary.merge_all()


    # 全局variable对象和op
    with tf.name_scope("globle_ops"):
        # 初始化op
        init = tf.initialize_all_variables()
        # 将所有汇总合并到一个op中
        # merged_sumaries=tf.merge_all_summarises()

sess = tf.Session(graph=graph)
writer = tf.summary.FileWriter('./improved_graph',sess.graph)
sess.run(init)

def run_graph(input_tensor):
    feed_dict={a:input_tensor}
    _,step,summary=sess.run([output,increment_step,merged_sumaries], feed_dict = feed_dict)
    print(step)
    print(summary)
    writer.add_summary(summary,global_step=step)

run_graph([2,8])
run_graph([3,6])



writer.flush()
writer.close()
sess.close()

2 步骤

(1)代码run后生成log文件
TensorFlow——可视化(1)_第1张图片
(2)定位到log文件的保存目录上,如上图,并复值该目录
(3)打开cmd到终端,cd到log文件所在的目录
(4)输入tensorboard.exe --logdir="D:\work\python\pycharm_code\improved_graph"
在这里插入图片描述
(5)打开浏览器,输入网址localhost:6006,此时可以看到下图的可视化界面,tensorboard启动完成"
TensorFlow——可视化(1)_第2张图片

你可能感兴趣的:(遇到的问题及解决)