From 75f800ebce4f98c9252e71c0539982517e14d64b Mon Sep 17 00:00:00 2001 From: Lei Li Date: Thu, 2 May 2024 22:22:03 +0800 Subject: [PATCH] step18: fix AttributeError: NoneType object has no attribute generation --- steps/step18.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/steps/step18.py b/steps/step18.py index 4afae80..13959b2 100755 --- a/steps/step18.py +++ b/steps/step18.py @@ -52,7 +52,9 @@ def add_func(f): seen_set.add(f) funcs.sort(key=lambda x: x.generation) - add_func(self.creator) + + if self.creator is not None: # skip the first variable, whose creator is none + add_func(self.creator) while funcs: f = funcs.pop() @@ -148,4 +150,10 @@ def add(x0, x1): with no_grad(): x = Variable(np.array(2.0)) - y = square(x) \ No newline at end of file + y = square(x) + +with using_config('enable_backprop', False): + x = Variable(np.ones((100, 100, 100))) + y = square(square(square(x))) + y.backward() + print(x.grad) \ No newline at end of file