Pytorch to predict flight coefficient

Hi there!
I am a begginer in ML and NN, but currently have to develop one at work.
This is something I am interested in pregressed quite a bit, but after being able to predict one value based on one input, I am now facing a wall I cant climb over on my own.

I have 7 inputs (year, month, day, class of the plane, departure, arrival and customly made ‘flight’ which is basically departure + arrival)
I have to be able to predict coefficient based on those inputs (except ‘flight’)
I prepared all the date (it is all in numbers, not strings) and have date from last 3 years (2019-2021 with calcualeted inputs)
I tried solving it manually adjusting weights etc but too many FP.

My x_train tensor is shaped

torch.Size([256817, 7])

and my NN looks like this:

class Net(nn.Module):
    def __init__(self, input_size): #input size (1x7)
        super(Net, self).__init__()
        n1 = 20 #no. nodes in layer 1
        n2 = 10 #no. nodes in layer 2
        self.fc1 = nn.Linear(input_size, n1)
        self.fc2 = nn.Linear(n1, n2)
        self.fc3 = nn.Linear(n2, 1)
    def forward(self, x):
        x = self.fc1(x)
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x
  1. for some reason

model = Net( )
TypeError: init() missing 1 required positional argument: ‘input_size’

gives me following error and if I input 7 as ‘input_size’ I get following

Traceback (most recent call last):
File “C:\Users\User\PycharmProjects\pythonProject1\NN_1103.py”, line 108, in
y_pred = model(X_train)
File “C:\Users\User\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _call_impl
return forward_call(*input, **kwargs)
File “C:\Users\User\PycharmProjects\pythonProject1\NN_1103.py”, line 87, in forward
x = torch.relu(self.fc1(x))
File “C:\Users\User\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _call_impl
return forward_call(*input, **kwargs)
File “C:\Users\User\anaconda3\lib\site-packages\torch\nn\modules\linear.py”, line 103, in forward
return F.linear(input, self.weight, self.bias)
File “C:\Users\User\anaconda3\lib\site-packages\torch\nn\functional.py”, line 1848, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (256817x20 and 7x20)

any and all help will be appriciated.
I understand that I am trying to muliply matrixes that cant be multipled but have no idea how to fix it

if i remove model = Net, then

#Backward pass
criterion = nn.MSELoss() #loss function
optimizer = optim.SGD(Net.parameters(), lr=1e-3, momentum=0.9)

gives error:

Traceback (most recent call last):
File “C:\Users\User\PycharmProjects\pythonProject1\NN_1103.py”, line 101, in
optimizer = optim.SGD(Net.parameters(), lr=1e-3, momentum=0.9)
TypeError: parameters() missing 1 required positional argument: ‘self’

input_size should match the feature dimension of your input.
Based on the error message I guess you are using input_size=7 while the input has a shape of [batch_size, 20].
Change input_size=20 and it should work.
However, you would then hit the next shape mismatch error, since you are reusing self.fc1 and self.fc2 while their in_features and out_features differ:

        x = self.fc1(x)
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        x = torch.relu(self.fc2(x))

I’m not familiar with your use case, but you might want to remove the duplicated calls or make sure these layers can be used recursively.

1 Like

Thank you for your answer!
Would u mind how I should go around the problem
my x (input) is a tensor

([290816, 7])
which means

batch_size = 290816
and 7 features as an input

I have y as a single values

I was told I could select any number of nodes for my NN… it is an issue? From what I understood I must have first node as 7, right?

Sorry to bother you. If you do tutoring I would gladly pay for a couple of lessons (Im ok at maths, just struggling with syntax)

Yes, this is correct and would work for the first layer,

The previously described error is raised since you are reusing the same layers.
Remove the duplicated usage and it would work:

class Net(nn.Module):
    def __init__(self, input_size): #input size (1x7)
        super(Net, self).__init__()
        n1 = 20 #no. nodes in layer 1
        n2 = 10 #no. nodes in layer 2
        self.fc1 = nn.Linear(input_size, n1)
        self.fc2 = nn.Linear(n1, n2)
        self.fc3 = nn.Linear(n2, 1)
    def forward(self, x):
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x
    

model = Net(input_size=7)
x = torch.randn([290816, 7])
out = model(x)
1 Like