The following content is what i have quoted from
- "CSDN - adrianna_xy"
Original link - https://blog.csdn.net/u012223913/article/details/78533910
- tf.Variable()
W = tf.Variable(<initial-value>, name=<optional-name>)
用于生成一個(gè)初始值為initial-value的變量。必須指定初始化值
- tf.get_variable()
W = tf.get_variable(name, shape=None, dtype=tf.float32, initializer=None,
regularizer=None, trainable=True, collections=None)
獲取已存在的變量(要求不僅名字,而且初始化方法等各個(gè)參數(shù)都一樣),如果不存在,就新建一個(gè)。
可以用各種初始化方法,不用明確指定值。
- 區(qū)別
推薦使用tf.get_variable(), 因?yàn)椋?br> _初始化更方便
比如用xavier_initializer:
W = tf.get_variable("W", shape=[784, 256],
initializer=tf.contrib.layers.xavier_initializer())
_方便共享變量
因?yàn)閠f.get_variable() 會(huì)檢查當(dāng)前命名空間下是否存在同樣name的變量,可以方便共享變量。而tf.Variable 每次都會(huì)新建一個(gè)變量。
需要注意的是tf.get_variable() 要配合reuse和tf.variable_scope() 使用。
- 舉個(gè)栗子
4.1 首先介紹一下tf.variable_scope().
如果已經(jīng)存在的變量沒有設(shè)置為共享變量,TensorFlow 運(yùn)行到第二個(gè)擁有相同名字的變量的時(shí)候,就會(huì)報(bào)錯(cuò)。為了解決這個(gè)問題,TensorFlow 提出了 tf.variable_scope 函數(shù):它的主要作用是,在一個(gè)作用域 scope 內(nèi)共享一些變量,舉個(gè)簡單的栗子:
with tf.variable_scope("foo"):
v = tf.get_variable("v", [1]) #v.name == "foo/v:0"
簡單來說就是給變量名前再加了個(gè)變量空間名。
4.2 對(duì)比
接下來看看怎么用tf.get_variable()實(shí)現(xiàn)共享變量:
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]) #創(chuàng)建兩個(gè)名字一樣的變量會(huì)報(bào)錯(cuò) 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" 成功共享,因?yàn)樵O(shè)置了reuse
assert a==c #Assertion is true, they refer to the same object.
然后看看如果用tf.Variable() 會(huì)有什么效果:
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:
https://stackoverflow.com/questions/37098546/difference-between-variable-and-get-variable-in-tensorflow
https://www.tensorflow.org/versions/r1.2/api_docs/python/tf/variable_scope
http://blog.csdn.net/u013645510/article/details/53769689
I would recommend to use tf.get_Variable as it will make refactoring code easy and plus it doesn't create any problems when using multiple GPU. tf.gets_Variable sees or gets the variable ,if not found it would create it . We can specify the initializer such as xavier_initializer.
tf.Variable always creates variable. It requires an initial value .To know more about tf.Variable ,go to Getting started with TensorFlow
tf.Variable is kinda old ,lower level and tf.get_Variable is newer and better
[1] Information, syntax and example for tf.Variable
Footnotes
[1] Getting started with TensorFlow