Pytorch 的 add_module() 是什么?

Gul*_*zar 6 python machine-learning deep-learning pytorch

add_module()我在 Pytorch 模型中偶然发现了该方法。

该文件仅说明

将子模块添加到当前模块。

可以使用给定名称将模块作为属性进行访问。

我不明白“添加子模块”是什么意思。

它与仅使用设置指向其他模块的指针有何不同self._other module = other_module

有哪些细微差别?

小智 2

正如这里提到的:https://discuss.pytorch.org/t/when-to-use-add-module-function/10534

\n

一般来说,您\xe2\x80\x99不需要调用add_module. 一种潜在的用例如下:

\n
class Net(nn.Module):\n    def __init__(self):\n    super(Net, self).__init__()\n        modules = [...]  # some list of modules\n        for module in modules:\n            self.add_module(...)\n
Run Code Online (Sandbox Code Playgroud)\n