Converting linear to CNN

Hi everyone, I try to convert my linear system to CNN. My linear NN class is

class Module(nn.Module):
        def __init__(self, D_in, H1, H2,D_out):
                        super().__init__()
                        self.linear1 = nn.Linear(D_in, H1)
                        self.linear2 = nn.Linear(H1, H2)
                        self.linear3 = nn.Linear(H2, D_out)
                        self.dropout1 = nn.Dropout(0.2)
                        self.dropout2 = nn.Dropout(0.2)
        def forward(self, x):
                        x = F.relu(self.linear1(x))
                        x = self.dropout1(x)  
                        x = F.relu(self.linear2(x))
                        x = self.dropout2(x)
                        x = self.linear3(x)
                        return x

and my training set input shape is [2048,15,1]
and my interpreter loop is

for e in range(epochs):
  
        running_loss = 0.0
        running_corrects = 0.0
        val_running_loss = 0.0
        val_running_corrects = 0.0
  
        for inputs,out in train_generator:
                
                
                
                output=model(inputs)
                print(inputs.size())
                print(output.size())
                loss = criterion(output,out)
        
                preds,_=torch.max(output,1)
                #        
                outputss.append(preds.max().detach().numpy())
                losses.append(loss)
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                                                                
                

        
        else:
          with torch.no_grad():
                for val_inputs, val_labels in valid_generator:
                        
                        val_inputs=val_inputs.to(device)
                        val_labels=val_labels.to(device)
                        val_outputs = model(val_inputs)
                        
                        val_loss = criterion(val_outputs, val_labels)
                        
                        val_preds,_ = torch.max(val_outputs, 1)
                        val_running_loss_history.append(val_loss)
                        val_running_corrects_history.append(val_preds.max().detach().numpy())

and my CNN class :


class Module(nn.Module):
    #def __init__(self,insize,hid,out):
    def __init__(self,insize=15,hid=16,out=1):
        super().__init__()
        self.con1=nn.Conv1d(insize,hid,1)
        self.con2=nn.Conv1d(hid,out,1)
        self.fc1=nn.Linear(1*2048,1)
        self.fc2=nn.Linear(1901,1)
        self.drop=nn.Dropout(0.2)
    def forward(self,x):
        x=F.elu(self.con1(x))
        x=F.max_pool1d(x,1)
        x=F.elu(self.con2(x))
        x=self.drop(x)
        if x.size(0)==1901:
            x=x.view(-1,1901)
            x=self.fc2(-1,1901)
        else:
            x=x.view(-1,2048*1)
            x=self.fc1(x)
        
        return x

when I execute my code I got this error:

TypeError: forward() takes 2 positional arguments but 3 were given
How can I solve this?

Thank you

I would guess this error is raised in self.fc2, as you are feeding the shape:

x=self.fc2(-1,1901)

instead of the activation x. :wink:

1 Like

Oh thank you so much :smiley: