tensorflow变量命名空间与可视化

variable_scope

  • 使用tf.variable_scope定义的命名空间,只要空间名称不同,定义的变量互不干挠,即使函数name参数相同

  • 如果是在相同命名空间下,

如果是不可重用的(reuse=False),tf. get_variable函数会查找在当前命名空间下是否存在由tf.get_variable定义的同名变量(而不是tf.Variable定义的),如果不存在,则新建对象,否则会报错

如果是可重用的(reuse=True),如果存在,则会返回之前的对象,否则报错,

tf. Variable不管在什么情况下都是创建新变量,自己解决命名冲突

下面举个例子说明

import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
sess = tf.InteractiveSession()
with tf.variable_scope("scope1"):
    w1 = tf.get_variable("w1", initializer=4.)
    w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope2"):
    w1_p = tf.get_variable("w1", initializer=5.)
    w2_p = tf.Variable(1.0, name="w2")
with tf.variable_scope("scope1", reuse=True):
    w1_reuse = tf.get_variable("w1")
    w2_reuse = tf.Variable(1.0, name="w2")

def compare_var(var1, var2):
    print '-----------------'
    if var1 is var2:
        print sess.run(var2)
    print var1.name, var2.name
sess.run(tf.global_variables_initializer())

compare_var(w1, w1_p)
compare_var(w2, w2_p)
compare_var(w1, w1_reuse)
compare_var(w2, w2_reuse)

name_scope

  • 使用name_scope命名空间

get_variable不受name_scope命名空间约束

Variable受命名空间约束,但可以自己解决冲突

import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
sess = tf.InteractiveSession()

with tf.name_scope("scope1"):
    w1 = tf.Variable(0.0, name="w1")
    w2 = tf.get_variable("w2", initializer=4.)
with tf.name_scope("scope1"):
    w1_p = tf.Variable(1.0, name="w1")

    w2_p = tf.get_variable("w1", initializer=5.)

def compare_var(var1, var2):
    print '-----------------'
    if var1 is var2:
        print sess.run(var2)
    print var1.name, var2.name
    print '-----------'
sess.run(tf.global_variables_initializer())

compare_var(w1, w2)
compare_var(w1_p, w2_p)

总结两个命名空间的作用不同

variable_scope与get_variable搭配使用可以使得共享变量
name_scope主要用来tensorboard可视化

tensorboard可视化

import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
sess = tf.InteractiveSession()
log_dir = '../datachuli'
def practice_num():
# 练习1: 构建简单的计算图
    input1 = tf.constant([1.0, 2.0, 3.0],name="input1")
    input2 = tf.Variable(tf.random_uniform([3]),name="input2")
    output = tf.add_n([input1,input2],name = "add")

    sess.run(tf.global_variables_initializer())
    sess.run(output)
#生成一个写日志的writer,并将当前的tensorflow计算图写入日志
    writer = tf.summary.FileWriter(log_dir + "/log",tf.get_default_graph())
    writer.close()
    
practice_num()
  • 加入命名空间,tensorboard可视化将非常有层次感,更清晰
ops.reset_default_graph()
sess = tf.InteractiveSession()

def practice_num_modify():
    #将输入定义放入各自的命名空间中,从而使得tensorboard可以根据命名空间来整理可视化效果图上的节点
    # 练习1: 构建简单的计算图
    with tf.name_scope("input1"):
        input1 = tf.constant([1.0, 2.0, 3.0],name="input1")
    with tf.name_scope("input2"):
        input2 = tf.Variable(tf.random_uniform([3]),name="input2")
    with tf.name_scope('add1'):
        output = tf.add_n([input1,input2],name = "add")
    sess.run(tf.global_variables_initializer())
    sess.run(output)

#生成一个写日志的writer,并将当前的tensorflow计算图写入日志
    writer = tf.summary.FileWriter(log_dir + "/log_namescope",tf.get_default_graph())
    writer.close()
practice_num_modify()

可以点击add1和input2右上角+号展开


展开前

展开后
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文章均迁移到我的主页 http://zhenlianghe.com my github: https://gith...
    LynnHoHZL阅读 2,309评论 0 2
  • 共享变量 - TensorFlow 官方文档中文版 - 极客学院Wiki 你可以在怎么使用变量中所描述的方式来创建...
    hzyido阅读 713评论 1 1
  • 对于TensorFlow中的基本用法,参考了网上很多博客,把自己认为解释的比较好的做一些整理。 tensorflo...
    有你的世界5726阅读 2,437评论 0 1
  • #玩卡不卡·每日一抽# 每一位都可以通过这张卡片觉察自己: 1、直觉他叫什么名字?岳岳 2、他几岁了? 快六岁了...
    陈恒丽阅读 195评论 0 0
  • 雨水不住的打在绾月的脸上,好似一把把匕首不停的剜着绾月的心。她受伤的左脚一瘸一拐的跟在冷连城的身后,泪眼婆娑地...
    绾月小仙女阅读 355评论 0 0