【问题标题】:Pytorch TypeError: forward() takes 2 positional arguments but 4 were givenPytorch TypeError: forward() 接受 2 个位置参数,但给出了 4 个
【发布时间】:2021-08-20 07:34:27
【问题描述】:
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
class Graphconvlayer(nn.Module):
  def __init__(self,adj,input_feature_neurons,output_neurons):
    super(Graphconvlayer, self).__init__()
    self.adj=adj
    self.input_feature_neurons=input_feature_neurons
    self.output_neurons=output_neurons
    self.weights=Parameter(torch.normal(mean=0.0,std=torch.ones(input_feature_neurons,output_neurons)))
    self.bias=Parameter(torch.normal(mean=0.0,std=torch.ones(input_feature_neurons)))
  
  def forward(self,inputfeaturedata):
    output1= torch.mm(self.adj,inputfeaturedata)
    print(output1.shape)
    print(self.weights.shape)
    print(self.bias.shape)
    output2= torch.matmul(output1,self.weights.t())+ self.bias
    return output2 

class GCN(nn.Module):
   def __init__(self,lr,dropoutvalue,adjmatrix,inputneurons,hidden,outputneurons):
     super(GCN, self).__init__()
     self.lr=lr
     self.dropoutvalue=dropoutvalue
     self.adjmatrix=adjmatrix
     self.inputneurons=inputneurons
     self.hidden=hidden
     self.outputneurons=outputneurons
     self.gcn1 = Graphconvlayer(adjmatrix,inputneurons,hidden)
     self.gcn2 = Graphconvlayer(adjmatrix,hidden,outputneurons)
  
   def forward(self,x,adj):
     x= F.relu(self.gcn1(adj,x,64))
     x= F.dropout(x,self.dropoutvalue)
     x= self.gcn2(adj,x,7)
     return F.log_softmax(x,dim=1)

a=GCN(lr=0.001,dropoutvalue=0.5,adjmatrix=adj,inputneurons=features.shape[1],hidden=64,outputneurons=7)
a.forward(adj,features)

TypeError                                 Traceback (most recent call last)
<ipython-input-85-7d1a2a73ecad> in <module>()
     37 
     38 a=GCN(lr=0.001,dropoutvalue=0.5,adjmatrix=adj,inputneurons=features.shape[1],hidden=64,outputneurons=7)
---> 39 a.forward(adj,features)

1 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self.forward(*input, **kwargs)
    888         for hook in itertools.chain(
--> 889                 _global_forward_hooks.values(),
    890                 self._forward_hooks.values()):
    891             hook_result = hook(self, input, result)

TypeError: forward() takes 2 positional arguments but 4 were given
print(a)
>>>
GCN(
  (gcn1): Graphconvlayer()
  (gcn2): Graphconvlayer()
)

这是一个图神经网络。我想要得到的是前向层的输出。我不确定为什么会出现上述错误以及我应该更改哪些代码才能正常工作。 谁能指导我完成这个?

如果我将类 graphconvlayer 传递给类 GCN,我现在是否必须将它的每个参数也分别传递给类 GCN 的对象 ä?

【问题讨论】:

    标签: class oop deep-learning neural-network pytorch


    【解决方案1】:

    您的GCN 由两个Graphconvlayer 组成。
    正如您发布的代码中所定义的,Graphconvlayerforward 方法只需要 一个 输入参数:inputfeaturedata。但是,当GCN 调用self.gcn1self.gcn2(在其forward 方法中)时,它会传递3 个参数:self.gcn1(adj,x,64)self.gcn2(adj,x,7)
    因此,self.gcn1self.gcn2 接收的不是单个输入参数,而是 3 - 这是您得到的错误。

    【讨论】:

    • 我用三个参数初始化了Graphconvlayer。所以我不必传递三个参数来调用该类,然后分别调用前向吗?
    • @kapooraae489 你在GCN__init__ 方法中初始化Graphconvlayer ——你需要所有三个参数。但是在 GCNforward 方法中,您不再需要 __init__ Graphconvlayer - 您只需调用 (implicitly) 它是需要 single 参数的 forward 方法.
    • 当我传递单个参数 GNC(data) 时,我收到以下错误:TypeError: __init__() missing 2 required positional arguments: 'input_feature_neurons' 和 'output_neurons'
    • @kapooraae489 您的错误不是来自您调用GCN 的方式,而是来自GCN 在其Graphconvlayer 方法中调用Graphconvlayer 的方式。你需要调试GCN.forward的代码。
    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 2019-12-07
    • 2017-09-06
    相关资源
    最近更新 更多