From 7a0607ee32812f8a871f9a8a57f61a01d3f72e16 Mon Sep 17 00:00:00 2001 From: Florian Richoux Date: Fri, 21 Apr 2017 10:44:53 +0200 Subject: [PATCH] Fix AttributeError in two_layer_net_autograd.py The line w1.grad.data.zero_() (previously line 55, now line 56) raised an "AttributeError: 'NoneType' object has no attribute 'data'". Indeed w1.grad and w2.grad were None at the first loop iteration. Fixed with the if at line 55. --- autograd/two_layer_net_autograd.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/autograd/two_layer_net_autograd.py b/autograd/two_layer_net_autograd.py index ad9f4fa..449e282 100644 --- a/autograd/two_layer_net_autograd.py +++ b/autograd/two_layer_net_autograd.py @@ -52,8 +52,9 @@ print(t, loss.data[0]) # Manually zero the gradients before running the backward pass - w1.grad.data.zero_() - w2.grad.data.zero_() + if w1.grad: + w1.grad.data.zero_() + w2.grad.data.zero_() # Use autograd to compute the backward pass. This call will compute the # gradient of loss with respect to all Variables with requires_grad=True.