简体   繁体   English

Pytorch 的 add_module() 是什么?

[英]what is Pytorch's add_module()?

I stumbled upon the method add_module() in a Pytorch model.我偶然发现了 Pytorch model 中的add_module()方法。

The doc only states 该文档仅说明

Adds a child module to the current module.将子模块添加到当前模块。

The module can be accessed as an attribute using the given name.可以使用给定名称作为属性访问模块。

I don't understand what "adding a child module" means.我不明白“添加子模块”是什么意思。

How is it different from just setting a pointer to the other module using self._other module = other_module ?与仅使用self._other module = other_module设置指向其他模块的指针有何不同?

What are the nuances?细微差别是什么?

As mentioned here: https://discuss.pytorch.org/t/when-to-use-add-module-function/10534如此处所述: https://discuss.pytorch.org/t/when-to-use-add-module-function/10534

In general, you won't need to call add_module .通常,您不需要调用add_module One potential use case is the following:一个潜在的用例如下:

class Net(nn.Module):
    def __init__(self):
    super(Net, self).__init__()
        modules = [...]  # some list of modules
        for module in modules:
            self.add_module(...)

nn.Module s have a hierarchy of child modules that you can access via methods like module.named_children() or module.children() . nn.Module具有子模块的层次结构,您可以通过module.named_children()module.children()方法访问这些子模块。

As mentioned in the forum post above, doing self._other module = other_module will type check other_module , see it's an nn.Module , and also add it to the child list, so add_module isn't really necessary.正如上面论坛帖子中提到的那样,执行self._other module = other_module将检查other_module ,看到它是一个nn.Module ,并将其添加到子列表中,因此add_module并不是真正必要的。

Adding a module as an attribute works fine, as you say.正如您所说,将模块添加为属性可以正常工作。 But it can be a bit difficult to do at runtime if you don't know how many modules you have in advance, and you have to construct names programmatically.但是如果你事先不知道你有多少个模块,并且你必须以编程方式构造名称,那么在运行时可能会有点困难。 In such a case, add_module() is a very convenient way to do this.在这种情况下, add_module()是一种非常方便的方法。 I've just written a short blog post showing this in action: https://blog.d-and-j.net/deep-learning/2021/04/23/pytorch-add_module.html我刚刚写了一篇简短的博客文章,展示了这一点: https://blog.d-and-j.net/deep-learning/2021/04/23/pytorch-add_module.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PyTorch 中“模块”的定义究竟是什么? - What exactly is the definition of a 'Module' in PyTorch? 在 PyTorch Function 中使用 PyTorch 模块的正确方法是什么? - What is the correct way to use a PyTorch Module inside a PyTorch Function? PyTorch 中“参差不齐/锯齿状张量”的解决方法是什么? - What's the workaround for “ragged/jagged tensors” in PyTorch? 将行为添加到外部模块返回的对象的pythonic方法是什么? - What's the pythonic way to add behavior to objects returned by an external module? Tensorboard (PyTorch) add_graph 中的错误 - Error in Tensorboard's(PyTorch) add_graph tensorflow 中 pytorch module.register_parameter(name, param) 的替代方案是什么? - What is the alternative of pytorch module.register_parameter(name, param) in tensorflow? 如何在pytorch自定义模型的模块类中添加参数? - How to add parameters in module class in pytorch custom model? 在这个 PyTorch 派生的 nn.Module class 方法中 self 指的是什么? - What is self referring to in this PyTorch derived nn.Module class method? pytorch 的 nn.Module 如何注册子模块? - How does pytorch's nn.Module register submodule? 是什么导致 Pytorch nn 模块为此数组的 argmax 返回 1 或 0? - What causes the Pytorch nn Module to return a 1 or 0 for the argmax of this array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM