Pytorch to predict flight coefficient

MagaMakhauri: <p>Hi there!<br>
I am a begginer in ML and NN, but currently have to develop one at work.<br>
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.</p>
<p>I have 7 inputs (year, month, day, class of the plane, departure, arrival and customly made ‘flight’ which is basically departure + arrival)<br>
I have to be able to predict coefficient based on those inputs (except ‘flight’)<br>
I prepared all the date (it is all in numbers, not strings) and have date from last 3 years (2019-2021 with calcualeted inputs)<br>
I tried solving it manually adjusting weights etc but too many FP.</p>
<p>My x_train tensor is shaped</p>
<blockquote>
<p>torch.Size([256817, 7])</p>
</blockquote>
<p>and my NN looks like this:</p>
<pre><code class="lang-auto">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
</code></pre>
<ol>
<li>for some reason</li>
</ol>
<blockquote>
<p>model = Net( )<br>
TypeError: <strong>init</strong>() missing 1 required positional argument: ‘input_size’</p>
</blockquote>
<p>gives me following error and if I input 7 as ‘input_size’ I get following</p>
<blockquote>
<p>Traceback (most recent call last):<br>
File “C:\Users\User\PycharmProjects\pythonProject1\NN_1103.py”, line 108, in <br>
y_pred = model(X_train)<br>
File “C:\Users\User\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “C:\Users\User\PycharmProjects\pythonProject1\NN_1103.py”, line 87, in forward<br>
x = torch.relu(self.fc1(x))<br>
File “C:\Users\User\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “C:\Users\User\anaconda3\lib\site-packages\torch\nn\modules\linear.py”, line 103, in forward<br>
return F.linear(input, self.weight, self.bias)<br>
File “C:\Users\User\anaconda3\lib\site-packages\torch\nn\functional.py”, line 1848, in linear<br>
return torch._C._nn.linear(input, weight, bias)<br>
RuntimeError: mat1 and mat2 shapes cannot be multiplied (256817x20 and 7x20)</p>
</blockquote>
<p>any and all help will be appriciated.<br>
I understand that I am trying to muliply matrixes that cant be multipled but have no idea how to fix it</p>
<p>if i remove model = Net, then</p>
<blockquote>
<p><span class="hashtag-raw">#Backward</span> pass<br>
criterion = nn.MSELoss() <span class="hashtag-raw">#loss</span> function<br>
optimizer = optim.SGD(Net.parameters(), lr=1e-3, momentum=0.9)</p>
</blockquote>
<p>gives error:</p>
<blockquote>
<p>Traceback (most recent call last):<br>
File “C:\Users\User\PycharmProjects\pythonProject1\NN_1103.py”, line 101, in <br>
optimizer = optim.SGD(Net.parameters(), lr=1e-3, momentum=0.9)<br>
TypeError: parameters() missing 1 required positional argument: ‘self’</p>
</blockquote>

ptrblck: <p><code>input_size</code> should match the feature dimension of your input.<br>
Based on the error message I guess you are using <code>input_size=7</code> while the input has a shape of <code>[batch_size, 20]</code>.<br>
Change <code>input_size=20</code> and it should work.<br>
However, you would then hit the next shape mismatch error, since you are reusing <code>self.fc1</code> and <code>self.fc2</code> while their <code>in_features</code> and <code>out_features</code> differ:</p>
<pre><code class="lang-python">        x = self.fc1(x)
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        x = torch.relu(self.fc2(x))
</code></pre>
<p>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.</p>

MagaMakhauri: <p>Thank you for your answer!<br>
Would u mind how I should go around the problem<br>
my x (input) is a tensor</p>
<blockquote>
<p>([290816, 7])<br>
which means</p>
</blockquote>
<blockquote>
<p>batch_size = 290816<br>
and 7 features as an input</p>
</blockquote>
<p>I have y as a single values</p>
<p>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?</p>
<p>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)</p>

ptrblck: <aside class="quote no-group" data-username="MagaMakhauri" data-post="3" data-topic="146172">
<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/magamakhauri/48/50804_2.png" class="avatar"> MagaMakhauri:</div>
<blockquote>
<p>From what I understood I must have first node as 7, right?</p>
</blockquote>
</aside>
<p>Yes, this is correct and would work for the first layer,</p>
<p>The previously described error is raised since you are reusing the same layers.<br>
Remove the duplicated usage and it would work:</p>
<pre data-code-wrap="python"><code class="lang-python">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)
</code></pre>