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)
Error
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)
3
4 def accuracy(preds, targs):
----> 5 preds = torch.max(preds, dim=1)[1]
6 return (preds==targs).float().mean()
7

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)

(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


#6

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)

Yields:


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()
7
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)


#8

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.

Brian


(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


#10

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


(Dhiman Dey) #11

Hi,

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!


(Sudarshan) #14

I have the latest script but I am still getting that error.

metrics.py:

from .imports import *
from .torch_imports import *

def accuracy_np(preds, targs):
    preds = np.argmax(preds, 1)
    return (preds==targs).mean()

def accuracy(preds, targs):
    preds = torch.max(preds, dim=1)[1]
    return (preds==targs).float().mean()

def accuracy_thresh(thresh):
    return lambda preds,targs: accuracy_multi(preds, targs, thresh)

def accuracy_multi(preds, targs, thresh):
    return ((preds>thresh).float()==targs).float().mean()

def accuracy_multi_np(preds, targs, thresh):
    return ((preds>thresh)==targs).mean()

accuracy(log_preds, y)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-6e0f1447c02b> in <module>()
----> 1 accuracy(log_preds, y)

~/fastai/fastai/metrics.py in accuracy(preds, targs)
      7 
      8 def accuracy(preds, targs):
----> 9     preds = torch.max(preds, dim=1)[1]
     10     return (preds==targs).float().mean()
     11 

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)

Any ideas?


#15

This worked for me, thank you!


(Kiran Puri) #16

When running the line:

accuracy_np(log_preds, y), metrics.log_loss(y, probs)

I get the error:


AttributeError Traceback (most recent call last)
in ()
----> 1 accuracy_np(log_preds, y), metrics.log_loss(y, probs)

~/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()
7
8 def accuracy(preds, targs):

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

When looking at the code, it appears that preds==targs would yield a Boolean value of True or False, which doesn’t make sense to me to then use method .mean() on this… Can anyone explain this/help with this error?


#17

I pull the latest code in 20th April 2018,but I still get the following error:


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

~/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()
7
8 def accuracy(preds, targs):

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

So how can I change the type of (preds==targs) to ‘double’?


(Joao Ramos) #19

try using probs instead of log_preds in the accuracy_np function. Also, you should probably get the mean of probs, like in here


#20

Thanks a lot! It works!


(Flavia Souza Santos) #21

Nice! It worked.


Wiki: Lesson 3