Skip to content

3.3 节最后的损失函数需要sum后才可以调用backward函数 #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/chapter03_DL-basics/3.3_linear-regression-pytorch.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tensor([[-2.7723, -0.6627],
[ 0.2576, -0.2928],
[ 2.0475, -2.7440],
[ 1.0685, 1.1920],
[ 1.0996, 0.5106]])
[ 1.0996, 0.5106]])
tensor([ 0.9066, -0.6247, 9.3383, 3.6537, 3.1283, 17.0213, 5.6953, 17.6279,
2.2809, 4.6661])
```
Expand All @@ -68,7 +68,7 @@ class LinearNet(nn.Module):
def forward(self, x):
y = self.linear(x)
return y

net = LinearNet(num_inputs)
print(net) # 使用print可以打印出网络的结构
```
Expand Down Expand Up @@ -195,7 +195,7 @@ num_epochs = 3
for epoch in range(1, num_epochs + 1):
for X, y in data_iter:
output = net(X)
l = loss(output, y.view(-1, 1))
l = loss(output, y.view(-1, 1)).sum()
optimizer.zero_grad() # 梯度清零,等价于net.zero_grad()
l.backward()
optimizer.step()
Expand Down