TensorFlow入门(四) name / variable_scope 的使用
来源:互联网 发布:世界黑客编程大赛题目 编辑:程序博客网 时间:2024/06/05 03:21
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 = Truesess = 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.nameprint weights2.name
conv1/weights:0conv2/weights:0
# 注意,这里的 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.nameprint weights2.name
conv1_1/weights:0conv2_1/weights:0
import tensorflow as tf
2.下面来看看 tf.variable_scope(‘scope_name’)
tf.variable_scope() 主要结合 tf.get_variable() 来使用,实现 变量共享。
# 这里是正确的打开方式~~~可以看出,name 参数才是对象的唯一标识import tensorflow as tfwith tf.variable_scope('v_scope') as scope1: Weights1 = tf.get_variable('Weights', shape=[2,3]) bias1 = tf.get_variable('bias', shape=[3])# 下面来共享上面已经定义好的变量# note: 在下面的 scope 中的变量必须已经定义过了,才能设置 reuse=True,否则会报错with tf.variable_scope('v_scope', reuse=True) as scope2: Weights2 = tf.get_variable('Weights')print Weights1.nameprint Weights2.name# 可以看到这两个引用名称指向的是同一个内存对象
v_scope/Weights:0v_scope/Weights:0
也可以结合 tf.Variable() 一块使用。
import tensorflow as tf# 注意, bias1 的定义方式with tf.variable_scope('v_scope') as scope1: Weights1 = tf.get_variable('Weights', shape=[2,3])# 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.nameprint Weights2.nameprint bias2.name
v_scope/Weights:0v_scope/Weights:0v_scope_1/bias:0
如果 reuse=True 的scope中的变量没有已经定义,会报错!!
import tensorflow as tf# 注意, bias1 的定义方式with tf.variable_scope('v_scope') as scope1: Weights1 = tf.get_variable('Weights', shape=[2,3]) bias1 = tf.Variable([0.52], name='bias')print Weights1.nameprint 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', [1]) # ‘biasprint Weights2.nameprint bias2.name# 这样子的话就会报错# Variable v_scope/bias does not exist, or was not created with tf.get_variable()
v_scope/Weights:0v_scope/bias:0
本文代码:https://github.com/yongyehuang/Tensorflow-Tutorial
- TensorFlow入门(四) name / variable_scope 的使用
- TensorFlow入门(七) 充分理解 name / variable_scope
- 2. tensorflow学习之name/variable_scope 的使用
- TensorFlow 充分理解 name / variable_scope
- Tensorflow中tf.get_variable和tf.variable_scope的使用
- tensorflow 中 name_scope 及 variable_scope 的异同
- Tensorflow name_scope和variable_scope的区别
- TensorFlow中,variable_scope和name_scope的不同之处
- tensorflow: name_scope 和 variable_scope的差别
- Notes on tensorflow(六)variable_scope
- Tensorflow函数——tf.variable_scope()
- tensorflow学习之路(2-2):tf.variable_scope(),tf.name_scope(),tf.get_variable()的认识(补充)
- 【TensorFlow】变量管理tf.get_variables/tf.Variable/tf.variable_scope的应用(七)
- Tensorflow-variable_scope,name_scope
- tensorflow name_scope variable_scope
- tensorflow variable_scope\name_scope
- tensorflow variable_scope共享变量
- tensorflow name_scope && variable_scope
- java网络编程____tcp协议原理
- Android App优化之提升你的App启动速度之实例挑战
- 前端小技巧总结
- maven项目中resource包中的配置文件load失败问题
- C语言的函数重载与可变参数函数--variable argument in function
- TensorFlow入门(四) name / variable_scope 的使用
- thinkphp生成项目
- Myeclipese建立servelet时出现带有javax.servlet.http.HttpServlet;变成了红色的解决方法
- fork与clone的区别
- flume+kafka+spark streaming日志流式处理系统搭建实验
- 数据库学习
- MacBook中获取高德地图的发布版和开发版的SHA1的值
- 高效使用Bitmaps(一) 大Bitmap的加载
- java网络编程____UDP协议原理