一个上下文管理器,可将操作从 control-flow 范围和 function-building 图表中提取出来。
用法
@tf_contextlib.contextmanager
tf.init_scope()
抛出
-
RuntimeError
如果图形状态与此初始化不兼容。
通常需要将变量初始化操作从control-flow 范围、function-building 图形和渐变磁带中提升出来。输入init_scope
是满足这些需求的一种机制。特别是,输入 init_scope
具有三个效果:
(1)在进入范围的那一刻清除所有的控制依赖;这等效于进入从 control_dependencies(None)
返回的上下文管理器,它具有退出 control-flow 作用域(如 tf.cond
和 tf.while_loop
)的副作用。
(2) 在作用域处于活动状态时创建的所有操作都被提升到context_stack
上未构建图形函数的最低上下文中。在这里,上下文被定义为图形或即刻的上下文。每个上下文切换,即作为默认图的每个图的安装和每个切换到渴望模式的切换,都记录在名为 context_switches
的线程本地堆栈中;当上下文退出时,上下文切换的日志条目会从堆栈中弹出。输入 init_scope
相当于向上爬 context_switches
,找到第一个没有构建图函数的上下文,然后输入它。需要注意的是,如果启用了图形模式但默认图形堆栈为空,则输入 init_scope
将简单地安装一个新图形作为默认图形。
(3) 示波器处于活动状态时,梯度磁带暂停。
启用即刻执行后,即使在跟踪 tf.function
时,init_scope 块内的代码也会在启用即刻执行的情况下运行。例如:
tf.compat.v1.enable_eager_execution()
@tf.function
def func():
# A function constructs TensorFlow graphs,
# it does not execute eagerly.
assert not tf.executing_eagerly()
with tf.init_scope():
# Initialization runs with eager execution enabled
assert tf.executing_eagerly()
相关用法
- Python tf.inside_function用法及代码示例
- Python tf.image.random_brightness用法及代码示例
- Python tf.image.pad_to_bounding_box用法及代码示例
- Python tf.io.gfile.GFile.close用法及代码示例
- Python tf.image.adjust_hue用法及代码示例
- Python tf.image.random_contrast用法及代码示例
- Python tf.image.rot90用法及代码示例
- Python tf.identity_n用法及代码示例
- Python tf.image.random_hue用法及代码示例
- Python tf.io.gfile.join用法及代码示例
- Python tf.io.parse_example用法及代码示例
- Python tf.image.flip_left_right用法及代码示例
- Python tf.image.convert_image_dtype用法及代码示例
- Python tf.image.stateless_random_flip_up_down用法及代码示例
- Python tf.image.random_saturation用法及代码示例
- Python tf.image.extract_glimpse用法及代码示例
- Python tf.image.flip_up_down用法及代码示例
- Python tf.image.crop_to_bounding_box用法及代码示例
- Python tf.image.stateless_random_jpeg_quality用法及代码示例
- Python tf.io.gfile.exists用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.init_scope。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。