-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a super basic test comparing our grad backward pass to that of py…
…torch
- Loading branch information
Showing
2 changed files
with
28 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import torch | ||
from micrograd.engine import Value | ||
from micrograd import nn | ||
|
||
def test_sanity_check(): | ||
|
||
x = Value(-4.0) | ||
z = 2 * x + 2 + x | ||
q = z.relu() + z * x | ||
h = (z * z).relu() | ||
y = h + q + q * x | ||
y.backward() | ||
xmg, ymg = x, y | ||
|
||
x = torch.Tensor([-4.0]) | ||
x.requires_grad = True | ||
z = 2 * x + 2 + x | ||
q = z.relu() + z * x | ||
h = (z * z).relu() | ||
y = h + q + q * x | ||
y.backward() | ||
xpt, ypt = x, y | ||
|
||
# forward pass went well | ||
assert ymg.data == ypt.data.item() | ||
# backward pass went well | ||
assert xmg.grad == xpt.grad.item() |