There is an error in the code for chapter 2 with title building a neural network using pytorch on a toy dataset
abdelkareemkobo opened this issue · comments
I run the code in my editor and get some errors.
in the update loop we must convert the loss value into item object not a tensor so it raises an error
loss_history.append(loss_value.item()) not
loss_history.append(loss_value)
python loss_history = [] for _ in range(50): opt.zero_grad() loss_value = loss_func(mynet(X),Y) loss_value.backward() opt.step() loss_history.append(loss_value.item())
this is the modified code and it works fine and every thing is fine!
or you can use this code loss_history = [v.item() for v in loss_history]
try loss_history.append(loss.detach().cpu().numpy())
detach() -> cuts of the computational graph
cpu()-> makes memory into RAM
numpy() -> converts tensor to variable