Properly loading a fine tuned model from directory

Hi everybody,

After reading the docs I still don’t really understand how should I load my saved model properly.

I fine tuned a CamembertForSequenceClassification.from_pretrained model with some data, the results was good so I saved it using save_pretrained(model_path). Now I would like to use this model to do inference… I use these lines of code:

config = AutoConfig.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_config(config)

I now have several interogation:

  1. If I tokenize a new sequence with tokenizer.encode_plus(new_seq), it doesn’t tokenize the sentence the way it did when I previously saved the model. I must precise all the params (max_length…) to retrieve what I want. Is that normal?
  2. If I load my saved model without using AutoModelForSequenceClassification but with AutoModel the model do not load a classification layer however I saved my model using this last layer. Is that also normal?

So I’m wondering how can I be sure that I load the exact same model with all the weights and params that I saved previously.

Thanks for everything! :slight_smile:

  1. Yes, you should pass the same params to tokenizer that you passed during training.
  2. AutoModel won’t add the heads (classfication layer, LM head etc). Use proper AutoModel classes to get same weights and params i.e AutoModelForSequenceClassification for classification model, AutoModelForCausalLM for causal LM’s like GPT-2 etc

You can load the saved model using just AutoModelForSequenceClassification.from_pretrained, it’ll load the config itself. You should pass config explicitly when you want to change something in it.

1 Like

Ok that’s clear, thank you very much for your answer.