I don't find where the mistake is.Who can help me

haoxiansheng: <p>import torch.nn as nn</p>
<p>N, D_in, H, D_out = 64,1000,100,10</p>
<p><span class="hashtag">#随机创建一些训练数据</span><br>
x = torch.randn(N, D_in)<br>
y = torch.randn(N, D_out)</p>
<p>class TwoLayerNet(torch.nn.Module):<br>
def <strong>init</strong>(self, D_in, H, D_out):<br>
super(TwoLayerNet, self).<strong>init</strong>()<br>
self.Linear1 = torch.nn.Linear(D_in, H, bias=False)<br>
self.Linear2 = torch.nn.Linear(H, D_out, bias=False)</p>
<pre><code>def forward(self, x):
    y_pred = self.linear2(self.linear1(x).clamp(min=0))
    return y_pred
</code></pre>
<p>model = TwoLayerNet(D_in, H, D_out)<br>
loss_fn = nn.MSELoss(reduction=‘sum’)<br>
learning_rate = 1e-4<br>
optimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)</p>
<p>for it in range(500):<br>
# Forward pass<br>
y_pred = model(x)</p>
<pre><code># compute loss
loss = loss_fn(y_pred, y)  # computation graph
print(it, loss.item())

optimizer.zero_grad()
# Backward pass
loss.backward()

# update model parameters
optimizer.step()
</code></pre>
<hr>
<p>ModuleAttributeError                      Traceback (most recent call last)<br>
 in <br>
24 for it in range(500):<br>
25     # Forward pass<br>
—&gt; 26     y_pred = model(x)<br>
27<br>
28     # compute loss</p>
<p>E:\anaconda3\envs\sbw\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, x)<br>
14<br>
15     def forward(self, x):<br>
—&gt; 16         y_pred = self.linear2(self.linear1(x).clamp(min=0))<br>
17         return y_pred<br>
18</p>
<p>E:\anaconda3\envs\sbw\lib\site-packages\torch\nn\modules\module.py in <strong>getattr</strong>(self, name)<br>
776             if name in modules:<br>
777                 return modules[name]<br>
 → 778         raise ModuleAttributeError("’{}’ object has no attribute ‘{}’".format(<br>
779             type(self).<strong>name</strong>, name))<br>
780</p>
<p>ModuleAttributeError: ‘TwoLayerNet’ object has no attribute ‘linear2’</p>

aguennecjacq: <p>Can you please clean your code and explain what is your problem ?</p>

haoxiansheng: <p>I have solved it perfectly with someone’s help.Thank you.</p>

RaLo4: <p>On initialization you wrote it capitalized and later you didn’t:</p>
<aside class="quote no-group" data-username="haoxiansheng" data-post="1" data-topic="114850">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/a88e4f/48.png" class="avatar"> haoxiansheng:</div>
<blockquote>
<p>self.Linear2</p>
</blockquote>
</aside>
<aside class="quote no-group" data-username="haoxiansheng" data-post="1" data-topic="114850">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/a88e4f/48.png" class="avatar"> haoxiansheng:</div>
<blockquote>
<p><code>self.linear2</code></p>
</blockquote>
</aside>