tensorflow-数据流图汇总及运行次数(tf.summary)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tensorflow-数据流图汇总及运行次数(tf.summary)相关的知识,希望对你有一定的参考价值。


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Sep  6 10:16:37 2018
@author: myhaspl
@email:[email protected]
"""

import tensorflow as tf
g1=tf.Graph()

with g1.as_default(): 
    my_var=tf.Variable(1,dtype=tf.float32)
    my_step=tf.Variable(1,dtype=tf.int32)
    varop=tf.assign_add(my_var,tf.pow(my_var,2))
    stepop=tf.assign_add(my_step,1)
    init=tf.initialize_all_variables()
    addop=tf.group([varop,stepop])

with tf.Session(graph=g1) as sess:  
    sess.run(init)
    print sess.run([my_step,my_var])
    sess.run(addop)
    print sess.run([my_step,my_var])
    sess.run(addop)
    print sess.run([my_step,my_var])
    sess.run(addop)
    print sess.run([my_step,my_var])

[1, 1.0]
[2, 2.0]
[3, 6.0]
[4, 42.0]

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Sep  6 10:16:37 2018
@author: myhaspl
@email:[email protected]
"""

import tensorflow as tf
g1=tf.Graph()

with g1.as_default(): 
    with tf.name_scope("input_Variable"):        
        my_var=tf.Variable(1,dtype=tf.float32)
    with tf.name_scope("global_step"):
        my_step=tf.Variable(0,dtype=tf.int32)
    with tf.name_scope("update"):
        varop=tf.assign_add(my_var,tf.pow(my_var,2))
        stepop=tf.assign_add(my_step,1)
        addop=tf.group([varop,stepop])
    with tf.name_scope("summaries"):
        tf.summary.scalar(‘myvar‘,my_var)
    with tf.name_scope("global_ops"):
        init=tf.global_variables_initializer()
        merged_summaries=tf.summary.merge_all()

with tf.Session(graph=g1) as sess:  
    writer=tf.summary.FileWriter(‘sum_vars‘,sess.graph)
    sess.run(init)
    #---0
    step,var,summary=sess.run([my_step,my_var,merged_summaries])
    writer.add_summary(summary,global_step=step)
    print step,var
    #---1
    sess.run(addop)
    step,var,summary=sess.run([my_step,my_var,merged_summaries])
    writer.add_summary(summary,global_step=step)
    print step,var
    #--2
    sess.run(addop)
    step,var,summary=sess.run([my_step,my_var,merged_summaries])
    writer.add_summary(summary,global_step=step)
    print step,var
    #--3
    sess.run(addop)
    step,var,summary=sess.run([my_step,my_var,merged_summaries])
    writer.add_summary(summary,global_step=step)
    print step,var  

    writer.flush()
    writer.close()

0 1.0
1 2.0
2 6.0
3 42.0

以上是关于tensorflow-数据流图汇总及运行次数(tf.summary)的主要内容,如果未能解决你的问题,请参考以下文章

学习笔记TF039:TensorBoard

Tensorflow变量及共享变量

tensorflow

TensorFlow遇到的问题汇总(持续更新中......)

『TensorFlow』0.x_&_1.x版本框架改动汇总

Tensorflow(tf.Graph)和(tf.session)