码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-11-27 21:05:32      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:update   group   ssi   cal   file   variable   efault   date   env   

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Thu Sep 6 10:16:37 2018 @author: myhaspl @email:myhaspl@myhaspl.com """ 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:myhaspl@myhaspl.com
"""

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)

标签:update   group   ssi   cal   file   variable   efault   date   env   

原文地址:http://blog.51cto.com/13959448/2322798

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!