一 tf.name_scope()
用于定义python op的上下文管理器;
__init__(
name,
default_name=None,
values=None
)
- name:此scope的名字;
- default_name:name没有预设值时使用此默认名字
- values:传递给name_scopesd 变量列表,可在上下文区域对这些变量操作
二 tf.variable_scope()
__init__(
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,
constraint=None,
auxiliary_name_scope=True
)
- name_or_scope:设置variable_scope的名字或者是打开一个已经存在的scope
- default_name:如果没有提供name_or_scope,则使用的默认值
- values:提供给variable_scope的变量列表
- initializer:在此上下文范围内变量的默认初始化器;
- regularizer:在此上下文范围变量的默认正则化;
- reuse:是否重用,可设置为tf.AUTO_REUSE,如果变量不存在则创建一个新变量,存在就返回它;
三 tf.name_scope()和tf.variable_scope()的区别
(1)当设置的scope名字与之前的scope相同时
- tf.name_scope()会自动在name加后缀_N来区分两个scope
- tf.variable_scope() 会打开之前的scope
(2)在作用域区域对tf.get_variable()的处理
- tf.name_scope()会给除了tf.get_variable()的其他所有操作加上前缀scope的名字
- tf.variable_scope()会给所有的变量加上前缀scope的名字
(3)tf.variable_scope()专注于变量;
- tf.variable_scope()专用于变量的创建或者复用,可为作用域内的变量设置一些默认值,如默认的初始化操作,默认的正则项;
- 而 tf.name_scope()不可为上下文作用域设置默认值;只能为作用域的操作加前缀;