0
点赞
收藏
分享

微信扫一扫

Pytorch----Module __init__

sullay 2023-02-21 阅读 34

super().setattr ——待解决

    def __init__(self) -> None:
"""
Initializes internal Module state, shared by both nn.Module and ScriptModule.
"""

#这一行代码是 PyTorch 1.7 的新功能,可用于监测并记录 API 的调用
torch._C._log_api_usage_once("python.nn_module")

"""
Calls super().__setattr__('a', a) instead of the typical self.a = a
to avoid Module.__setattr__ overhead. Module's __setattr__ has special
handling for parameters, submodules, and buffers but simply calls into
super().__setattr__ for all other attributes.
"""
# 控制 training/testing 状态
super().__setattr__('training', True)

# 在训练过程中会随着 BP 而更新的参数
super().__setattr__('_parameters', OrderedDict())

# 在训练过程中不会随着 BP 而更新的参数
super().__setattr__('_buffers', OrderedDict())

super().__setattr__('_non_persistent_buffers_set', set())

# Backward 完成后会被调用的 hook
super().__setattr__('_backward_hooks', OrderedDict())


super().__setattr__('_is_full_backward_hook', None)

# Forward 完成后会被调用的 hook
super().__setattr__('_forward_hooks', OrderedDict())

# Forward 前会被调用的 hook
super().__setattr__('_forward_pre_hooks', OrderedDict())

# 得到 state_dict 以后会被调用的 hook
super().__setattr__('_state_dict_hooks', OrderedDict())


super().__setattr__('_load_state_dict_pre_hooks', OrderedDict())
super().__setattr__('_load_state_dict_post_hooks', OrderedDict())

# 子神经网络模块
super().__setattr__('_modules', OrderedDict())

举报

相关推荐

0 条评论