RuntimeError: Expected object of scalar type Float but got scalar type Long for argument #2 'other'

Hello
When I am running below lines for my image classification problem getting error as mentioned :
interp.plot_confusion_matrix()

RuntimeError Traceback (most recent call last)
in ()
----> 1 interp.plot_confusion_matrix()

1 frames
/usr/local/lib/python3.6/dist-packages/fastai/train.py in confusion_matrix(self, slice_size)
155 for i in range(0, self.y_true.shape[0], slice_size):
156 cm_slice = ((self.pred_class[i:i+slice_size]==x[:,None])
–> 157 & (self.y_true[i:i+slice_size]==x[:,None,None])).sum(2)
158 torch.add(cm, cm_slice, out=cm)
159 return to_np(cm)

RuntimeError: Expected object of scalar type Float but got scalar type Long for argument #2 ‘other’

Good morning, I’m experiencing the same problem, did you find the solution?

Thank you very much :slight_smile:

Hi , Even I am seeing the same error , did you find any resolution for this? If yes please share, thanks in advance

1 Like

nop, I changed the project I was making and now theres no error.

However, dont nkow nothing about what that error is…

srry

np, thanks for the reply

Hi, I stumbled sometimes with this error, I don’t know what kind of data set you are trying to plot the CM, but what happened to me was that when I tried to plot CM with a multi-label data set I got this error. I’m not sure that you can have a confusion matrix for multi-label data sets.

1 Like

Hi Felipe,

I am not using a multi label data set , below is the code I tried, if you dont mind can you please provide any code snippets you tried.

I am using Kaggle competition data for blindness detection [https://www.kaggle.com/c/aptos2019-blindness-detection]

np.random.seed(42)
src = (ImageList.from_csv(path, ‘train.csv’, folder=‘train_images’, suffix=’.png’)
.split_by_rand_pct(0.2)
.label_from_df())
data = (src.transform(tfms, size=128)
.databunch().normalize(imagenet_stats))

def accuracy_01(input:Tensor, targs:Tensor)->Rank0Tensor:
“Computes accuracy with targs when input is bs * n_classes.”
targs = targs.view(-1).long()
n = targs.shape[0]
input = input.argmax(dim=-1).view(n,-1)
targs = targs.view(n,-1)
return (input==targs).float().mean()

acc_02 = partial(accuracy_thresh, thresh=0.2)
f_score = partial(fbeta, thresh=0.2)
#learn = cnn_learner(data, arch, metrics=[acc_02, f_score],callback_fns=ShowGraph) . #failing with Conf Mat
#learn = cnn_learner(data, arch, metrics=[error_rate],callback_fns=ShowGraph) . #failing with Conf Mat
#learn = cnn_learner(data, arch, metrics=[accuracy_01]) #,callback_fns=ShowGraph) #failing with training
learn = cnn_learner(data, arch, metrics=[accuracy_thresh]) #failing with training [ ]