From f4df18cc6cdd243b84ad33252a937bfd0c2b2a72 Mon Sep 17 00:00:00 2001 From: Alex Garvey <137834410+alex-garvey@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:03:19 -0500 Subject: [PATCH] Add test_backprop_shared_parent test_backprop_shared_parent makes sure that the backprop function adds gradients if there are contributions from multiple child nodes. --- .../exercises/part5_backprop/tests.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/chapter0_fundamentals/exercises/part5_backprop/tests.py b/chapter0_fundamentals/exercises/part5_backprop/tests.py index 30bb0c51..69baff9b 100644 --- a/chapter0_fundamentals/exercises/part5_backprop/tests.py +++ b/chapter0_fundamentals/exercises/part5_backprop/tests.py @@ -233,7 +233,6 @@ def test_backprop(Tensor): assert np.allclose(a.grad.array, 1 / b.array / a.array) print("All tests in `test_backprop` passed!") - def test_backprop_branching(Tensor): a = Tensor([1, 2, 3], requires_grad=True) b = Tensor([1, 2, 3], requires_grad=True) @@ -243,7 +242,6 @@ def test_backprop_branching(Tensor): assert np.allclose(b.grad.array, a.array) print("All tests in `test_backprop_branching` passed!") - def test_backprop_requires_grad_false(Tensor): a = Tensor([1, 2, 3], requires_grad=True) b = Tensor([1, 2, 3], requires_grad=False) @@ -253,7 +251,6 @@ def test_backprop_requires_grad_false(Tensor): assert b.grad is None print("All tests in `test_backprop_requires_grad_false` passed!") - def test_backprop_float_arg(Tensor): a = Tensor([1, 2, 3], requires_grad=True) b = 2 @@ -267,6 +264,19 @@ def test_backprop_float_arg(Tensor): assert np.allclose(a.grad.array, np.array([4.0, 4.0, 4.0])) print("All tests in `test_backprop_float_arg` passed!") +def test_backprop_shared_parent(Tensor): + a = 2 + b = Tensor([1, 2, 3], requires_grad=True) + c = 3 + d = a * b + e = b * c + f = d * e + f.backward(end_grad=np.array([1.0, 1.0, 1.0])) + assert f.grad is None + assert b.grad is not None + assert np.allclose(b.grad.array, np.array([12.0, 24.0, 36.0])), "Multiple nodes may have the same parent." + print("All tests in `test_backprop_shared_parent` passed!") + def test_negative_back(Tensor): a = Tensor([-1.0, 0.0, 1.0], requires_grad=True) b = -a