Classification Prediction using encoder of text_classifier_learner

Once a trained / fine tuned encoder is saved, what is the right way to load the encoder for prediction?

say a learner is defined as
leaner=text_classifier_learner(data_clas, drop_mult=0.5)

saved encoder was ‘fine_tuned_enc_1’

now doe we load the encoder by

learner.load_encoder(‘fine_tuned_enc_1’)

or

learner.load(‘fine_tuned_enc_1’) ???

1 Like

Once you have trained your classifier, you should save the model, so learner.save then learner.load.

Thanks @sgugger,

I am experimenting with lesson3: imdb notebook.
My language-model learner is trained and saved, the encoder is also saved (as ‘fine_tuned_enc’).
Now in the classification part of the notebook, where we create a text_classifier_learner, should I load ‘fine_tuned_enc’ using learner.load or leaner.load_encoder?
The notebook uses load_encoder initially but later after some freeze_to and trainings it uses learner.save and learner.load (not load_encoder). Hence the question.

Many thanks again

When you are initializing the weights of your classifier, you should use load_encoder to load the language model you fine-tuned. Later, once you have fitted your model, you should use load.

@sgugger,
Thank you sir,
If one uses load_decoder, the prediction results are terrible.
For example:
learn.predict(“It was awful,terribly worse than I could have imagined”)>>(Category neg, tensor(0), tensor([0.5442, 0.4558])) :joy::thinking:

Hi @sam2 I woul like to know once I have exported the model in the export.pkl file, what code should I use to load in a new python file the model to make a classification of one review?

I tried:

from fastai2.vision.all import *
from fastai2.vision.widgets import *
from utils import *
learn = load_learner(path/‘export.pkl’, cpu=True)
learn.path = path
learn.predict(“you suck”)

FileNotFoundError: [Errno 2] No such file or directory: ‘you suck’