文章均迁移到我的主页 http://zhenlianghe.com
my github: https://github.com/LynnHo
tf.name_scope(name, default_name=None, values=None)
- 域重名
- 当命名域重名的时候,tf.name_scope会自动对重名的域打上序号
with tf.name_scope('s'): a = tf.placeholder(dtype=tf.int64, name='a') with tf.name_scope('s'): b = tf.placeholder(dtype=tf.int64, name='b') print(a.name) print(b.name) [out] s/a:0 s_1/b:0
- 当命名域重名的时候,tf.name_scope会自动对重名的域打上序号
tf.variable_scope(name_or_scope, default_name=None, values=None, initializer=None, regularizer=None, caching_device=None, partitioner=None, custom_getter=None, reuse=None, dtype=None, use_resource=None)
- 这个函数会同时创建一个name_scope,即相当于先调用了一次tf.name_scope
- 域重名
- 当变量域重名的时候,官方文档 -> If name_or_scope is not None, it is used as is. If scope is None, then default_name is used. In that case, if the same name has been previously used in the same scope, it will made unique be appending _N to it.
- 如果给定了name_or_scope,那么两个重名的变量域对tf.get_variable生成的变量来说实际上是同一个域,而对于ops来说则是不同的命名域。可以理解为除了tf.get_variable生成的变量,其他ops均受到这个函数创建的tf.name_scope影响
with tf.variable_scope('s'): a = tf.get_variable(name='a', shape=(2)) c = tf.placeholder(dtype=tf.int64, name='c') with tf.variable_scope('s'): b = tf.get_variable(name='b', shape=(2)) d = tf.placeholder(dtype=tf.int64, name='d') print(a.name) print(b.name) print(c.name) print(d.name) [out] s/a:0 s/b:0 s/c:0 s_1/d:0
- 如果name_or_scope为None,那么tf将对重复的命名域打上序号,这个对一些函数式的layer很重要
with tf.variable_scope(None, 's'): a = tf.get_variable(name='a', shape=(2)) c = tf.placeholder(dtype=tf.int64, name='c') with tf.variable_scope(None, 's'): b = tf.get_variable(name='b', shape=(2)) d = tf.placeholder(dtype=tf.int64, name='d') print(a.name) print(b.name) print(c.name) print(d.name) [out] s/a:0 s_1/b:0 s/c:0 s_1/d:0
- 如果给定了name_or_scope,那么两个重名的变量域对tf.get_variable生成的变量来说实际上是同一个域,而对于ops来说则是不同的命名域。可以理解为除了tf.get_variable生成的变量,其他ops均受到这个函数创建的tf.name_scope影响
- 当变量域重名的时候,官方文档 -> If name_or_scope is not None, it is used as is. If scope is None, then default_name is used. In that case, if the same name has been previously used in the same scope, it will made unique be appending _N to it.