0


forward() takes 2 positional arguments but 3 were given

问题描述:

在forward中明明正确数量的参数,却报错:forward() takes 2 positional arguments but 3 were given;

问题分析:

使用nn.Sequential()定义的网络,只接受单输入

例如:

self.backbone=nn.Sequential(nn.lstm(input_size=20, hidden_size=40, num_layers=2),

                                nn.linear(in_features=40, out_features=2))

def forward(self, input):

    h0 = torch.randn(hidden_layers, batch_size, hidden)

    c0 = torch.randn(hidden_layers, batch_size, hidden)
     output, _ = self.backbone(input)  **(对)**

     output, _ = self.backbone(input, (h0, c0)   (**错误,因为nn.Sequential()定义的网络,只接受单输入**)

本文转载自: https://blog.csdn.net/weiyuangong/article/details/127919107
版权归原作者 沐辰光 所有, 如有侵权,请联系我们删除。

“forward() takes 2 positional arguments but 3 were given”的评论:

还没有评论