Reputation: 45
I got the following error using a pretrained ResNet50 in PyTorch:
RuntimeError
Traceback (most recent call last)
<ipython-input-14-8f0d0641ef12> in <module>()
28 # Update parameters
29 optimizer.zero_grad()
---> 30 loss.backward()
31 optimizer.step()
32
1 frames
/usr/local/lib/python3.6/dist-packages/torch/autograd/__init__.py in
backward(tensors, grad_tensors, retain_graph, create_graph,
grad_variables)
98 Variable._execution_engine.run_backward(
99 tensors, grad_tensors, retain_graph, create_graph,
--> 100 allow_unreachable=True) # allow_unreachable flag
101
102
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
Notebook is in this link: https://colab.research.google.com/drive/1k40NNulSIS6ANagopSPBH4Xty_Cw39qC?usp=sharing
Upvotes: 1
Views: 387
Reputation: 13601
The problem is that you're setting a new attribute model.classifier
, while you actually want to replace the current "classifier", i.e., change the model.fc
.
It is beyond the scope of your question, but you'll find another problem later on. Your new classifier has a LogSoftmax()
module and you're using the nn.CrossEntropyLoss()
. As you can see here, you should not do this.
Upvotes: 1