1

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

jccarrasco
  • 45
  • 9

1 Answers1

1

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.

Berriel
  • 12,659
  • 4
  • 43
  • 67
  • Thank you very much for your help. It's true that LogSoftmax() should not be used with nn.CrossEntropyLoss(). It was included in a Udemy Course code but Udacity's is correct. I will take that into account. Regarding your answer, I would really appreciate an example because it is not clear for me. Sorry for the inconvenience. – jccarrasco Jun 19 '20 at 10:41
  • @jccarrasco My answer basically says that you have to change model.classifier to model.fc in your code. Consider upvoting if you find this answer helpful. – Berriel Jun 19 '20 at 11:11
  • I already did but regrettably I get this message: Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score. – jccarrasco Jun 19 '20 at 11:21
  • Thanks for your help. It worked however now I get this error: RuntimeError: size mismatch, m1: [30 x 2048], m2: [9216 x 1024] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:283 – jccarrasco Jun 19 '20 at 11:53
  • @jccarrasco this is because you have set the first linear of the classifier with 9216 units, and it should have 2048 instead. print the model before changing the fc and you'll see that it originally has 2048 units – Berriel Jun 19 '20 at 13:21
  • Thanks! I already did and got this: Linear(in_features=2048, out_features=1000, bias=True). How can I change it to be used for CIFAR10? I tried this: model.fc = nn.Sequential(nn.Linear(2048,1000), nn.ReLU(),nn.Dropout(0.4),nn.Linear(1000,10)) and got this: IndexError: index 42549 is out of bounds for axis 0 with size 10000. Thanks for your time @berriel, I am really grateful for it. – jccarrasco Jun 19 '20 at 14:49
  • @jccarrasco not sure why and where you'd get this error... perhaps you could open another question with the necessary info. Also, avoid colab links in the next questions: always post the code in the question itself. – Berriel Jun 19 '20 at 15:38
  • thanks for your help and kindness. I will open another question if I cannot solve it. – jccarrasco Jun 19 '20 at 16:35