tensorflow变量命名空间与可视化

作者: 阿发贝塔伽马 | 来源:发表于2018-05-17 22:18 被阅读45次

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右上角+号展开


展开前
展开后

相关文章

  • tensorflow变量命名空间与可视化

    variable_scope 使用tf.variable_scope定义的命名空间,只要空间名称不同,定义的变量互...

  • tensorflow命名空间

    1. 功能: 为防止变量复用造成的错误,比如第一层网络的权重叫weight,第二层的权重就必须得换个名字了. 加了...

  • c++命名空间

    一.命名空间的三种引用 1.第一种引入命名空间变量 2.第二种引入命名空间变量 3.第三种引入命名空间变量 二.命...

  • Python基础手册28——模块的高级概念

    三、模块命名空间 命名空间(名称空间)中保存了变量名到对象的映射。向命名空间添加变量名的操作过程涉及到绑定变量到指...

  • 转载:tensorflow变量命名

    转载:http://www.cnblogs.com/salan668/p/6994928.html

  • Python动态创建变量

    命名空间locals() 局部命名空间 globals() 全局命名空间 创建局部变量--全局不能使用 创建全局变...

  • python中的作用域与命名空间

    python中的几个命名空间:1.局部命名空间:包含局部变量2.全局命名空间: 当前模块的最外层的全局变量3.内置...

  • 深度学习——tensorflow可视化

    前言:主要介绍一下,TensorFlow变量作用域和可视化,变量作用域能更好底可视化展示数据流图 TensorFl...

  • C++ 项目相关知识命名空间

    命名空间 命名空间一般是对全局变量,函数,类做处理的,以防多个重名变量引起的重定义错误。命名空间主要分为三种 你指...

  • 关于 Python 命名空间

    在 Python 中,使用命名空间记录变量。Python 中的命名空间就像是一个 dict ,key 是变量的名字...

网友评论

    本文标题:tensorflow变量命名空间与可视化

    本文链接:https://www.haomeiwen.com/subject/abvidftx.html