Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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
Class Pytorch TypeError:forward()接受2个位置参数,但给出了4个_Class_Oop_Deep Learning_Neural Network_Pytorch - Fatal编程技术网

Class Pytorch TypeError:forward()接受2个位置参数,但给出了4个

Class Pytorch TypeError:forward()接受2个位置参数,但给出了4个,class,oop,deep-learning,neural-network,pytorch,Class,Oop,Deep Learning,Neural Network,Pytorch,这是一个图形神经网络。我想得到的是前向层的输出。我不确定为什么会出现上述错误,以及我应该修改什么才能使代码正常工作。 有人能指导我完成这件事吗 同样,如果我将类graphconvlayer传递给类GCN,我现在是否必须将它的每个参数也分别传递给类GCN的对象?您的GCN由两个graphconvlayer组成 正如您发布的代码中所定义的,Graphconvlayer的forward方法只需要一个输入参数:inputfeaturedata。但是,当GCN调用self.gcn1或self.gcn2(在

这是一个图形神经网络。我想得到的是前向层的输出。我不确定为什么会出现上述错误,以及我应该修改什么才能使代码正常工作。 有人能指导我完成这件事吗


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

您的
GCN
由两个
graphconvlayer
组成
正如您发布的代码中所定义的,
Graphconvlayer
forward
方法只需要一个输入参数:
inputfeaturedata
。但是,当
GCN
调用
self.gcn1
self.gcn2
(在其
forward
方法中)时,它传递3个参数:
self.gcn1(adj,x,64)
self.gcn2(adj,x,7)


因此,不是单个输入参数,
self.gcn1
self.gcn2
接收到3——这是您得到的错误。

我用三个参数初始化了Graphconvlayer。所以我不需要传递三个参数来调用该类,然后分别向前调用吗?@kapooraae489您可以在
GCN
中初始化
Graphconvlayer
\uuuu init\uuuu
方法——这里需要所有三个参数。但是在
GCN
forward
方法中,您不再需要
\uuuu init\uuuu
Graphconvlayer
-您只需调用()它是
forward
方法`需要一个参数。当我传递单参数GNC(data)时,我得到以下错误:TypeError:\uu init\uuuu()缺少2个必需的位置参数:“input_feature_neurons”和“output_neurons”@kapooraae489您的错误不是来自调用
GCN
的方式,而是来自
GCN
调用
Graphconvlayer
方法的方式。您需要调试
GCN.forward
的代码。
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()
)