Pytorch simple begginer example class: not registering parameters correctly

lobachevscki: <p>Hi!</p>
<p>Im following the <a href="http://LearnPytorch.io" rel="noopener nofollow ugc">LearnPytorch.io</a> tutorial and Im stuck in the ‘Putting it all Together’ section of the workflow fundamental (i cant post the link as a new user i can only post 1 link in my first post, but all the relevant info is below)</p>
<p>Im not using Google Colab but my own installation of PyCharm Community and Anaconda. It works perfectly.</p>
<p>Now the problem is that creating the class doesnt seem to register the parameters as expected.</p>
<p>My code is the same than in the lessons, here it is:</p>
<pre><code class="lang-auto">import torch
from torch import nn
import matplotlib.pyplot as plt
device = 'cuda' if torch.cuda.is_available() else 'cpu'

weight = 0.7
bias = 0.23

start = 0
end = 1

step = 0.01

X = torch.arange(start, end, step).unsqueeze(dim = 1)
y = weight * X + bias

train_split = int(0.8*len(X))
X_train, y_train = X[:train_split], y[:train_split]
X_test, y_test = X[train_split:], y[train_split:]

class LinearRegressionModelV2(nn.Module):
    def __int__(self):
        super().__init__()
        self.linear_layer = nn.Linear(in_features=1, out_features=1)

    def forward(self, x: torch.Tensor) -&gt; torch.Tensor:
        return self.linear_layer(x)

</code></pre>
<p>But when i try to print the state_dict by simply it prints an empty dictionary. This is a test code for you to see the result:</p>
<pre><code class="lang-auto">torch.manual_seed(42)
model_1 = LinearRegressionModelV2()

print(model_1)
print(model_1.state_dict())
print('---------------------')
print(nn.Linear(in_features=1, out_features=1).state_dict())
</code></pre>
<p>Gives this result:</p>
<pre><code class="lang-auto">LinearRegressionModelV2()
OrderedDict()
---------------------
OrderedDict([('weight', tensor([[0.7645]])), ('bias', tensor([0.8300]))])


</code></pre>
<p>So if i create an instance of my newly created linear class it doesnt register any parameters it seems, for comparison if i just simply instance directly from nn.Linear the parameters are there (as seen above).</p>
<p>Moreover if write this</p>
<p><code>print(next(nn.Linear(in_features=1, out_features=1).parameters()).device)</code></p>
<p>It prints the device but if try to do that with the instance of the class i created it prompts an error.</p>
<p>This blocks me completely for moving forward with the lesson.</p>
<p>So, what i understood from reading in the internet is that my class is not inheriting the parameters from the nn.Linear as we should expect, which is in any case what is being reflected in the behavior described above. Moreover, in the previous lessons everything worked perfectly and the difference is precisely that I literally defined the parameters with the nn.Parameter method. So the parameters are definitely missing but i simply dont have the knowledge to know how to fix it.</p>
<p>When I look for a similar issue on Google i get this result: <a href="https://discuss.pytorch.org/t/the-difference-in-usage-between-nn-modulelist-and-python-list/7744" class="inline-onebox">The difference in usage between nn.ModuleList and python list</a></p>
<p>But, again, i simply dont have the intuition yet to understand the solution.</p>
<p>Any suggestions here?</p>
<p>Thanks</p>

ptrblck: <p>You have a typo in <code>__init__(self)</code> as you are using <code>__int__(self)</code>. Fix it and it should work.</p>

lobachevscki: <p>Yes, that did it.</p>
<p>Im so embarassed hahahah, i guess i can learn to be as careful as possible all the time.</p>
<p>Thank you very much for taking the time to read.</p>