pandeyaah
(Anil Pandey)
January 31, 2018, 9:50pm
#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)
2 Likes

raja4net
(Raja Rupinder Singh)
February 2, 2018, 9:15am
#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
(Yash)
February 2, 2018, 8:25pm
#3
Change that line to `accuracy_np(probs, y)`

14 Likes

pandeyaah
(Anil Pandey)
February 6, 2018, 6:38pm
#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)

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â

1 Like

pandeyaah
(Anil Pandey)
February 7, 2018, 5:15am
#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)

6 Likes

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

pandeyaah
(Anil Pandey)
February 7, 2018, 7:32pm
#9

npâŚI pip installed it and am running it now. Thanks.

dhiman63
(Dhiman Dey)
February 16, 2018, 5:12am
#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
(nok)
February 20, 2018, 11:08am
#12
```
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()
```

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

Thanks, that did the trick!

shaun1
(Sudarshan)
March 14, 2018, 12:44pm
#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?

This worked for me, thank you!

kiran.puri
(Kiran Puri)
April 18, 2018, 3:40pm
#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?

1 Like

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â?

jpramos
(Joao Ramos)
April 20, 2018, 10:32am
#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

1 Like