Modifying resnet number of output classes for regression

For some classification problems it can make sense to structure it as a regression problem. To do this with fastai, I’m trying to set num_classes = 1, and changing my loss to MSE.

I tried passing in num_classes as kwargs to cnn_learner with resnet34 pretrained network but it isn’t a kwargs argument. I also tried this:

learn = cnn_learner(data, models.resnet34(num_classes=1), pretrained=True, 
                callback_fns=[partial(CSVLogger, append=True)], 
                path='../tmp/model/', 
                loss_func=MSELoss())

but you cannot instantiate the resnet model before, hence it does not work.

I figured out a possible solution while I was typing that:

learn = Learner(data, models.resnet34(num_classes=1), loss_func=MSELoss())

This seems to work. The one concern I had was that the output will be passed through a sigmoid or a softmax, but I looked through the resnet code and I don’t believe it’s there

Fastai decides the number of classes and the loss function to use (classification vs. regression) based on the DataBunch, not on the Learner. Check out the docs and forum posts for “FloatList”.