1

I can't initialize my model in pytorch and get:

TypeError                                 Traceback (most recent call last)
<ipython-input-82-9bfee30a439d> in <module>()
288 dataset = News_Dataset(true_path=args.true_news_file, 
                            fake_path=args.fake_news_file, 
289                         embeddings_path=args.embeddings_file)
 --> 290 classifier = News_classifier_resnet_based().cuda()
291 try:
292   classifier.load_state_dict(torch.load(args.model_state_file))

/usr/local/lib/python3.6/dist-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() missing 1 required positional argument: 'input'

Here is my code:

class News_classifier_resnet_based(torch.nn.Module):
    def __init__(self):
        super().__init__()

        self.activation = torch.nn.ReLU6()
        self.sigmoid = torch.nn.Sigmoid()

        self.positional_encodings = PositionalEncoder()

        self.resnet = list(torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).children())

        self.to_appropriate_shape = torch.nn.Conv2d(in_channels=1, out_channels=1, kernel_size=77)

        self.conv1 = torch.nn.Conv2d(in_channels=1,out_channels=64,kernel_size=7,stride=2,padding=3)
        self.conv1.weight = torch.nn.Parameter(self.resnet[0].weight[:,0,:,:].data)
        self.center = torch.nn.Sequential(*self.resnet[1:-2])
        self.conv2 = torch.nn.Conv2d(in_channels=512, out_channels=1, kernel_size=1)
        self.conv3 = torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=7)

        self.title_conv = torch.nn.Sequential(
                    torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=3,stride=3),
                    self.activation(),
                    torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=2,stride=2),
                    self.activation(),
                    torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=2,stride=2)
                )
       self.title_lin = torch.nn.Linear(25,1)

       self.year_lin = torch.nn.Linear(10,1)
       self.month_lin = torch.nn.Linear(12,1)
       self.day_lin = torch.nn.Linear(31,1)
       self.date_lin = torch.nn.Linear(3,1)

       self.final_lin = torch.nn.Linear(3,1)

   def forward(self,x_in):
       #input shape - (batch_size, 3+title_len+seq_len, embedding_dim)
       #output shape - (batch_size, 1)
       year = x_in[:,0,:10]
       month = x_in[:,1,:12]
       day = x_in[:,2,:31]

       title = x_in[:,3:3+args.title_len,:]
       text = x_in[:,3+args.title_len:,:]
       title = self.positional_encodings(title)
       text = self.positional_encodings(text)

       text = text.unsqueeze(1)
       text = self.activation(self.to_appropriate_shape(text))
       text = self.activation(self.conv1(text))
       text = self.activation(self.center(text))
       text = self.activation(self.conv2(text))
       text = self.activation(self.conv3(text))
       text = text.reshape(args.batch_size,-1)

       title = title.unsqueeze(1)
       title = self.activation(self.title_conv(title))
       title = title.reshape(args.batch_size,-1)
       title = self.activation(self.title_lin(title))

       year = self.activation(self.year_lin(year))
       month = self.activation(self.month_lin(month))
       day = self.activation(self.day_lin(day))
       date = torch.cat([year,month,day], dim=-1)
       date = self.activation(self.date_lin(date))

       final = torch.cat([date,title,text], dim=-1)
       final = self.sigmoid(self.final_lin(final))

       return final

classifier = News_classifier_resnet_based().cuda()

What should I do? I'm trying to classify texts using word embeddings but problem lies in last line. I am working in google colab. Also when I created some models in other code blocks, I've got no problems.

4
  • 1
    Could you show the model News_classifier_resnet_based please? Commented Jul 11, 2020 at 13:36
  • please add more details like what error you gets. Commented Jul 11, 2020 at 13:36
  • Don't expect SO users to login in colab to check the code. Commented Jul 11, 2020 at 13:47
  • It might be a better choice to place the code here, else you might have to give access to your colab file to everyone. As of now it denies access. Commented Jul 11, 2020 at 13:51

2 Answers 2

2

The problem is in your init function. When you create title_conv, insted of passing the activation object previously created, your are calling the activation without arguments. You can fix it by changing that part of code with this:

self.title_conv = torch.nn.Sequential(
                    torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=3,stride=3),
                    self.activation, # Notice I have removed ()
                    torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=2,stride=2),
                    self.activation, # Notice I have removed ()
                    torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=2,stride=2)
                )
Sign up to request clarification or add additional context in comments.

Comments

0

If the self.title_conv is changed in either of the below ways, the error doesn't appear.

self.title_conv = torch.nn.Sequential(
                torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=3,stride=3),
                torch.nn.ReLU6(),
                torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=2,stride=2),
                torch.nn.ReLU6(),
                torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=2,stride=2)
            )

or as:

self.title_conv = torch.nn.Sequential(
                torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=3,stride=3),
                self.activation,
                torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=2,stride=2),
                self.activation,
                torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=2,stride=2)
            )

I am not sure of the reason. I think it could be because self.activation is a function and a call is made again by writing it as self.activation().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.