hhsecond/HandsOnDeepLearningWithPytorch

Code in section 2 does not work

Closed this issue · 1 comments

in numpy_like_fizbuz.py:

Issue seems to be that you set dtype conditional on whether or not cuda is available. however, if cuda is available dtype is set to torch.cuda.FloatTensor
and the line:
w1 = torch.randn(input_size, hidden_size, requires_grad=True).type(dtype)
causes w1 to forget it is a user created variable and it loses the "requires_grad=True" attribute:

Here is an illustration. I am running pytorch 1.0 and python 3.6 (on a device with an attached gpu)

dtype
<class 'torch.cuda.FloatTensor'>
y = torch.zeros(1, 2, requires_grad=True).type(dtype)
y
tensor([[0., 0.]], device='cuda:0', grad_fn=)
y.requires_grad = True
Traceback (most recent call last):
File "", line 1, in
RuntimeError: you can only change requires_grad flags of leaf variables.
y = torch.zeros(1, 2).type(dtype)
y
tensor([[0., 0.]], device='cuda:0')
y.requires_grad = True
y
tensor([[0., 0.]], device='cuda:0', requires_grad=True)

Thanks a lot for bringing this up. I changed the code to use device and dtype separately instead of passing both information as part of dtype. Now torch will consider the user created weight and bias tensors as user created. Closing this issue. Feel free to re-open if you have further queries.