TensorFlow入门(四) name / variable_scope 的使
name/variable_scope 的作用
欢迎转载,但请务必注明原文出处及作者信息。
@author: huangyongye
@creat_date: 2017-03-08
refer to: Sharing Variables
name / variable_scope 详细理解请看: TensorFlow入门(七) 充分理解 name / variable_scope
* 起因:在运行 RNN LSTM 实例代码的时候出现 ValueError。 *
在 TensorFlow 中,经常会看到这两个东东出现,这到底是什么鬼,是用来干嘛的。在做 LSTM 的时候遇到了下面的错误:
ValueError: Variable rnn/basic_lstm_cell/weights already exists, disallowed.
然后谷歌百度都查了一遍,结果也不知是咋回事。我是在 jupyter notebook 运行的示例程序,第一次运行的时候没错,然后就总是出现上面的错误。后来才知道是 get_variable() 和 variable_scope() 搞的鬼。
下面就来分析一下 TensorFlow 中到底用这来干啥。
import tensorflow as tf
# 设置GPU按需增长
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
1. 首先看看比较简单的 tf.name_scope(‘scope_name’).
tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理。
'''
Signature: tf.name_scope(*args, **kwds)
Docstring:
Returns a context manager for use when defining a Python op.
'''
# 也就是说,它的主要目的是为了更加方便地管理参数命名。
# 与 tf.Variable() 结合使用。简化了命名
with tf.name_scope('conv1') as scope:
weights1 = tf.Variable([1.0, 2.0], name='weights')
bias1 = tf.Variable([0.3], name='bias') # 下面是在另外一个命名空间来定义变量的
with tf.name_scope('conv2') as scope:
weights2 = tf.Variable([4.0, 2.0], name='weights')
bias2 = tf.Variable([0.33], name='bias') # 所以,实际上weights1 和 weights2 这两个引用名指向了不同的空间,不会冲突
print weights1.name
print weights2.name
conv1/weights:
conv2/weights:
# 注意,这里的 with 和 python 中其他的 with 是不一样的
# 执行完 with 里边的语句之后,这个 conv1/ 和 conv2/ 空间还是在内存中的。这时候如果再次执行上面的代码
# 就会再生成其他命名空间
with tf.name_scope('conv1') as scope:
weights1 = tf.Variable([1.0, 2.0], name='weights')
bias1 = tf.Variable([0.3], name='bias') with tf.name_scope('conv2') as scope:
weights2 = tf.Variable([4.0, 2.0], name='weights')
bias2 = tf.Variable([0.33], name='bias') print weights1.name
print weights2.name conv1_1/weights:
conv2_1/weights:
import tensorflow as tf
2.下面来看看 tf.variable_scope(‘scope_name’)
tf.variable_scope() 主要结合 tf.get_variable() 来使用,实现 变量共享。
# 这里是正确的打开方式~~~可以看出,name 参数才是对象的唯一标识
import tensorflow as tf
with tf.variable_scope('v_scope') as scope1:
Weights1 = tf.get_variable('Weights', shape=[,])
bias1 = tf.get_variable('bias', shape=[]) # 下面来共享上面已经定义好的变量
# note: 在下面的 scope 中的变量必须已经定义过了,才能设置 reuse=True,否则会报错
with tf.variable_scope('v_scope', reuse=True) as scope2:
Weights2 = tf.get_variable('Weights') print Weights1.name
print Weights2.name
# 可以看到这两个引用名称指向的是同一个内存对象
v_scope/Weights:
v_scope/Weights:
也可以结合 tf.Variable() 一块使用。
import tensorflow as tf
# 注意, bias1 的定义方式
with tf.variable_scope('v_scope') as scope1:
Weights1 = tf.get_variable('Weights', shape=[,])
# bias1 = tf.Variable([0.52], name='bias') # 下面来共享上面已经定义好的变量
# note: 在下面的 scope 中的get_variable()变量必须已经定义过了,才能设置 reuse=True,否则会报错
with tf.variable_scope('v_scope', reuse=True) as scope2:
Weights2 = tf.get_variable('Weights')
bias2 = tf.Variable([0.52], name='bias') print Weights1.name
print Weights2.name
print bias2.name
v_scope/Weights:
v_scope/Weights:
v_scope_1/bias:
如果 reuse=True 的scope中的变量没有已经定义,会报错!!
import tensorflow as tf
# 注意, bias1 的定义方式
with tf.variable_scope('v_scope') as scope1:
Weights1 = tf.get_variable('Weights', shape=[,])
bias1 = tf.Variable([0.52], name='bias') print Weights1.name
print bias1.name # 下面来共享上面已经定义好的变量
# note: 在下面的 scope 中的get_variable()变量必须已经定义过了,才能设置 reuse=True,否则会报错
with tf.variable_scope('v_scope', reuse=True) as scope2:
Weights2 = tf.get_variable('Weights')
bias2 = tf.get_variable('bias', []) # ‘bias print Weights2.name
print bias2.name # 这样子的话就会报错
# Variable v_scope/bias does not exist, or was not created with tf.get_variable()
v_scope/Weights:
v_scope/bias:
本文代码:https://github.com/yongyehuang/Tensorflow-Tutorial
TensorFlow入门(四) name / variable_scope 的使的更多相关文章
- (转)TensorFlow 入门
TensorFlow 入门 本文转自:http://www.jianshu.com/p/6766fbcd43b9 字数3303 阅读904 评论3 喜欢5 CS224d-Day 2: 在 Da ...
- TensorFlow入门(五)多层 LSTM 通俗易懂版
欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @creat_date: 2017-03-09 前言: 根据我本人学习 TensorFlow 实现 LSTM 的经 ...
- TensorFlow 入门 | iBooker·ApacheCN
原文:Getting Started with TensorFlow 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 不要担心自己的形象,只关心如何实现目标.--<原则>,生活原 ...
- #tensorflow入门(1)
tensorflow入门(1) 关于 TensorFlow TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操 ...
- 微服务(入门四):identityServer的简单使用(客户端授权)
IdentityServer简介(摘自Identity官网) IdentityServer是将符合规范的OpenID Connect和OAuth 2.0端点添加到任意ASP.NET核心应用程序的中间件 ...
- Spring Boot入门(四):开发Web Api接口常用注解总结
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...
- [译]TensorFlow入门
TensorFlow入门 张量(tensor) Tensorflow中的主要数据单元是张量(tensor), 一个张量包含了一组基本数据,可以是列多维数据.一个张量的"等级"(ra ...
- 转:TensorFlow入门(六) 双端 LSTM 实现序列标注(分词)
http://blog.csdn.net/Jerr__y/article/details/70471066 欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @cr ...
- TensorFlow入门之MNIST最佳实践
在上一篇<TensorFlow入门之MNIST样例代码分析>中,我们讲解了如果来用一个三层全连接网络实现手写数字识别.但是在实际运用中我们需要更有效率,更加灵活的代码.在TensorFlo ...
随机推荐
- python3学习笔记(9)_closure
#python 学习笔记 2017/07/13 # !/usr/bin/env python3 # -*- conding:utf-8 -*- #从高阶函数的定义,我们可以知道,把函数作为参数的函数, ...
- 2018/03/08 每日一学PHP 之 常量defind 和 const区别
常量defind 和 const区别 什么是常量? 如字面理解的,在脚本执行期间不可改变的的量. 定义一个常量应该注意的事项? 1:常量默认大小写敏感,错误的大小写不会被识别为常量. 2:常量只能是标 ...
- Qt Creator 4.3.0,Quick Designer里面也看以同时看到和编辑qml code了(Qt5.9的配套IDE)
作者:Summer Fang链接:https://www.zhihu.com/question/60486611/answer/177584284来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...
- Python开发【笔记】:关于子线程(子进程)与主线程(主进程)的关联
前言: 主要分析下面的问题: 主线程启线程 主线程执行完毕,会关闭子线程吗? 子线程启线程 主线程执行完毕,会结束吗? 主进程启动进程,主进程执行完毕,会怎样? 子进程启动进程,进程执行完毕,又会 ...
- oracle(十)临时表
1.临时表的特点 (1)多用户操作的独立性:对于使用同一张临时表的不同用户,oracle都会分配一个独立的 Temp Segment,这样就避免了多个用户在对同一张临时表操作时 发生交叉,从而保证了多 ...
- git使用简单指南
参考: https://www.fengerzh.com/git-reset/?utm_source=tool.lu git建库小结 (一)远端:1.在git网站上建设一个远程仓库复制git远程仓库地 ...
- php表单身份验证
1. index.php <form method="post" action="dbtest.php"> 姓名: ...
- MySQL多线程备份工具:mydumper
MySQL多线程备份工具:mydumper http://www.orczhou.com/index.php/2011/12/how-to-split-mysqldump-file/ Mydumper ...
- js-jquery-Validate校验【一】
一.导入 js 库 <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.j ...
- 十天精通CSS3(11)
Media Queries——媒体类型(一) 随着科学技术不断的向前发展,网页的浏览终端越来越多样化,用户可以通过:宽屏电视.台式电脑.笔记本电脑.平板电脑和智能手机来访问你的网站.尽管你无法保证一个 ...