Deep learning和tensorflow学习记录(二十九):tf.name_scope和tf.variable_scope的用法

tensorflow中的variables和tensors有一个name属性,如果没有指定name,则tensorflow会自动赋一个name。

import tensorflow as tf

a = tf.constant(1)
print(a.name)

b = tf.Variable(1)
print(b.name)

输出:

Const:0
Variable:0

也可以显式的指定name。

a = tf.constant(1, name="a")
print(a.name)

b = tf.Variable(1, name="b")
print(b.name) 

输出:

a:0
b:0

tensorflow有两个context manager来更改tensors和variables的name,其中一个是tf.name_scope。

with tf.name_scope("scope"):
  a = tf.constant(1, name="a")
  print(a.name)

  b = tf.Variable(1, name="b")
  print(b.name)  

  c = tf.get_variable(name="c", shape=[])
  print(c.name)

输出:

scope/a:0
scope/b:0
c:0

两种方式创建variables,tf.Variable和tf.get_variable。用tf.get_variable指定一个name会创建一个新的variable,但如果指定的name已经存在的话,则会抛出ValueError exception,表示不允许重复声明变量。

tf.name_scope会影响用tf.Variable创建的tensors和variables,但不会影响tf.get_variable创建的variables。

与tf.name_scope不同的是,tf.variable_scope会改变 tf.get_variable创建的variables的name。

with tf.variable_scope("scope"):
  a = tf.constant(1, name="a")
  print(a.name)

  b = tf.Variable(1, name="b")
  print(b.name)

  c = tf.get_variable(name="c", shape=[])
  print(c.name)

输出:

scope/a:0
scope/b:0
scope/c:0

如果用 tf.get_variable重复声明变量就会报错。

with tf.variable_scope("scope"):
  a1 = tf.get_variable(name="a", shape=[])
  a2 = tf.get_variable(name="a", shape=[])

报错:

File "D:/code/tensorflow/test/test.py", line 17, in <module>
    a2 = tf.get_variable(name="a", shape=[]) 
  File "C:\ProgramData\Anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1317, in get_variable
    constraint=constraint)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1079, in get_variable
    constraint=constraint)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 425, in get_variable
    constraint=constraint)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 394, in _true_getter
    use_resource=use_resource, constraint=constraint)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 733, in _get_single_variable
    name, "".join(traceback.format_list(tb))))
ValueError: Variable scope/a already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

如果想重用变量,则需设置reuse属性。

with tf.variable_scope("scope"):
  a1 = tf.get_variable(name="a", shape=[])
  print(a1.name)
with tf.variable_scope("scope", reuse=True):
  a2 = tf.get_variable(name="a", shape=[])
  print(a2.name)

输出:

scope/a:0
scope/a:0

也可以设置tf.AUTO_REUSE告诉tensorflow如果变量不存在则创建一个新的变量,如果已经存在了则重用。

猜你喜欢

转载自blog.csdn.net/heiheiya/article/details/81085297