TensorFlow:作用域name_scope和variable_scope
Tensorflow中的名称作用域和变量作用域
简单来说name_scope是给Op_name加前缀的,variable_scope是给变量variable_name和Op_name加前缀的.作用域在使用Tensorboard对Graph对象进行可视化的时候很有帮助,作用域会把一些Op划分到较大的语句块当中.使用tensorboard可视化数据流图的时候,每个作用域都对自己的Op进行封装,从而获得更好的可视化效果.
name_scope
基本用法是把Op添加到with tf.name_scope()语句块当中
import tensorflow as tf
# 接下来在默认图中,划分作用域,并添加Op
with tf.name_scope('A'):a = tf.add(1, 2, name='A_add')b = tf.subtract(5, a, name='A_sub')
# 作用域B
with tf.name_scope('B'):c = tf.add(3, 4, name='B_add')d = tf.subtract(c, 5, name='B_sub')
# 在作用域外部,添加Op
e = tf.add(b, d, name='output')#使用TensorBoard对计算图进行可视化,创建一个Summary对象,传入路径参数和图对象
writer = tf.summary.FileWriter('./name_scope_2', graph=tf.get_default_graph())
writer.close()
Variable变量的作用域
tf.varibale_scope() #变量指定命名空间
tf.get_variable(name, shape, dtype, initializer) # 通过标识名参数创建或返回一个变量
# tf.get_variable_scope().reuse == False时,(默认为False)变量作用域只能用来创建新的变量,
with tf.variable_scope('var'):v = tf.get_variable('v1', [1])v2 = tf.get_variable('v2', [1])
# tf.get_variable_scope().reuse == True时,作用域可以共享变量
#with tf.variable_scope('var2') as scope:#v = tf.get_variable('v', [1])
# 或者
with tf.variable_scope('var3', reuse=True): # scope.reuse_variables()v3 = tf.get_variable('v3', [1])
---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-29-df5e8f9929d5> in <module>()
----> 6 v3 = tf.get_variable('v3', [1])ValueError: Variable var3/v3 does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?
# 获取变量的作用域
with tf.variable_scope('var4') as var4_scope:v4 = tf.get_variable('v', [1])with tf.variable_scope('var5') as var5_scope:v5 = tf.get_variable('w', [1])
assert var4_scope.name == 'var4'
assert v4.name == 'var4/v:0'
assert var5_scope.name == 'var4/var5'
assert v5.name == 'var4/var5/w:0'
变量作用域的初始化
变量作用域可以默认携带一个初始化器,在这个作用域中的子作用域或者变量都可以继承或者重新父作用域的初始化器
with tf.variable_scope('v1', initializer=tf.constant_initializer(0.4)):a = tf.get_variable('a', [1])#assert a.eval() == 0.4 # a被作用域初始化为0.4b = tf.get_variable('b', [1], initializer=tf.constant_initializer(0.3))#assert b.eval() == 0.3 # 复写初始化器的值with tf.variable_scope('v2'):a = tf.get_variable('a', [1])# assert a.eval() == 0.4 继承第一个初始化器
---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-32-c3e093686d98> in <module>()
----> 5 b = tf.get_variable('b', [1], initializer=tf.constant_initializer(0.3))ValueError: Variable v1/b does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?
name_scope示例
name_scope的作用域范围会影响到Op_name,但是不会作用到tf.get_variable()创建的变量
会作用到tf.Variable()创建的变量
with tf.variable_scope('funny',reuse=True):with tf.name_scope('haha'):e = tf.get_variable('E', [1])# name参数,给op指定标识d = tf.Variable(tf.zeros([1]), name='zero')f = 1 + e # 等价于 f = tf.add(1, e)print 'e.name ' ,e.name
print 'd.name ' ,d.name
print 'd.op.name ',d.op.name
print'f.name ' ,f.name
print'f.op.name ',f.op.name
e.name funny/E:0
d.name funny_8/haha/zero:0
d.op.name funny_8/haha/zero
f.name funny_8/haha/add:0
f.op.name funny_8/haha/add
funny_数字,这个数字没执行一次,获取作用域的操作都会+1
TensorFlow:作用域name_scope和variable_scope相关推荐
- tensorflow中name_scope和variable_scope变量的使用
1. variable_scope的使用 首先,使用variable_scope可以很方便的管理get_varibale. 如何确定 get_variable 的 prefixed name? 1.1 ...
- Tensorflow name_scope和variable_scope的用法
Tensorflow name_scope和variable_scope 的用法 简介 当一个神经网络比较复杂.参数比较多时,就比较需要一个比较好的方式来传递和管理这些参数.而Tensorflow提供 ...
- TensorFlow学习笔记(1):variable与get_variable, name_scope()和variable_scope()
Variable tensorflow中有两个关于variable的op,tf.Variable()与tf.get_variable()下面介绍这两个的区别 使用tf.Variable时,如果检测到命 ...
- 【TensorFlow-windows】name_scope与variable_scope
前言 探索一下variable_scope和name_scope相关的作用域,为下一章节tensorboard的学习做准备 其实关于variable_scope与get_variable实现变量共享, ...
- Tensorflow中的name_scope和variable_scope
Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行 ...
- TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习
转载http://blog.csdn.net/jerr__y/article/details/60877873 1. 首先看看比较简单的 tf.name_scope('scope_name'). tf ...
- tensorflow tf.name_scope() 命名空间(用于规定对象和操作属于哪个区域)
tf.name_scope()规定了对象和操作属于哪个区域 本质上name_scope只对对象的name属性进行圈定,并不会对其作用域产生任何影响 tf.name_scope('cgx_scope') ...
- tf.name_scope tf.variable_scope学习
1. 首先看看比较简单的 tf.name_scope('scope_name'). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...
- 接上篇,CNN在短文本分类中的应用遇到的一些问题/GPU/cuda/tensorflow
主要记录一些用CNN在文本分类(tensorflow)时遇到的问题/GPU.cuda等开发环境的配置问题 主要记录一些用CNN在文本分类tensorflow时遇到的问题GPUcuda等开发环境的配置问 ...
- 通俗理解tf.name_scope()、tf.variable_scope()
前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记: tf.name_scope ...
最新文章
- [dp] Jzoj P5804 简单的序列
- 从词向量到Bert——简单作业题+讲解
- grails 环境找不到java_home
- shell脚本之nginx的安装
- JavaScript语句模版
- flutter Web打包
- php中var_dump是什么意思,php中的var_dump()方法的詳細說明
- boost::phoenix::function相关的测试程序
- oracle expdp自动导出数据,Oracle expdp数据泵远程导出
- Linux C语言调用C++动态链接库
- 重磅!云+X 案例征集正式启动啦!
- python装饰器系列(五)
- 微信小程序四种父子相互传值方式
- VIO-为什么要进行在线时间标定
- 吴裕雄--天生自然 高等数学学习:对坐标的曲面积分
- svn删除文件文件夹(遇到的问题解决)
- sql分组排序mysql_SQL分组排序
- python递归函数例子_Python递归函数经典案例-汉诺塔问题
- MySQL的between语句和in语句的区别
- 小白装机工具提示在引导修复时检测到错误解决方法