fastai / course22p2

course.fast.ai 2022 part 2
https://course.fast.ai/Lessons/part2.html
Apache License 2.0
467 stars 252 forks source link

Fix grad assignment and `grad` property read #6

Closed Hofer-Julian closed 1 year ago

Hofer-Julian commented 1 year ago

Double assignment seems to do something different than expected in Python Changing it to two statements fixes it. This also reveals that the grad property is accessed at the wrong value.

review-notebook-app[bot] commented 1 year ago

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

PiotrCzapla commented 1 year ago

The issue is with map being a generator/iterator that you can use only once

>>> m = map(lambda x:x*10, [1,2,3])
>>> a,b,c = m
>>> test = m
>>> a, b, c
(10, 20, 30)
>>> test
<map object at 0x1046689a0>
>>> list(test)
[]

you can solve this by adding tuple in front of map

>>> test = a,b,c = tuple(map(str, [1,2,3]))
>>> test
('1', '2', '3')
>>> a, b, c
('1', '2', '3') 

But it is good catch, the double assignment is indeed quite problematic, as it doesn't work for some getitem implementations.

you can simplify the code a bit by saving to grads first, like so

ptgrads = w12,w22,b12,b22,xt2 = map(mkgrad, chks)

becomes:

ptgrads = tuple(map(mkgrad, chks))
w12,w22,b12,b22,xt2 = ptgrads
jph00 commented 1 year ago

Many thanks! I'll update it to the tuple version.