Using my own model as a CNN learner

I have my own pytorch model and I want to use it as a fastai learner. How do I do this?

Based on what I understand from the docs, the cnn_learner method uses the passed model as a base architecture and adds a head to it. I don’t want it to happen. Also, I have a custom loss function I’d like to use to train the model.

1 Like

You’d want to use Learner() and pass your architecture in as arch and your loss function for loss_func

You want to create a Learner object. The most basic way to do that is to do

learn = learner(data, model, loss_func = loss_func)

data should be a DataBunch, that feeds data to your model.
Your model should then process that data and return a prediction vector which will be used with the loss_func and a target vector to compute the loss. You can use the data_block API to construct the DataBunch. If your models takes multiple input in its forward pass, your collate_fn will have to stack them in the form ((x1, x2, x3, ...), y) where x1, x2, x3, … are the different arguments of the forward
method of your model, if not, either x, y or [x], y are fine and most likely you won’t have to touch the collate_fn at all.

1 Like

Thank you. Got it working.

What is the default loss function for multi class classification data.?
learn.loss_func gives FlattenedLoss of CrossEntropyLoss().
I want load a pytorch model trained for 1000 classes and fine tune it for 5 classes.
As suggested I am using learn = learner(data, model, loss_func = loss_func).
But what is the loss_func to be passed?

What’s wrong with CrossEntropy?