Tensorflow name_scope
在 Tensorflow 当中有两种途径生成变量 variable, 一种是 tf.get_variable(), 另一种是 tf.Variable().
使用tf.get_variable()定义的变量不会被tf.name_scope()当中的名字所影响
import tensorflow as tf
with tf.name_scope("a_name_scope"):
initializer = tf.constant_initializer(value=1)
var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32, initializer=initializer)
var2 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32)
var21 = tf.Variable(name='var2', initial_value=[2.1], dtype=tf.float32)
var22 = tf.Variable(name='var2', initial_value=[2.2], dtype=tf.float32)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(var1.name) # var1:0
print(sess.run(var1)) # [ 1.]
print(var2.name) # a_name_scope/var2:0
print(sess.run(var2)) # [ 2.]
print(var21.name) # a_name_scope/var2_1:0
print(sess.run(var21)) # [ 2.0999999]
print(var22.name) # a_name_scope/var2_2:0
print(sess.run(var22)) # [ 2.20000005]
想要达到重复利用变量的效果, 我们就要使用 tf.variable_scope(), 并搭配 tf.get_variable()这种方式产生和提取变量. 不像 tf.Variable() 每次都会产生新的变量, tf.get_variable() 如果遇到了同样名字的变量时, 它会单纯的提取这个同样名字的变量(避免产生新变量). 而在重复使用的时候, 一定要在代码中强调 scope.reuse_variables(), 否则系统将会报错, 以为你只是单纯的不小心重复使用到了一个变量.
with tf.variable_scope("a_variable_scope") as scope:
initializer = tf.constant_initializer(value=3)
var3 = tf.get_variable(name='var3', shape=[1], dtype=tf.float32, initializer=initializer)
scope.reuse_variables()
var3_reuse = tf.get_variable(name='var3',)
var4 = tf.Variable(name='var4', initial_value=[4], dtype=tf.float32)
var4_reuse = tf.Variable(name='var4', initial_value=[4], dtype=tf.float32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(var3.name) # a_variable_scope/var3:0
print(sess.run(var3)) # [ 3.]
print(var3_reuse.name) # a_variable_scope/var3:0
print(sess.run(var3_reuse)) # [ 3.]
print(var4.name) # a_variable_scope/var4:0
print(sess.run(var4)) # [ 4.]
print(var4_reuse.name) # a_variable_scope/var4_1:0
print(sess.run(var4_reuse)) # [ 4.]
或
with tf.variable_scope('foo') as foo_scope:
v = tf.get_variable('v', [1])
with tf.variable_scope('foo', reuse=True):
v1 = tf.get_variable('v')
assert v1 == v
1. 使用tf.Variable()的时候,tf.name_scope()和tf.variable_scope() 都会给 Variable 和 op 的 name属性加上前缀。
2. 使用tf.get_variable()的时候,tf.name_scope()就不会给 tf.get_variable()创建出来的Variable加前缀。
Tensorflow name_scope的更多相关文章
- 学习笔记TF030:实现AlexNet
ILSVRC(ImageNet Large Scale Visual Recognition Challenge)分类比赛.AlexNet 2012年冠军(top-5错误率16.4%,额外数据15.3 ...
- tensorflow中的name_scope, variable_scope
在训练深度网络时,为了减少需要训练参数的个数(比如LSTM模型),或者是多机多卡并行化训练大数据.大模型等情况时,往往就需要共享变量.另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变 ...
- TensorFlow学习笔记(1):variable与get_variable, name_scope()和variable_scope()
Variable tensorflow中有两个关于variable的op,tf.Variable()与tf.get_variable()下面介绍这两个的区别 使用tf.Variable时,如果检测到命 ...
- tensorflow入门笔记(五) name_scope和variable_scope
一.上下文管理器(context manager) 上下文管理器是实现了上下文协议的对象,主要用于资源的获取与释放.上下文协议包括__enter__.__exit__,简单说就是,具备__enter_ ...
- Tensorflow中的name_scope和variable_scope
Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行 ...
- tensorflow里面共享变量、name_scope, variable_scope等如何理解
tensorflow里面共享变量.name_scope, variable_scope等如何理解 name_scope, variable_scope目的:1 减少训练参数的个数. 2 区别同名变量 ...
- 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 中 name_scope 及 variable_scope 的异同
Let's begin by a short introduction to variable sharing. It is a mechanism in TensorFlow that allows ...
- tensorflow 中 name_scope和variable_scope
import tensorflow as tf with tf.name_scope("hello") as name_scope: arr1 = tf.get_variable( ...
随机推荐
- 使用thrift实现订阅服务和发布服务
使用thrift实现订阅服务和发布服务 服务:订阅服务 market_subscriber 和 发布服务 market_publisher功能:market_subscriber 能够向 market ...
- UDP可靠传输那些事
有空来论坛走走,发现讨论udp可靠传输又热了起来,有人认为udp高效率,有人认为udp丢包重传机制容易控制,还有朋友搞极限测试,当然也有人推销自己的东西,这里写一点我个人的看法. udp可靠传输其实非 ...
- SPOJ - DQUERY D-query 主席树
题意; 给一个数列$\{ a_i\}$ 一些询问$(l_i,r_i)$ 问你$[l,r]$有多少个不同元素 题解: 其实本质思路和离线化处理询问的线段树/树状数组写法差不多,对区间$[x,r]$来说, ...
- day2 --> pyc 文件
执行python 代码时,如果导入了其他的.py文件,那么,执行过程中会自动生成一个与其同名的.pyc文件,该文件就是python解释器便宜之后产生的字节码. PS:代码经过便宜可以产生字节码;字节码 ...
- Select查询命令
一开始SELECT查询的命令为 SELECT * FROM employee; SELECT 要查询的列名 FROM 表名字 WHERE 限制条件; 若要查询所有内容,就用*代 ...
- Swift 学习- 03 -- 基本运算符
// 运算符是检查,改变,合并值的特殊符号或短语, // swift 支持大部分标准的 C语言的运算符, 且改进许多特性来减少常规编码错误. /* 如: 赋值符 (=) 不返回值, 以防止把想要判断相 ...
- Windows&Word 常用快捷键
Win:显示开始菜单 Win + E:打开文件管理器 Win + D:显示桌面 Win + L:锁定计算机 Win + I:打开设置 Win + M:最小化所有窗口 Alt + F4:1.用来关闭当前 ...
- Confluence 6 管理协同编辑 - 审计的考虑
我们知道一些客户对审计是主要考虑的方面.我们不能保证在协同编辑的时候具有审计,审查功能.所有页面的修改当前附加到用户发布页面的属性中而不是用户的特定修改. 如果这个对你来说是一个问题的话,我们建议你在 ...
- Confluence 6 连接到外部用户目录服务器的问题分析
在有关外部目录服务器配置页面中有一个测试配置(Test Settings)按钮.这个功能将会帮助你分析你的用户管理在 Active Directory 和其他 LDAP 服务器中出现的问题. 希望对你 ...
- PHP之十六个魔术方法
1.__construct,__destruct__constuct构建对象的时被调用:__destruct明确销毁对象或脚本结束时被调用:2.__get,__set__set当给不可访问或不存在属性 ...