In my understanding a context manager is designed to make sure that the resource is reclaimed at the end of the scope of the context. Whereas, in lesson6-pets-more.ipynb we actually return the object which should normally be destroyed by the time we get to return():
def hooked_backward(cat=y):
with hook_output(m[0]) as hook_a:
with hook_output(m[0], grad=True) as hook_g:
preds = m(xb)
preds[0,int(cat)].backward()
return hook_a,hook_g
That feels very unintuitive. What am I missing?
Thanks.