【问题标题】:Pycharm complains about using [] operator with pytorch's nn.ModuleList objectPycharm 抱怨在 pytorch 的 nn.ModuleList 对象中使用 [] 运算符
【发布时间】:2020-02-04 04:38:06
【问题描述】:

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

有没有办法解决这个问题而不抑制它?

【问题讨论】:

标签: python pycharm pytorch


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2020-11-12
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多