tf.get_variable()
2017-03-25 22:23
225 查看
tf.get_variable()通常和tf.variable_scope()一起使用,可用来共享变量
tf.get_variable(name, shape, initializer): 通过所给的名字创建或是返回一个变量.
tf.variable_scope(): 通过 tf.get_variable()为变量名指定命名空间.
函数tf.get_variable() 用来获取或创建一个变量,不同于tf.Variable那样直接传值,tf.get_variable()用initializer来创建变量。一个initializer 就是一个函数,创建一个形状是shape的张量。TensorFlow中使用的initializer有以下几种 :
tf.constant_initializer(value) 用value初始化所有的值。initializes everything to the provided value,
tf.random_uniform_initializer(a, b) 从a到b均匀初始化,
tf.random_normal_initializer(mean, stddev) 用所给平均值和标准差初始化均匀分布
情况1:当
这种情况下,v将通过tf.Variable所提供的形状和数据类型来重新创建。创建变量的全称将会由当前变量作用域名+所提供的名字所组成,并且还会检查来确保没有任何变量使用这个全称。如果这个全称已经有一个变量使用了,那么方法将会抛出ValueError错误。如果一个变量被创建,用initializer(shape)进行初始化。比如:
情况2:当
这种情况下,调用就会搜索一个已经存在的变量,变量的全称和当前变量的作用域名+所提供的名字是否相等。如果不存在相应的变量,就会抛出ValueError 错误。如果变量找到了,就返回这个变量。如下:
tf.get_variable(name, shape, initializer): 通过所给的名字创建或是返回一个变量.
tf.variable_scope(): 通过 tf.get_variable()为变量名指定命名空间.
函数tf.get_variable() 用来获取或创建一个变量,不同于tf.Variable那样直接传值,tf.get_variable()用initializer来创建变量。一个initializer 就是一个函数,创建一个形状是shape的张量。TensorFlow中使用的initializer有以下几种 :
tf.constant_initializer(value) 用value初始化所有的值。initializes everything to the provided value,
tf.random_uniform_initializer(a, b) 从a到b均匀初始化,
tf.random_normal_initializer(mean, stddev) 用所给平均值和标准差初始化均匀分布
v = tf.get_variable(name, shape, dtype, initializer)
情况1:当
tf.get_variable_scope().reuse == False时,作用域就是为创建新变量所设置的。
这种情况下,v将通过tf.Variable所提供的形状和数据类型来重新创建。创建变量的全称将会由当前变量作用域名+所提供的名字所组成,并且还会检查来确保没有任何变量使用这个全称。如果这个全称已经有一个变量使用了,那么方法将会抛出ValueError错误。如果一个变量被创建,用initializer(shape)进行初始化。比如:
with tf.variable_scope("foo"): v = tf.get_variable("v", [1]) assert v.name == "foo/v:0"
情况2:当
tf.get_variable_scope().reuse == True时,作用域是为重用变量所设置的。
这种情况下,调用就会搜索一个已经存在的变量,变量的全称和当前变量的作用域名+所提供的名字是否相等。如果不存在相应的变量,就会抛出ValueError 错误。如果变量找到了,就返回这个变量。如下:
with tf.variable_scope("foo"): v = tf.get_variable("v", [1]) with tf.variable_scope("foo", reuse=True): v1 = tf.get_variable("v", [1]) assert v1 == v
相关文章推荐
- tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- tensorflow学习之路(2-2):tf.variable_scope(),tf.name_scope(),tf.get_variable()的认识(补充)
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- tensorflow API: tf.get_variable
- tf.get_variable 和tf.variable_scope
- #tensorflow学习笔记#理解tf.get_variable
- tf.Variable()与tf.get_variable()与不同之处
- 【tensorflow 学习】tf.get_variable()和tf.Variable()的区别
- tf.get_variable 和tf.variable_scope
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- tf.Variable()与tf.get_variable()对比
- tf.variable_scope()和tf.name_scope(),tf.Variable()和tf.get_variable()
- 图变量作用域tf.get_variable
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- 彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同
- TensorFlow--tf.get_variable
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- tensorflow中使用tf.variable_scope和tf.get_variable的ValueError
- tf.get_variable tf.variable_scope tf.name_scope