Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python forward()接受1个位置参数,但给出了2个_Python_Machine Learning_Pytorch_Efficientnet - Fatal编程技术网

Python forward()接受1个位置参数,但给出了2个

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

我正试图用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         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)调用前向方法

其他可概括如下:

  • 定义一个新的MyEfficientNet,该MyEfficientNet覆盖forward方法,以避免调用最后一层

  • 覆盖不需要标识层的层。例如:

    m、 _fc=nn.Identity()

  • 当使用efficientnet作为功能提取器时,应首选第一种方法,而当需要更多定制时,应使用其他方法。

    通过使用include\u top参数,您可以轻松获得无最后一层的网络:

    m = EfficientNet.from_pretrained('efficientnet-b0', include_top=False)
    
    很容易看出,它所做的不是为最后一层(AveragePool、Dropout、FC)调用前向方法

    其他可概括如下:

  • 定义一个新的MyEfficientNet,该MyEfficientNet覆盖forward方法,以避免调用最后一层

  • 覆盖不需要标识层的层。例如:

    m、 _fc=nn.Identity()


  • 当使用efficientnet作为功能提取器时,应首选第一种方法,而当需要更多定制时,应使用其他方法。

    转发(self,*x)
    中添加星号将列表解压缩
    x
    。您应该删除它,这个内核可以使用ResNet运行。我认为没有必要使用星号,因为我正在输入一个元组。请让我知道我是否能找出原因。请将代码部分粘贴到您使用
    模型
    类实例的位置。感谢您联系我们,作为一个模型,我们已在此基础上转移了学习。在
    forward(self,*x)
    中添加星号将解压列表
    x
    。您应该删除它,这个内核可以使用ResNet运行。我认为没有必要使用星号,因为我正在输入一个元组。请让我知道我是否能找出原因。请将代码部分粘贴到您使用
    模型
    类实例的位置。感谢您联系我们,作为一个模型,我们已在此基础上转移了学习。