diff --git a/micrograd/engine.py b/micrograd/engine.py index cfc95bc..e8dd32c 100644 --- a/micrograd/engine.py +++ b/micrograd/engine.py @@ -34,8 +34,8 @@ class Value: _children=(self, other), _op='+') def _backward(): - self.grad = 1.0 * out.grad - other.grad = 1.0 * out.grad + self.grad += 1.0 * out.grad + other.grad += 1.0 * out.grad out._backward = _backward return out @@ -49,8 +49,8 @@ class Value: _children=(self, other), _op='*') def _backward(): - self.grad = other.data * out.grad - other.grad = self.data * out.grad + self.grad += other.data * out.grad + other.grad += self.data * out.grad out._backward = _backward return out @@ -61,6 +61,6 @@ class Value: _op='tanh', label='tanh') def _backward(): - self.grad = (1 - t**2) * out.grad + self.grad += (1 - t**2) * out.grad out._backward = _backward return out diff --git a/test/test_backprop.py b/test/test_backprop.py index 829ee18..2e38902 100644 --- a/test/test_backprop.py +++ b/test/test_backprop.py @@ -61,7 +61,6 @@ def test_large_backprop(): assert pytest.approx(w2.grad, 0.001) == 0.0 -@pytest.mark.skip(reason="non-deterministic") def test_auto_diff(): # inputs x1 = Value(2.0, label='x1') @@ -89,7 +88,6 @@ def test_auto_diff(): y.backward() assert pytest.approx(n.grad, 0.001) == 0.5 - assert h.grad == 0.0 assert pytest.approx(b.grad, 0.001) == 0.5 assert pytest.approx(h.grad, 0.001) == 0.5 @@ -102,3 +100,10 @@ def test_auto_diff(): assert pytest.approx(x2.grad, 0.001) == 0.5 assert pytest.approx(w2.grad, 0.001) == 0.0 + + +def test_accumulation(): + a = Value(2.0, label='x1') + b = a + a + b.backward() + assert a.grad == 2.0