auto gfrad test added

This commit is contained in:
Nicolas Kruse 2025-12-04 18:19:43 +01:00
parent 5daa54fafb
commit 257fe96bb3
1 changed files with 38 additions and 0 deletions

38
tests/test_autograd.py Normal file
View File

@ -0,0 +1,38 @@
from copapy import variable, grad
import copapy as cp
import pytest
def test_autograd():
# Validate against micrograd results from Andrej Karpathy
# https://github.com/karpathy/micrograd/blob/master/test/test_engine.py
a = variable(-4.0)
b = variable(2.0)
c = a + b
d = a * b + b**3
c += c + 1
c += 1 + c + (-a)
d += d * 2 + cp.relu(b + a)
d += 3 * d + cp.relu(b - a)
e = c - d
f = e**2
g = f / 2.0
g += 10.0 / f
dg = grad(g, (a, b))
tg = cp.Target()
tg.compile(g, dg)
tg.run()
print(f"g = {tg.read_value(g)}")
print(f"dg/da = {tg.read_value(dg[0])} grad:{dg[0]} val:{a} = {tg.read_value(a)}")
print(f"dg/db = {tg.read_value(dg[1])} grad:{dg[1]} val:{b} = {tg.read_value(b)}")
assert pytest.approx(dg[0], abs=1e-4) == 138.83381 # pyright: ignore[reportUnknownMemberType]
assert pytest.approx(dg[1], abs=1e-4) == 645.57725 # pyright: ignore[reportUnknownMemberType]
if __name__ == "__main__":
test_autograd()