AssertionError: Expected output and target to have the same number of elements but got 160 and 80

Hi there!

This is my first post here so might be in the wrong topic… I’m trying to build a custom CNN which will classify spectras in the form of .npy vectors to 2 classes. I run into this AssertionError when trying to run learn.fit.one_cycle and have scouted different forums but don’t seem to find this error for classification issues anywhere. I should mention learn.lr_find and learn.pred_batch() works fine.

I’ve previously done a regression problem for calculating molecular mass from the spectras without problems but now when I change to classification the error occurs. The error only appears when the last layer in my CNN is dim 2 (as in my number of classes) but does not appear when dim 1 (as in regression). The mismatch is due to the batch size (80) being multiplied by the number of classes (2*80=160), which is probably why it works when last layer is dim 1.

One of the reasons it doesn’t work may be simply that I’m using .npy vectors with an added dimention to fit fastai (samples of shape (1,153000)) and not the typical format of an image (i.e (3,H,W). However as mentioned everything works fine in regression which leads me to beleive there’s another problem.

I know this is a very specific problem but I’m very grateful for any answers!

welcome!

excuse what may be a patronising question but have you changed to binary cross entropy loss?

Thank you! I’m a beginner so no harm done haha. Well I’ve tried using both CrossEntropy functions yielding:

BCELoss: Following error occurs in learn.lr_find
ValueError: Target and input must have the same number of elements. target nelement (80) != input nelement (160)

CELoss: Following error occurs in learn.fit.one_cycle
AssertionError: Expected output and target to have the same number of elements but got 160 and 80.

If I understand it correctly BCE should be used for this problem with 2 classes.

I’ve also tried a multi classification problem using 8 classes and CELoss, but here the same AssertionError is received and yielding a mismatch of 8*batch_size…

not sure, i don’t plug custom NNs together myself but with the factory methods, how you create your databunch implies to fastai whether you want a single or multi label classifier. check whether you end up with a CategoryList or a MultiCategoryList.

other than that and double checking that you’re using CE with softmax and BCE with sigmoid, i’ve got no idea and you’ll have to wait for a grown-up to come along. sorry.

I see that the labels become a category list by default, but that should be fine even though it’s a multiclassification problem right? Anyway when I print data.classes() all classes show up as they should. And in the first problem where there’s only 2 classes it should be a CategoryList anyway but still error…

Thanks for taking your time!

your number of classes doesn’t matter, CategoryList is for 1 prediction from N classes. MultiCategoryList is for N predictions from K classes, eg: this photo contains a boat and a car and a person.

Okay didn’t know that! But then a CategoryList should be fine for now but good to know for further analysis of the spectras when this error is solved

Well turns out the problem was only due to the metrics. I had MSE displayed, which I get is a bad errortype for classification, but that it would cause that error is strange to me… Anyway, works fine now when MSE is removed

1 Like