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( ...
随机推荐
- jquery源码解析
//局部作用域,外部引用不到这个闭合函数里面的东西,这时候需要用提供的对外访问接口来访问里面的变量 (function(){ ; function $() { alert(a) } window.$ ...
- Windows下return,exit和ExitProcess的区别和分析
通常,我们为了使自己的程序结束,会在主函数中使用return或调用exit().在windows下还有ExitProcess()和TerminateProcess()等函数. 本文的目的是比较以上几种 ...
- NO-CARRIER
自己动手写了创建虚拟接口,删除虚拟接口程序,频繁调用创建删除时,有时将接口up起来时会报错: Name not unique on network 利用ip link命令来查看接口(及其对应的索引) ...
- shell脚本学习系列之一---入门
参考:http://me.52fhy.com/shell-book/ 待后续整理...
- Vuex与axios介绍
Vuex集中式状态管理里架构 axios (Ajax) Vuex集中式状态管理架构 -简单介绍: vuex是一个专门为Vue.js设计的集中式状态管理架构. 我们把它理解为在data中需要共享给其他组 ...
- 关于socket.io获取客户端真实IP地址
1 前言 由于使用了CDN加速,导致了socket.handshake.address拿到值都是服务器的,而没有使用CDN加速时,可以拿到客户端真实IP. 2 代码 if(socket.handsha ...
- 安装mysql5.7与创建用户和远程登录授权
环境:ubuntu18.04 参考文章:安装并远程登录授权:https://www.cnblogs.com/chancy/p/9444187.html 用户管理:https://www.cnblogs ...
- bootstrap的treeview使用方法
首先引入文件: <link href="./css/bootstrap.css" rel="stylesheet"> <script src= ...
- Python-多表关联 外键 级联
分表为什么分表 多表关联多表关系 ****** 表之间的关系 为什么要分表 多对一 一个外键 多对多 一个中间表 两个外键 一对一 一个外键加一个唯一约束外键约束 ****** foreign key ...
- js——this
每个函数的this是在调用时绑定的,完全取决于函数的调用位置 1. 绑定规则总结 一般情况下,按下列顺序从下至上来判断this的绑定对象(绑定的优先级从下至上递减) 默认:在严格模式下绑定到undef ...