tensorflow中创建variable的2种方式
tf.Variable():只要使用该函数,一律创建新的variable,如果出现重名,变量名后面会自动加上后缀
var:0
var_1:0
var_2:0tf.get_variable():如果变量存在,则共享以前创建的变量,如果不存在,则新创建一个变量
1,变量存在,但无法共享

2,变量不存在,但无法创建

tensorflow中的两种作用域
命名域(name scope):通过tf.name_scope()来实现;
变量域(variable scope):通过tf.variable_scope()来实现;可以通过设置reuse 标志以及初始化方式来影响域下的变量。
这两种作用域都会给tf.Variable()创建的变量加上词头,但是name scope不会给tf.get_variable()加上词头所谓词头就是给变量名添加前缀
with tf.variable_scope('foo'):
with tf.variable_scope('bar'):
v = tf.get_variable('v',[1])
print(v.name)
assert v.name == 'foo/bar/v:0'变量域(variable scope)
- 不设置reuse,tf.get_variable()不可共享,也不可能创建
- reuse = True,可共享变量,但不可创建
- reuse = tf.AUTO_REUSE,此时若未存在同名变量,则创建,若存在,则共享变量
- 插入 scope.reuse_variables() :reuse设置为True,可共享,不可创建
with tf.variable_scope('foo'):
v = tf.Variable('v',[1])
with tf.variable_scope('foo',reuse = True):
v = tf.get_variable('v',[1])
with tf.variable_scope('foo'):
scope.reuse_variables()
v = tf.get_variable('v',[1])
with tf.variable_scope('foo',reuse = tf.AUTO_REUSE):
v = tf.get_variable('v',[1])
with tf.variable_scope('foo'):
v = tf.Variable('v',[1])
with tf.variable_scope('foo',reuse = True):
v = tf.Variable('v',[1])
with tf.variable_scope('foo'):
scope.reuse_variables()
v = tf.Variable('v',[1])
with tf.variable_scope('foo',reuse = tf.AUTO_REUSE):
v = tf.Variable('v',[1])
版权声明:本文为qq_24677259原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。