Lesson1.ipynb - Error - TypeError: torch.max received an invalid combination of arguments - got (numpy.ndarray, dim=int)

(Anil Pandey) #1

Tried - Running the delivered code and getting error on step - accuracy(probs, y)
TypeError Traceback (most recent call last)
in ()
----> 1 accuracy(probs, y)

/mnt/data/ssd000/dsb2017/anil/fastai/courses/dl1/fastai/metrics.py in accuracy(preds, targs)
4 def accuracy(preds, targs):
----> 5 preds = torch.max(preds, dim=1)[1]
6 return (preds==targs).float().mean()

TypeError: torch.max received an invalid combination of arguments - got (numpy.ndarray, dim=int), but expected one of:

  • (torch.FloatTensor source)
  • (torch.FloatTensor source, torch.FloatTensor other)
    didn’t match because some of the keywords were incorrect: dim
  • (torch.FloatTensor source, int dim)
  • (torch.FloatTensor source, int dim, bool keepdim)

Kaggle Comp: Plant Seedlings Classification
(Raja Rupinder Singh) #2

Even I faced the same error with accuracy function yesterday. I think some update to this function behind the scene is causing this error. @jeremy kindly look into this error.

(Yash) #3

Change that line to accuracy_np(probs, y)

(YJ Park) #4

Thank you. It worked!

(Anil Pandey) #5

Thanks Yash accuracy_np(probs,y) works


I changed the line to “accuracy_np(probs, y)” and got a new error. Any ideas?
log_preds,y = learn.TTA()
probs = np.exp(log_preds)
accuracy_np(probs, y)


AttributeError Traceback (most recent call last)
in ()
1 log_preds,y = learn.TTA()
2 probs = np.exp(log_preds)
----> 3 accuracy_np(probs, y)

~/fastai/courses/dl1/fastai/metrics.py in accuracy_np(preds, targs)
4 def accuracy_np(preds, targs):
5 preds = np.argmax(preds, 1)
----> 6 return (preds==targs).mean()
8 def accuracy(preds, targs):

AttributeError: ‘bool’ object has no attribute ‘mean’

(Anil Pandey) #7

Yes. Get updated metric.py and make sure it has the following code
def accuracy_np(preds, targs):
preds = np.argmax(preds, 1)
return (preds==targs).mean()

Use the following code
log_preds,y = learn.TTA()
probs = np.mean(np.exp(log_preds),0)
accuracy_np(probs, y)


Is that metric.py as in: https://github.com/apache/incubator-mxnet/blob/master/python/mxnet/metric.py

or metrics.py as in: https://pypi.python.org/pypi/metrics/0.2.8

The latter is in the fastai directory and I can update but the 1st one looks to be a different file.

Thanks for your help.


(Anil Pandey) #9

Sorry for typo in file name earlier. It is metrics.py. Here is the path https://github.com/fastai/fastai/tree/master/fastai/metrics.py


np…I pip installed it and am running it now. Thanks.

(Dhiman Dey) #11


I am getting following error:

NameError                                 Traceback (most recent call last)
<ipython-input-73-98448a3af0e1> in <module>()
----> 1 accuracy_np(probs, y)

NameError: name 'accuracy_np' is not defined

Any idea?

(nok) #12

Make sure you have the latest script. It should be in the latest repo. Try do a git pull.

(Jan Van de Poel) #13

Thanks, that did the trick!