Python forward()接受1个位置参数,但给出了2个
我正试图用EfficientNet-B0建立一个模型。 下面的代码显示了模型的详细信息。 当我尝试学习时,我犯了以下错误Python forward()接受1个位置参数,但给出了2个,python,machine-learning,pytorch,efficientnet,Python,Machine Learning,Pytorch,Efficientnet,我正试图用EfficientNet-B0建立一个模型。 下面的代码显示了模型的详细信息。 当我尝试学习时,我犯了以下错误 TypeError Traceback (most recent call last) ''' <ipython-input-17-fb3850894108> in forward(self, *x) 24 #x: bs*N x 3 x 128 x 128 25
TypeError Traceback (most recent call last)
'''
<ipython-input-17-fb3850894108> in forward(self, *x)
24 #x: bs*N x 3 x 128 x 128
25 print(x.shape) #([384, 3, 224, 224])
---> 26 x = self.enc(x)
27 #x: bs*N x C x 4 x 4
28 shape = x.shape
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
--> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
98 def forward(self, input):
99 for module in self:
--> 100 input = module(input)
101 return input
102
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
--> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)
TypeError: forward() takes 1 positional argument but 2 were given
通过使用include_top参数,您可以轻松获得没有最后一层的网络:
m = EfficientNet.from_pretrained('efficientnet-b0', include_top=False)
很容易看出,它所做的不是为最后一层(AveragePool、Dropout、FC)调用前向方法
其他可概括如下:
m = EfficientNet.from_pretrained('efficientnet-b0', include_top=False)
很容易看出,它所做的不是为最后一层(AveragePool、Dropout、FC)调用前向方法
其他可概括如下:
当使用efficientnet作为功能提取器时,应首选第一种方法,而当需要更多定制时,应使用其他方法。在
转发(self,*x)
中添加星号将列表解压缩x
。您应该删除它,这个内核可以使用ResNet运行。我认为没有必要使用星号,因为我正在输入一个元组。请让我知道我是否能找出原因。请将代码部分粘贴到您使用模型
类实例的位置。感谢您联系我们,作为一个模型,我们已在此基础上转移了学习。在forward(self,*x)
中添加星号将解压列表x
。您应该删除它,这个内核可以使用ResNet运行。我认为没有必要使用星号,因为我正在输入一个元组。请让我知道我是否能找出原因。请将代码部分粘贴到您使用模型
类实例的位置。感谢您联系我们,作为一个模型,我们已在此基础上转移了学习。