1. 程式人生 > >tensorflow-資料流圖彙總及執行次數(tf.summary)

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