Having trouble deciding which Loss and Metric to use in my problem

My current dls is:

trn_path = path/'train_images'
dls = DataBlock(
                blocks = (ImageBlock, RegressionBlock, RegressionBlock, RegressionBlock, RegressionBlock, RegressionBlock, RegressionBlock),
                n_inp = 1,
                get_items = get_image_files,
                get_y = [get_X4_mean, get_X11_mean, get_X18_mean, get_X50_mean, get_X26_mean, get_X3112_mean],
                splitter = RandomSplitter(0.2, seed=42),
                item_tfms = Resize(256, method='squish'),
                batch_tfms = aug_transforms(size=128, min_scale=0.75)
).dataloaders(trn_path)

My batches look like this:

I tried different metrics and loss functions but got this error:

TypeError: L1Loss.__init__() takes from 1 to 4 positional arguments but 8 were given

Still learning myself, but I will try to help. What does your learner code look like? Did you try loss_func=F.cross_entropy?

That error looks like it’s trying to pass 4 arguments when it creates the L1Loss. Maybe try setting a breakpoint (import pdb; pdb.set_trace()) and stepping thru, sometimes that might help give some insight.

1 Like

The problem with using F.cross_entropy is that is for classification problems. Mine is a Regression. You have 6 values that you want to predict for each image.

learner = vision_learner(dls,"convnext_small_in22k",loss_func=L1LossFlat, n_out=6).to_fp16()

Your point on choice of loss function makes sense.

I remember watching a fast ai video that did multi prediction. The loss function needed to be customized, as you need to take the parameters and create a blended loss function. It was something like creating a loss function for each thing being predicted, then you create a macro function
combines them, and that’s what’s used. Unfortunately I couldn’t find which course lesson it was.

I think what you could do for now is create a custom function that takes 8 positional arguments, and pass that in as the loss function. Got that number from the error msg. Put a breakpoint inside and see what is passed in. I suspect it might be the different preds and targets, which is what would be your macro loss function, where you do the combining. During SGD, the loss needs to reflect all the things being predicted, so they can all improve.

Again, I am still learning, so sorry if this is not helpful

1 Like

It was lesson 7 of practical deep learning for coders 2022. Yeah, thanks! I will need to do a bit more research in that regard. Don’t worry everything helps!!