Error: forward() missing 1 required positional argument: 'x'

hfdp: <p>Hello, I am quite new to pytorch. This is my first try and I am getting an error named <strong>forward() missing 1 required positional argument: ‘x’</strong> and I don’t know why. If somebody could help me that would be appreciated. Here is the code:</p>
<p><strong>First I load the data in and afterwards I make a directory to make the data a .pt tensor:</strong></p>
<pre><code class="lang-auto">try:
    os.mkdir('data')
except:
    print('Directory exists')

# Save the files with leading zeros (e.g., ID00001.pt) and save in each file all three frequencies
# Also, create a text file that lists all the files available (all data). This is used to sort the dataset in the different sets
decimal_places = 6
os.path.join('ID_list.txt') #hier stond eerst os.remove()
ID_list = open('ID_list.txt','a') 
for sample in range(T20MHz.shape[1]):
    name = 'ID' + str(sample+1).zfill(decimal_places) +'.pt'
    ID_list.write(name + '\n')
    torch.save([T20MHz[:,sample], T100MHz[:,sample], T250MHz[:,sample]], 'data/' + name)
ID_list.close()
</code></pre>
<p><strong>Then I load the data for trainign and validation:</strong></p>
<pre><code class="lang-auto">import torch
from torch.utils import data

class Dataset(data.Dataset):
    
    'Characterizes a dataset for PyTorch'
    def __init__(self, list_IDs):
          'Initialization'
          self.list_IDs = list_IDs
    
    def __len__(self):
          'Denotes the total number of samples'
          return len(self.list_IDs)
    
    def __getitem__(self, index):
          'Generates one sample of data'
          # Select sample
          ID = self.list_IDs[index]
    
          # Load data and get label
          sample = torch.load('data/' + ID)
          T20MHz = torch.DoubleTensor(sample[0]).detach()
          T100MHz = torch.DoubleTensor(sample[1]).detach()
          T250MHz = torch.DoubleTensor(sample[2]).detach()
          return T20MHz, T100MHz, T250MHz
      
</code></pre>
<p><strong>Then I define my CNN:</strong></p>
<pre><code class="lang-auto">import torch.nn as nn
import torch.nn.functional as F

class model(nn.Module):
   def __init__(self):
       super(model, self).__init__()
       #1 input trace channel, 18 output channels and 3x3 square (?) convolution kernel        
       self.conv1 = nn.Conv1d(1, 3, kernel_size=3) 
       self.conv2 = nn.Conv1d(3, 6, kernel_size=6) 
       self.conv3 = nn.Conv1d(6, 12, kernel_size=12) 
       self.conv4 = nn.Conv1d(12,24, kernel_size=24) 
       self.conv5 = nn.Conv1d(24,48, kernel_size = 48)
       self.conv_drop = nn.Dropout2d()
       self.conv6_transpose = nn.ConvTranspose1d(48,24, kernel_size = 48)
       self.conv7_transpose = nn.ConvTranspose1d(24,12, kernel_size = 24)
       self.conv8_transpose = nn.ConvTranspose1d(12,6, kernel_size = 12)
       self.conv9_transpose=nn.ConvTranspose1d(6,3, kernel_size = 6)
       self.conv10_transpose = nn.ConvTranspose1d(3,1, kernel_size = 3)
      
      #self.fc1 = nn.Linear(1536, 72) #Fully-connected classifier layer
      #self.fc2 = nn.Linear(72, 19) #Fully-connected classifier layer
       self.bn1 = nn.BatchNorm1d(3)
       self.bn2 = nn.BatchNorm1d(6)
       self.bn3 = nn.BatchNorm1d(12)
       self.bn4 = nn.BatchNorm1d(24)
       self.bn5 = nn.BatchNorm1d(48)
       self.bn6 = nn.BatchNorm1d(24)
       self.bn7 = nn.BatchNorm1d(12)
       self.bn8 = nn.BatchNorm1d(6)
       self.bn9 = nn.BatchNorm1d(3)
       self.bn10 = nn.BatchNorm1d(1)
      
   
   def forward(self, x):
       #x = x.unsqueeze(1) # create an additional channel dimension (only 1 channel here).. We have a single sample, so add a fake batch dimension
      #downsample
       out = self.conv1(x)
       out = F.relu(out)
       out = self.bn1(out)
       
       out = self.conv2(out)
       out = F.relu(out)
       out = self.bn2(out)
       
       out = self.conv3(out)
       out = F.relu(out)
       out = self.bn3(out)
       
       out = self.conv4(out)
       out = F.relu(out)
       out = self.bn4(out)
       
       #bottleneck
       out = self.conv5(out)
       out = F.relu(out)
       out = self.bn5(out)
       out = self.conv_drop(out)
       
       #upsample
       out = self.conv6_transpose(out)
       out = self.conv6_drop(out)
       out = F.relu(out)
       out = out.unsqueeze(1)
       out = self.bn6(out)
       
       out = self.conv7_transpose(out)
       out = self.conv_drop(out)
       out = F.relu(out)
       out = out.unsqueeze(1)
       out = self.bn7(out)
       
       out = self.conv8_transpose(out)
       out = self.conv_drop(out)
       out = F.relu(out)
       out = out.unsqueeze(1)
       out = self.bn8(out)
       
       out = self.conv9_transpose(out)
       out = self.conv_drop(out)
       out = F.relu(out)
       out = out.unsqueeze(1)
       out = self.bn9(out)
       
       out = self.conv10_transpose(out)
       out = self.conv_drop(out)
       out = F.relu(out)
       out = out.unsqueeze(1)
       out = self.bn10(out)
       
       return out
       
       
model = model()

print(model)
</code></pre>
<p><strong>I set my parameters and optimizer:</strong></p>
<pre><code class="lang-auto">import torch
from torch.utils import data
import numpy as np
import torch.optim as optim


# Check if CUDA is available
use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
# cudnn.benchmark = True


# Set training parameters
params = {'batch_size': 64,
          'shuffle': True,
          'num_workers': 6}
max_epochs = 100

# Load all the data from the txt file
file_IDs = open('ID_list.txt','r').read().split('\n')
file_IDs = file_IDs[:-1] # remove last line
complete_dataset = Dataset(file_IDs)


#%% Here we define a loss function and the optimizer
# create your loss function
def rmse(y, y_hat):
    """Compute root mean squared error"""
    return torch.sqrt(torch.mean((y - y_hat).pow(2)))

# create your optimizer
optimizer = optim.SGD(model.parameters(), lr=0.0003, momentum = 0.1)
</code></pre>
<p><strong>And try to train it/test if it works in the first place</strong></p>
<pre><code class="lang-auto"># Divide the dataset into the training and validation set
lengths = [int(np.ceil(len(complete_dataset)*0.8)), int(np.floor(len(complete_dataset)*0.1)), int(np.floor(len(complete_dataset)*0.1))]
training_set, validation_set, evaluation_set = torch.utils.data.random_split(complete_dataset, lengths)
training_generator = data.DataLoader(training_set, **params)
validation_generator = data.DataLoader(validation_set, **params)
evaluation_generator = data.DataLoader(evaluation_set, **params)


# instantiate the model to make it a double tensor
forward_model = model().double()
t20, t100 ,t250 = next(iter(training_generator))
one_prediction = forward_model(t20)
</code></pre>
<p>THE ERROR SEEMS TO APPEAR HERE IN THE LAST STEP.</p>
<p>I don’t know what to fizx so if anyone can help it would be lovely.<br>
Thank!</p>

albanD: <p>Hi,</p>
<p>I edited your post to make the code a bit more readable <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>I think the main confusion comes from the fact that your class name is <code>model</code> and the name of the instance you create is <code>model</code> as well. I am pretty sure that you call <code>forward_model = model().double()</code> with <code>model</code> already being an instance and not the class.<br>
You can rename the class <code>MyModel</code> or something and it should make it clearer.</p>

hfdp: <p>Hello!</p>
<p>Thank you for making it look nice and answering <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
That was indeed the main confusion!<br>
However, the last two lines shown on the code I posted here still give an error, somethin with  broken pipe <img src="https://discuss.pytorch.org/images/emoji/apple/stuck_out_tongue.png?v=9" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:"><br>
Do you also have any idea on that one?</p>
<p>Thanks so much.</p>

albanD: <p>Broken pipe? Do you use multiprocessing?</p>
<p>What is the full error message?</p>

hfdp: <aside class="quote no-group" data-username="albanD" data-post="5" data-topic="80332">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>multiprocessing</p>
</blockquote>
</aside>
<p>The idea is to eventually run the code on a cluster, yes. This was the error:</p>
<p>ForkingPickler(file, protocol).dump(obj)<br>
BrokenPipeError: [Errno 32] Broken pipe</p>

albanD: <p>Not sure if it is pytorch related…<br>
Maybe try using the spawn method ?</p>

hfdp: <p>Maybe I will try running it on the cluster now since the error is solved. And then see what happens.<br>
Thanks for the tips at least, I’m learning about Pytorch so good to know about the name of the classes etc <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"><br>
Thanks!</p>

Landi_Bamban: <p>thanks your answer is very helpful, maybe I’ll really slam my laptop if I do not find the answer​:joy:<img src="https://discuss.pytorch.org/images/emoji/apple/relieved.png?v=9" title=":relieved:" class="emoji" alt=":relieved:"></p>

shravanti_ivaturi: <p>I got this error<br>
Traceback (most recent call last):<br>
File “/Fabric-defect-detection-based-on-improved-RefineDet/test.py”, line 510, in <br>
test_net(args.save_folder, net, args.cuda, dataset,<br>
File “/Fabric-defect-detection-based-on-improved-RefineDet/test.py”, line 439, in test_net<br>
detections = net(x).data<br>
File “/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py”, line 1194, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
TypeError: forward() missing 1 required positional argument: ‘x’</p>
<p>Can someone help me with it</p>