Tensorflow学习(二) name_scope和variable_scope
2018-03-19 07:24
543 查看
tf.name_scope()创建名称作用域,可以影响tf.Variable和tf.Operation,主要用于对节点名称命名,方便Tensorboard记录和分类节点创建名称作用域,可以影响tf.Variable和tf.Operationwith tf.name_scope('foo') as foo_scope:
a = tf.Variable(1, name='a')
assert a.name == 'foo/a:0'
with tf.name_scope('bar') as bar_scope:
b = tf.Variable(2, name='b')
c = a * b
assert b.name == 'foo/bar/b:0'
assert c.op.name == 'foo/bar/mul'注:a.name是Tensor的name属性,name中‘:’后是Tensor的value_index属性,value_index表示该Tensor被产生它的操作的第几个输出,如#value是一个[5, 30]的矩阵
split0, split1, split2 = tf.split(value, [4, 15, 11], 1)则split0,split1,split2的value_index分别为0、1、2。
tf.variable_scope()用于创建变量作用域,可以通过tf.fet_variable()创建变量,tf.variable_scope()只影响由tf.get_variable()创建的变量,而不会影响tf.Variable()创建的变量,tf.get_variable()也可从内存中取出已有变量,tf.variable_scope()主要应用于需要大规模共享变量的场景,如RNN。tf.variable_scope().reuse默认为False,此时不可重复使用此变量域中的变量with tf.variable_scope('var_scope', reuse=tf.AUTO_REUSE) as var_scope
v1 = tf.get_variable('v', [1])
v2 = tf.get_variable('v', [1])
assert v1.name =='var_scope/v:0')
assert v1 == v2
#id(var)查询变量在内存中的位置,发现v1、v2一样,是因为v1、v2同时调用内存中同一变量
print(id(v1), id(v2))
a = tf.Variable(1, name='a')
assert a.name == 'foo/a:0'
with tf.name_scope('bar') as bar_scope:
b = tf.Variable(2, name='b')
c = a * b
assert b.name == 'foo/bar/b:0'
assert c.op.name == 'foo/bar/mul'注:a.name是Tensor的name属性,name中‘:’后是Tensor的value_index属性,value_index表示该Tensor被产生它的操作的第几个输出,如#value是一个[5, 30]的矩阵
split0, split1, split2 = tf.split(value, [4, 15, 11], 1)则split0,split1,split2的value_index分别为0、1、2。
tf.variable_scope()用于创建变量作用域,可以通过tf.fet_variable()创建变量,tf.variable_scope()只影响由tf.get_variable()创建的变量,而不会影响tf.Variable()创建的变量,tf.get_variable()也可从内存中取出已有变量,tf.variable_scope()主要应用于需要大规模共享变量的场景,如RNN。tf.variable_scope().reuse默认为False,此时不可重复使用此变量域中的变量with tf.variable_scope('var_scope', reuse=tf.AUTO_REUSE) as var_scope
v1 = tf.get_variable('v', [1])
v2 = tf.get_variable('v', [1])
assert v1.name =='var_scope/v:0')
assert v1 == v2
#id(var)查询变量在内存中的位置,发现v1、v2一样,是因为v1、v2同时调用内存中同一变量
print(id(v1), id(v2))
相关文章推荐
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- 关于tensorflow的变量(variable) 变量作用域(variable_scope) 名字作用域(name_scope)的学习记录
- TensorFlow学习笔记(1):variable与get_variable, name_scope()和variable_scope()
- TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- 2. tensorflow学习之name/variable_scope 的使用
- tensorflow里面共享变量、name_scope, variable_scope等如何理解
- tensorflow 中 variable_scope 与name_scope函数解析
- Tensorflow函数说明(4)—— variable_scope/name_scope
- tensorflow:上下文管理器 与 name_scope, variable_scope
- TensorFlow中,variable_scope和name_scope的不同之处
- tensorflow name_scope && variable_scope
- tensorflow学习笔记: variable scope
- 【Tensorflow】name_scope() 和 variable_scope() 区别
- TensorFlow:作用域name_scope和variable_scope
- Tensorflow-variable_scope,name_scope
- Tensorflow name_scope和variable_scope的区别
- tensorflow 中 name_scope和variable_scope
- tensorflow 中 name_scope 及 variable_scope 的异同
- TensorFlow 充分理解 name / variable_scope