Notes on tensorflow(六)variable_scope
2017-04-28 13:33
417 查看
https://www.tensorflow.org/programmers_guide/variable_scope
一个scope对应一个namespace,当在scope里创建任意有name的东西时, 它的name为:
Scope与Share机制
tensorflow 引入了namespace机制, 也就是scope, 可以方便地命名、共享变量. 当需要共享变量时, 创建变量使用tf.get_variable方法而不是
tf.Variable.
import tensorflow as tf with tf.variable_scope('foo'): v1 = tf.get_variable('v1', [1]) print v1.name with tf.variable_scope('foo', reuse = True): v2 = tf.get_variable('v1') #v3 = tf.get_variable('v3', [3]) 会报错 print v2.name assert v2 is v1
foo/v1:0 foo/v1:0
一个scope对应一个namespace,当在scope里创建任意有name的东西时, 它的name为:
scope_name/var_name
reuse = True不可少。它是
variable_scope的一个属性, 直接决定如何创建变量。
reuse = False时, 先检查是否已经存在相同name的Variable, 如果有, 报错。然后以对应name创建一个新的Variable
reuse = True时,不会创建新的Variable。直接查找自己name对应的variable, 如果没有, 则报错。
reuse属性可继承:在
reuse = True的scope里创建子scope时, 子scope的
reuse==True
import tensorflow as tf with tf.variable_scope('foo', reuse = True) as foo: print foo.reuse with tf.variable_scope('doo') as doo: print doo.reuse
True True
variable_scope与name_scope
它们在效果上的区别是variable_scope会影响它内部创建的所有有name属性的节点, 但
name_scope只影响Operator节点的命名。 用处之一是在多gpu训练时在不同的device上, 使用相同的
variable_scope, 但使用不同的
namescope
import tensorflow as tf with tf.variable_scope('foo'): with tf.name_scope('ns'): a = tf.get_variable('a', [1]) b = a + 1; print a.name print b.name print b.op.name
foo/a:0 foo_2/ns/add:0 foo_2/ns/add
为variable_scope指定默认的initializer
为variable_scope指定默认initializer的好处是不用在每次调用创建变量的方法时传入初始值了。它也是可以继承的。import tensorflow as tf def show(v): with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) print v.eval() with tf.variable_scope('foo', initializer = tf.constant_initializer(0.2)): cv1 = tf.get_variable('cv1', [1]) show(cv1) with tf.variable_scope('sub_foo'): cv2 = tf.get_variable('cv2', [1]) show(cv2) with tf.variable_scope('sub_sub_foo', initializer = tf.constant_initializer(0.1)): cv3 = tf.get_variable('cv3', [1]) show(cv3)
[ 0.2] [ 0.2] [ 0.1]
相关文章推荐
- Notes on learning tensorflow
- What's the difference of name scope and a variable scope in tensorflow?
- Notes on tensorflow(一) Framework Overview
- Notes on tensorflow(五)Tensor Ranks, Shapes, and Types
- Notes on tensorflow(八)read tfrecords with slim
- tensorflow variable_scope\name_scope
- 转载!tensorflow name scope和variable scope
- tensorflow scope命名方法(variable_scope()与name_scope()解析)
- tensorflow variable_scope共享变量
- What's the difference of name scope and a variable scope in tensorflow?
- Notes on Tensorflow
- ValueError: Attempt to reuse RNNCell <tensorflow.contrib.rnn.python.ops.core_rnn_cell_impl.BasicLSTMCell object at 0x7f1a3c448390> with a different variable scope than its first use.解决方法
- tensorflow name_scope与variable_scope
- tensorflow variable scope 变量命名空间和变量共享
- tensorflow variable_scope,tf.name_scope, tf.variable, tf.get_varible
- Notes on tensorflow(二)Get started
- Notes on Tensorflow(四)Variables
- Notes on tensorflow(七)将数据集转换为TFRecord
- Install Tensorflow on windows(Anaconda)
- TensorflowOnSpark 安装