Pytorch TypeError: forward() takes 2 positional arguments but 4 were given

Question:

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()
)

This is a graph neural network. What I am trying to get is the output from the forward layer. I am not sure why I am getting the above error and what I should change for the code to work.
Can anyone guide me through this?

Also I am if I pass class graphconvlayer to class GCN, do I have to now separately pass each of it’s parameters also to the object รค of class GCN?

Asked By: kapooraae489

||

Answers:

Your GCN is composed of two Graphconvlayer.
As defined in the code you posted, Graphconvlayer‘s forward method expects only one input argument: inputfeaturedata. However, when GCN calls self.gcn1 or self.gcn2 (in its forward method) it passes 3 arguments: self.gcn1(adj,x,64) and self.gcn2(adj,x,7).
Hence, instead of a single input argument, self.gcn1 and self.gcn2 are receiving 3 — this is the error you are getting.

Answered By: Shai