Pycharm抱怨将[]运算符与pytorch的nn.ModuleList对象一起使用

时间:2019-10-06 18:26:05

标签: python pycharm pytorch

Pycharm的类型检查抱怨以下代码块(取自pytorch's docs):

    import torch.nn as nn
    class MyModule(nn.Module):
    def __init__(self):
        super(MyModule, self).__init__()
        self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])

    def forward(self, x):
        # ModuleList can act as an iterable, or be indexed using ints
        for i, l in enumerate(self.linears):
            x = self.linears[i // 2](x) + l(x)
        #                   ^---------- complains here about:
        # Cannot find reference '[' in 'input:(Any, ...), kwargs:dict) -> Any'
        return x

有没有一种方法可以解决这个问题而不抑制它?

1 个答案:

答案 0 :(得分:0)

这是一个已知问题https://youtrack.jetbrains.com/issue/PY-37601,将在2020.1中修复。