repositori / bagas / micrograd

micrograd public ready

https://github.com/karpathy/micrograd commit 7bc720e 5 berkas · 10 potongan 2026-11-21T15:02:58Z
5berkas
4berkas python
10potongan
python 4 markdown 1
test/test_engine.pypython 73 baris ditampilkan
1
import torch
2
from micrograd.engine import Value
3
4
def test_sanity_check():
5
6
x = Value(-4.0)
7
z = 2 * x + 2 + x
8
q = z.relu() + z * x
9
h = (z * z).relu()
10
y = h + q + q * x
11
y.backward()
12
xmg, ymg = x, y
13
14
x = torch.Tensor([-4.0]).double()
15
x.requires_grad = True
16
z = 2 * x + 2 + x
17
q = z.relu() + z * x
18
h = (z * z).relu()
19
y = h + q + q * x
20
y.backward()
21
xpt, ypt = x, y
22
23
# forward pass went well
24
assert ymg.data == ypt.data.item()
25
# backward pass went well
26
assert xmg.grad == xpt.grad.item()
27
28
def test_more_ops():
29
30
a = Value(-4.0)
31
b = Value(2.0)
32
c = a + b
33
d = a * b + b**3
34
c += c + 1
35
c += 1 + c + (-a)
36
d += d * 2 + (b + a).relu()
37
d += 3 * d + (b - a).relu()
38
e = c - d
39
f = e**2
40
g = f / 2.0
41
g += 10.0 / f
42
g.backward()
43
amg, bmg, gmg = a, b, g
44
45
a = torch.Tensor([-4.0]).double()
46
b = torch.Tensor([2.0]).double()
47
a.requires_grad = True
48
b.requires_grad = True
49
c = a + b
50
d = a * b + b**3
51
c = c + c + 1
52
c = c + 1 + c + (-a)
53
d = d + d * 2 + (b + a).relu()
54
d = d + 3 * d + (b - a).relu()
55
e = c - d
56
f = e**2
57
g = f / 2.0
58
g = g + 10.0 / f
59
g.backward()
60
apt, bpt, gpt = a, b, g
61
62
tol = 1e-6
63
# forward pass went well
64
assert abs(gmg.data - gpt.data.item()) < tol
65
# backward pass went well
66
assert abs(amg.grad - apt.grad.item()) < tol
61
62
tol = 1e-6
63
# forward pass went well
64
assert abs(gmg.data - gpt.data.item()) < tol
65
# backward pass went well
66
assert abs(amg.grad - apt.grad.item()) < tol
67
assert abs(bmg.grad - bpt.grad.item()) < tol