【tensorflow 学习】tf.get_variable()和tf.Variable()的区别
来源:互联网 发布:系统交互流程图软件 编辑:程序博客网 时间:2024/06/01 08:58
1. tf.Variable()
W = tf.Variable(<initial-value>, name=<optional-name>)
用于生成一个初始值为initial-value
的变量。必须指定初始化值
2.tf.get_variable()
W = tf.get_variable(name, shape=None, dtype=tf.float32, initializer=None, regularizer=None, trainable=True, collections=None)
获取已存在的变量(要求不仅名字,而且初始化方法等各个参数都一样),如果不存在,就新建一个。
可以用各种初始化方法,不用明确指定值。
3.区别
推荐使用tf.get_variable()
, 因为:
1. 初始化更方便
比如用xavier_initializer:
W = tf.get_variable("W", shape=[784, 256], initializer=tf.contrib.layers.xavier_initializer())
2. 方便共享变量
因为tf.get_variable()
会检查当前命名空间下是否存在同样name的变量,可以方便共享变量。而tf.Variable
每次都会新建一个变量。
需要注意的是tf.get_variable()
要配合reuse
和tf.variable_scope()
使用。
4. 举个栗子
4.1 首先介绍一下tf.variable_scope()
.
如果已经存在的变量没有设置为共享变量,TensorFlow 运行到第二个拥有相同名字的变量的时候,就会报错。为了解决这个问题,TensorFlow 提出了 tf.variable_scope 函数:它的主要作用是,在一个作用域 scope 内共享一些变量,举个简单的栗子:
with tf.variable_scope("foo"): v = tf.get_variable("v", [1]) #v.name == "foo/v:0"
简单来说就是给变量名前再加了个变量空间名。
4.2 对比
接下来看看怎么用tf.get_variable()
实现共享变量:
with tf.variable_scope("one"): a = tf.get_variable("v", [1]) #a.name == "one/v:0"with tf.variable_scope("one"): b = tf.get_variable("v", [1]) #创建两个名字一样的变量会报错 ValueError: Variable one/v already exists with tf.variable_scope("one", reuse = True): #注意reuse的作用。 c = tf.get_variable("v", [1]) #c.name == "one/v:0" 成功共享,因为设置了reuseassert a==c #Assertion is true, they refer to the same object.
然后看看如果用tf.Variable()
会有什么效果:
with tf.variable_scope("two"): d = tf.get_variable("v", [1]) #d.name == "two/v:0" e = tf.Variable(1, name = "v", expected_shape = [1]) #e.name == "two/v_1:0" assert d==e #AssertionError: they are different objects
可以看到,同样的命名空间(‘two’)和名字(v),但是d和e的值却不一样。
Reference:
1. https://stackoverflow.com/questions/37098546/difference-between-variable-and-get-variable-in-tensorflow
2. https://www.tensorflow.org/versions/r1.2/api_docs/python/tf/variable_scope
3. http://blog.csdn.net/u013645510/article/details/53769689
- 【tensorflow 学习】tf.get_variable()和tf.Variable()的区别
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别:
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- 【tensorflow】scope的使用以及tf.Variable()和tf.get_variable()的区别
- tensorflow共享变量, tf.get_variable() 和 tf.Varibele()的区别
- TensorFlow tf.Variable()与tf.get_variable()的简单介绍
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- tf.Variable()和tf.get_variable()的使用方法和相同点
- Tensorflow中tf.get_variable和tf.variable_scope的使用
- tensorflow学习之路(2-1):tf.variable_scope(), tf.name_scope(), tf.get_variable()的认识
- tensorflow学习之路(2-2):tf.variable_scope(),tf.name_scope(),tf.get_variable()的认识(补充)
- TF中的tf.Variable 和 tf.placehold 的区别
- TF中的tf.Variable 和 tf.placehold 的区别
- #tensorflow学习笔记#理解tf.get_variable
- TensorFlow学习--tf.get_variable使用
- tensorflow 变量生成 变量管理 tf.Variable & tf.get_variable & tf.variable_scope
- tf.get_variable 和tf.variable_scope
- 用C++实现一个LRU cache
- 88. Merge Sorted Array
- redis基本操作
- Codeforces 888A Local Extrema
- JDK源码中使用的设计模式-总结
- 【tensorflow 学习】tf.get_variable()和tf.Variable()的区别
- 辗转相除法求两个整数的最大公约数
- 二叉树的建立,前序,中序,后序遍历
- Android开发中的代码混淆
- nuc972的ramfs的配置yaffs2,ubi文件系统
- muduo库源码学习(base):Logfile
- 集合类操作优化经验总结
- 改善程序员生活质量的 3+10 习惯
- HDU1754 I hate it 线段树 最大值