variable_scope和name_scope差别
先看代码:
- #命名空间函数tf.variable_scope()和tf.name_scope()函数区别于使用
- import tensorflow as tf
- with tf.variable_scope("foo"):
- a = tf.get_variable("bar", [1])
- print(a.name) #foo/bar:0
- b = tf.Variable("b", [1])
- print(b.name) #foo/Variable:0
- with tf.variable_scope("bar"):
- a = tf.get_variable("bar", [1])
- print(a.name) #bar/bar:0
- b = tf.Variable("b", [1])
- print(b.name) #bar/Variable:0
- with tf.name_scope("a"):
- a = tf.Variable([1])
- print(a.name) #a/Variable:0
- with tf.name_scope("b"):
- b = tf.get_variable("b", [1])
- print(b.name) #b_1:0
- with tf.name_scope("b"):
- c = tf.get_variable("b", [1])
- print(c.name) #出错,b已存在
可以看出,对于tf.Variable()函数,两者的使用情况都一样;而tf.get_variable()函数,它不受name_scope约束,已经声明过的变量就不能再声明了。
1. tf.name_scope('scope_name')或tf.name_scope(named_scope)
主要与tf.Variable搭配使用;
当传入字符串时,用以给变量名添加前缀,类似于目录,如case1所示;
当传入已存在的name_scope对象时,则其范围内变量的前缀只与当前传入的对象有关,与更上层的name_scope无关,如case2所示。
- import tensorflow as tf
- # case 1:
- with tf.name_scope('l1'):
- with tf.name_scope('l2'):
- wgt1 = tf.Variable([1,2,3], name='wgts')
- bias1 = tf.Variable([0.1], name='biases')
- print wgt1.name, bias1.name
- # >>> l1/l2/wgts:0 l1/l2/biases:0
- # case 2:
- with tf.name_scope('l1') as l1_scp:
- with tf.name_scope('l2'):
- wgt0 = tf.Variable([1,2,3], name='wgts')
- bias0 = tf.Variable([0.1], name='biases')
- with tf.name_scope(l1_scp):
- wgt1 = tf.Variable([1,2,3], name='wgts')
- bias1 = tf.Variable([0.1], name='biases')
- print wgt0.name, bias0.name, wgt1.name, bias1.name
- # >>> l1_1/l2/wgts:0 l1_1/l2/biases:0 l1_1/wgts:0 l1_1/biases:0
2. tf.variable_scope('scope_name', reuse=None)或
tf.variable_scope(named_scope)
与name_scope一样:当传入字符串时,用以给变量名添加前缀,类似于目录;
当传入已存在的variable_scope对象时,则其范围内变量的前缀只与当前传入的对象有关,与更上层的variable_scope无关。
常于get_variable搭配使用,多用于变量共享;其中 reuse 参数可设为 None、tf.AUTO_REUSE、True、False;
当 reuse=None(默认情况)时,与上层variable_scope的reuse参数一样。
- # case 1
- with tf.variable_scope('lv1'):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
- print wgt1.name, bias1.name
- # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
当 reuse=tf.AUTO_REUSE 时,自动复用,如果变量存在则复用,不存在则创建。这是最安全的用法。
- with tf.variable_scope('lv1'):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
- print wgt1.name, bias1.name
- # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
- with tf.variable_scope('lv1', reuse=tf.AUTO_REUSE):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt2 = tf.get_variable('wgts', [2,2])
- bias2 = tf.get_variable('biases', [2,2])
- print wgt2.name, bias2.name
- # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
- with tf.variable_scope('lv1', reuse=tf.AUTO_REUSE):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt2 = tf.get_variable('wgts', [2,2])
- bias2 = tf.get_variable('biases', [2,2])
- print wgt2.name, bias2.name
- # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
当 reuse=True 时,tf.get_variable会查找该命名变量,如果没有找到,则会报错;所以设置reuse=True之前,要保证该命名变量已存在。
- with tf.variable_scope('lv1', reuse=True):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
- print wgt1.name, bias1.name
- # >>> ValueError: Variable lv1/lv2/wgts does not exist,
- # or was not created with tf.get_variable(). Did you mean
- # to set reuse=tf.AUTO_REUSE in VarScope?
命名变量已存在:
- with tf.variable_scope('lv1'):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
- print wgt1.name, bias1.name
- # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
- # case 2
- with tf.variable_scope('lv1', reuse=True):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
- print wgt1.name, bias1.name
- # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
当 reuse=False 时,tf.get_variable会调用tf.Variable来创建变量,并检查创建的变量是否以存在,如果已存在,则报错;
- with tf.variable_scope('lv1'):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
- print wgt1.name, bias1.name
- # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
- # case 2
- with tf.variable_scope('lv1', reuse=False):
- with tf.variable_scope('lv2'):
- init = tf.constant_initializer(0.1)
- wgt1 = tf.get_variable('wgts', [2,2])
- bias1 = tf.get_variable('biases', [2,2])
- print wgt1.name, bias1.name
- # ValueError: Variable lv1/lv2/wgts already exists, disallowed.
- # Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
variable_scope和name_scope差别的更多相关文章
- Tensorflow中的name_scope和variable_scope
Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行 ...
- tensorflow里面共享变量、name_scope, variable_scope等如何理解
tensorflow里面共享变量.name_scope, variable_scope等如何理解 name_scope, variable_scope目的:1 减少训练参数的个数. 2 区别同名变量 ...
- tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别
在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...
- tensorflow中的name_scope, variable_scope
在训练深度网络时,为了减少需要训练参数的个数(比如LSTM模型),或者是多机多卡并行化训练大数据.大模型等情况时,往往就需要共享变量.另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变 ...
- 通俗理解tf.name_scope()、tf.variable_scope()
前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记:tf.name_scope( ...
- Tensorflow 之 name/variable_scope 变量管理
name/variable_scope 的作用 充分理解 name / variable_scope TensorFlow 入门笔记 当一个神经网络比较复杂.参数比较多时,就比较需要一个比较好的方式来 ...
- Tensorflow常用函数说明(一)
首先最开始应该清楚一个知识,最外面的那个[ [ [ ]]]括号代表第一维,对应维度数字0,第二个对应1,多维时最后一个对应数字-1:因为后面有用到 1 矩阵变换 tf.shape(Tensor) 返回 ...
- [TensorBoard] Name & Variable scope
TF有两个scope, 一个是name_scope一个是variable_scope 第一个程序: with tf.name_scope("hello") as name_scop ...
- 2.4scope
name_scope variable_scope scope (name_scope/variable_scope) from __future__ import print_function im ...
随机推荐
- .NET开源了,Visual Studio开始支持 Android 和 iOS 编程并自带Android模拟器
北京时间今天凌晨的大会上,多少程序员的假想成为现实..NET 开源,集成 Clang 和 LLVM 并且自带 Android 模拟器,这意味着 Visual Studio 这个当下最好没有之一的 ID ...
- 两次被百度k站两次恢复的亲身经历
最后,网站被k了并不是最重要的,最重要的是你能不能找到网站被k的病因所在,不知道病因的话,就盲目的优化,恢复起来可能效果就不太明显,也比较浪费时间.只有找到了被k的原因,这样恢复起来才会得心应手,省时 ...
- Lucene 3.0 输出相似度
http://www.cnblogs.com/ibook360/archive/2011/10/19/2217638.html Lucene3.0之结果排序(原理篇) 传统上,人们将信息检索系统返回结 ...
- LESS碎语
推荐在Brackets安装"LESS AutoCompile"插件,当保存less文件会自动生成或保存相应的css文件. 变量 以@开头声明变量,并且对变量进行分类,比如颜色变量. ...
- windows phone 基础
一.安装 建议安装Windows 7环境,XP中不能运行模拟器,Vista系统支持,但不解释.系统安装完后,直接去微软网站在线安装即可,非常方便,美中不足的是如果你的网速不快,那可能要折磨你半天,快得 ...
- CentOS RabbitMQ 高可用(Mirrored)
原文:https://www.sunjianhua.cn/archives/centos-rabbitmq.html 一.RabbitMQ 单节点 1.1.Windows 版安装配置 1.1.1 安装 ...
- Nginx中文手冊
下载 : Nginx 中文手冊 Nginx 常见应用技术指南[Nginx Tips] 第二版 作者:NetSeek http://www.linuxtone.org (IT运维专家网|集群架构|性能调 ...
- ios成长之每日一遍(day 1)
Hello world开始. 这里不讨论如何创建项目导入项目.由于趁上班时间打酱油所以也不谈细节, 只谈具体项目的实现与关键流程的解析, 只供本人实际程况使用.不喜请移驾. 首先来谈谈 AppDele ...
- linux find 10天内改动过的文件
find . -name "*.h" -mtime -10 -type f -print find . -regex ".*\.\(c\|h\)" -mtime ...
- android BitmapDrawable的使用
<span style="font-size:18px;"> //功能:显示缩略图,大小为40*40 //通过openRawResource获取一个inputStrea ...