Attribut Error: 'LinearRegressionModel' object has no attribute 'weights'

username: Chris_Paul
cooked: <p>Hi, I’m new to PyTorch. Please Help. I did a basic linear regression. Here’s my code.<br>
weight = 0.7<br>
bias = 0.3<br>
start = 0<br>
end = 1<br>
step = 0.02<br>
X = torch.arange(start, end, step).unsqueeze(dim=1)<br>
y = weight * X + bias</p>
<p>train_split = int(0.8 * len(X))<br>
X_train, y_train = X[:train_split], y[:train_split]<br>
X_test, y_test = X[train_split:], y[train_split:]</p>
<p>class LinearRegressionModel(nn.Module):<br>
def <em>init</em>(self):<br>
super().<em>init</em>()<br>
self.weights = nn.Parameter(torch.randn(1,<br>
requires_grad = True,<br>
dtype = torch.float))<br>
self.bias = nn.Parameter(torch.randn(1,<br>
requires_grad = True,<br>
dtype = torch.float))</p>
<pre><code>def forward(self, x: torch.Tensor) -&gt; torch.Tensor:          
    return self.weights * x + self.bias
</code></pre>
<p>torch.manual_seed(42)<br>
model_0 = LinearRegressionModel()<br>
model_0.state_dict()</p>
<p>with torch.inference_mode():<br>
y_predicts = model_0(X_test)<br>
y_predicts</p>
<p>Full error:<br>
AttributeError                            Traceback (most recent call last)<br>
 in <br>
1 #<span class="hashtag">#predict</span> y_test with X_test<br>
2 with torch.inference_mode():<br>
----&gt; 3     y_predicts = model_0(X_test)<br>
4 y_predicts</p>
<p>2 frames<br>
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in <strong>getattr</strong>(self, name)<br>
1206                 return modules[name]<br>
1207         raise AttributeError(“‘{}’ object has no attribute ‘{}’”.format(<br>
 → 1208             type(self).<strong>name</strong>, name))<br>
1209<br>
1210     def <strong>setattr</strong>(self, name: str, value: Union[Tensor, ‘Module’]) → None:</p>
<p>AttributeError: ‘LinearRegressionModel’ object has no attribute ‘weights’</p>
<p><a href="https://colab.research.google.com/drive/1shwU9kBVCWRCjgPDnCaeEw0wIiF-V5j7#scrollTo=B-XH_bAoAvHa" rel="noopener nofollow ugc">here’s the work on colab.research.google</a></p>

username: ptrblck
cooked: <p>I don’t have access to your notebook so cannot reproduce and debug the issue. Your code is also not formated properly so I’m also unsure if a syntax error might be causing the issue. Could you check if you are able to access the <code>.weight</code> attribute directly? If that’s not working could you check if the parameter initialization is skipped for some reason?</p>

username: Chris_Paul
cooked: <p>Thank you for your swift reply. Here are the screenshots of my code. I even suspect its a case of system python error as I’m getting much errors lately. I would be extremely grateful if you could help me with his.</p>
<p>Thank you<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/f/ff4055a6b559ad57ec0e26e7853cfc3d64816725.png" data-download-href="https://discuss.pytorch.org/uploads/default/ff4055a6b559ad57ec0e26e7853cfc3d64816725" title="Screenshot from 2022-10-20 13-42-43"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/f/ff4055a6b559ad57ec0e26e7853cfc3d64816725_2_690x388.png" alt="Screenshot from 2022-10-20 13-42-43" data-base62-sha1="Aq3AEoYBqko3kVd8VJdUlF5Xi9n" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/f/ff4055a6b559ad57ec0e26e7853cfc3d64816725_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/f/ff4055a6b559ad57ec0e26e7853cfc3d64816725_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/f/ff4055a6b559ad57ec0e26e7853cfc3d64816725_2_1380x776.png 2x" data-dominant-color="ECEBEC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2022-10-20 13-42-43</span><span class="informations">1920×1080 268 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Chris_Paul
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/b/4ba670227e73fbb6b973078190beec76aea01482.png" data-download-href="https://discuss.pytorch.org/uploads/default/4ba670227e73fbb6b973078190beec76aea01482" title="Screenshot from 2022-10-20 13-42-59"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4ba670227e73fbb6b973078190beec76aea01482_2_690x388.png" alt="Screenshot from 2022-10-20 13-42-59" data-base62-sha1="aNeoVIc9ujA7mmMchzHuO0J3Hj4" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4ba670227e73fbb6b973078190beec76aea01482_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4ba670227e73fbb6b973078190beec76aea01482_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4ba670227e73fbb6b973078190beec76aea01482_2_1380x776.png 2x" data-dominant-color="EFEEEF"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2022-10-20 13-42-59</span><span class="informations">1920×1080 249 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Chris_Paul
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/6/86cfba8e4a97499c768f76e52ebcd0ad9e837d8b.png" data-download-href="https://discuss.pytorch.org/uploads/default/86cfba8e4a97499c768f76e52ebcd0ad9e837d8b" title="Screenshot from 2022-10-20 13-43-07"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/6/86cfba8e4a97499c768f76e52ebcd0ad9e837d8b_2_690x388.png" alt="Screenshot from 2022-10-20 13-43-07" data-base62-sha1="jeB2liGAZXqtdNVszhdJCijOuUz" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/6/86cfba8e4a97499c768f76e52ebcd0ad9e837d8b_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/6/86cfba8e4a97499c768f76e52ebcd0ad9e837d8b_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/6/86cfba8e4a97499c768f76e52ebcd0ad9e837d8b_2_1380x776.png 2x" data-dominant-color="F0EEF0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2022-10-20 13-43-07</span><span class="informations">1920×1080 245 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Chris_Paul
cooked: <p>Also i forgot to mention that I’m getting the same error when i tried to access .weight attribute</p>