-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding comments and updates to pytorch :)
- Loading branch information
1 parent
fb9b279
commit 2d40855
Showing
3 changed files
with
28 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,34 @@ | ||
import torch | ||
from torch.autograd import Variable | ||
import pdb | ||
|
||
x_data = [1.0, 2.0, 3.0] | ||
y_data = [2.0, 4.0, 6.0] | ||
|
||
w = Variable(torch.Tensor([1.0]), requires_grad=True) # Any random value | ||
w = torch.tensor([1.0], requires_grad=True) | ||
|
||
# our model forward pass | ||
|
||
|
||
def forward(x): | ||
return x * w | ||
|
||
# Loss function | ||
|
||
|
||
def loss(x, y): | ||
y_pred = forward(x) | ||
return (y_pred - y) * (y_pred - y) | ||
def loss(y_pred, y_val): | ||
return (y_pred - y_val) ** 2 | ||
|
||
# Before training | ||
print("predict (before training)", 4, forward(4).data[0]) | ||
print("Prediction (before training)", 4, forward(4).item()) | ||
|
||
# Training loop | ||
for epoch in range(10): | ||
for x_val, y_val in zip(x_data, y_data): | ||
l = loss(x_val, y_val) | ||
l.backward() | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Hammania689
Author
Contributor
|
||
print("\tgrad: ", x_val, y_val, w.grad.data[0]) | ||
w.data = w.data - 0.01 * w.grad.data | ||
y_pred = forward(x_val) # 1) Forward pass | ||
l = loss(y_pred, y_val) # 2) Compute loss | ||
l.backward() # 3) Back propagation to update weights | ||
print("\tgrad: ", x_val, y_val, w.grad.item()) | ||
w.data = w.data - 0.01 * w.grad.item() | ||
|
||
# Manually zero the gradients after updating weights | ||
w.grad.data.zero_() | ||
|
||
print("progress:", epoch, l.data[0]) | ||
print(f"Epoch: {epoch} | Loss: {l.item()}") | ||
|
||
# After training | ||
print("predict (after training)", 4, forward(4).data[0]) | ||
print("Prediction (after training)", 4, forward(4).item()) |
Hi, using backward here give me an error saying "'float' object has no attribute 'backward'"
can you pls inform me on this, cant find any relevant material elsewhere.