简体   繁体   English

为什么我的 PyTorch ModuleList object 在分配为 class 变量后变成了一个“函数”object?

[英]Why does my PyTorch ModuleList object become a 'function' object after assigning it as a class variable?

I really need your help with something annoying that I can't explain.我真的需要你的帮助来解决一些我无法解释的烦人问题。 I am fairly new to PyTorch and want to implementa a Module that requires the usage of ModuleList.我是 PyTorch 的新手,想实现一个需要使用 ModuleList 的模块。 However after instantiation of an empty ModuleList object, I can't use its append-method.但是,在实例化一个空的 ModuleList object 之后,我无法使用它的追加方法。 Why?为什么?

self.modules = torch.nn.ModuleList([])
print(type(self.modules), type(torch.nn.ModuleList())) # -> <class 'method'> <class 'torch.nn.modules.container.ModuleList'>
for m in range(len(self.module_sizes)):

        prev_channels = 0 if m == 0 else self.module_out_channels[-m]
        vgg19_channels = self.vgg19_layer_channels[-(m+1)]
        module_out_channels = self.module_out_channels[-(m+1)]
        module_size = self.module_sizes[-(m+1)]

        self.modules.append(
            GeneratorModule(prev_channels=prev_channels, vgg19_channels=vgg19_channels, out_channels=module_out_channels, module_size=module_size, initial=(m==0))
            )

The error message says:错误消息说:

  File "main.py", line 90, in __init__
    self.modules.append(
AttributeError: 'function' object has no attribute 'append'

So far so good.到目前为止,一切都很好。 I assume that as soon as I assign a ModuleList as a class variable to my custom nn.Module subclass, it becomes a 'function' object. So I changed the code in a way that the ModuleList is assigned to self.modules after the appending loop.我假设一旦我将 ModuleList 作为 class 变量分配给我的自定义 nn.Module 子类,它就会变成一个“函数”object。所以我更改了代码,在附加后将 ModuleList 分配给 self.modules环形。 But then it throws another error in a later piece of code, where it says:但随后它在后面的一段代码中抛出了另一个错误,它说:

for m, module in enumerate(self.modules):
     pass

TypeError: 'method' object is not iterable

I know that this should work, since I found other people do it this way on the inte.net.我知道这应该可行,因为我发现其他人在 inte.net 上也是这样做的。

I would be so happy if any more experienced PyTorch programmer can help me out.如果有更多经验丰富的 PyTorch 程序员可以帮助我,我将非常高兴。 Thanks in advance!提前致谢!

Actually the problem is different.其实问题是不一样的。 As you can see torch.nn.ModuleList has append and other list-like methods so it should work.如您所见, torch.nn.ModuleList具有append和其他类似列表的方法,因此它应该可以工作。 No, it doesn't "change" to "function" object, that would be pretty unintuitive for everyone.不,它不会“改变”为“功能”object,这对每个人来说都是非常不直观的。

What's going on instead is PyTorch's torch.nn.Module has a few methods which you inherit, which includesmodules (it allows you to iterate over modules contained in torch.nn.Module instance).相反,PyTorch 的torch.nn.Module有一些你继承的方法,其中包括modules (它允许你迭代包含在torch.nn.Module实例中的modules )。

Just rename your self.modules to self.module_list and you are all good.只需将您的self.modules重命名为self.module_list就可以了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM