Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

[ASK] How to save and load finetuned model? #622

Open
lighteternal opened this issue Jun 2, 2021 · 3 comments
Open

[ASK] How to save and load finetuned model? #622

lighteternal opened this issue Jun 2, 2021 · 3 comments

Comments

@lighteternal
Copy link

Description

Hi, I am using one of the notebooks for sequence classification, finetuning a BERT variant. I noticed that the notebook instructions conclude when the model finishes training. I 'd like to ask if there's a straightforward way to save the finetuned model locally, and load it on another machine for inference on new data.

I have already managed to convert from txt/csv to df, convert the df to dataset and then create the dataloader for inference. However, I am unable to load the trained model. What I did to save the finetuned model is:
classifier.save_model("./trained_bert_base_classifier.bin")

and tried loading it (unsuccessfully) using the transformers Automodel, the torch.load and Transformer.load_model() from utils.
I would really appreciate some help on how to properly save and load a finetuned model using the existing recipes.

@lighteternal
Copy link
Author

lighteternal commented Jun 3, 2021

To answer my own question, what I did to load the finetuned checkpoint is the following:

from utils_nlp.models.transformers.sequence_classification import Processor, SequenceClassifier

MODEL_NAME = "bert-base-cased"
CACHE_DIR = '/local/path/where/base/model/will/be/downloaded/'

model = SequenceClassifier(model_name=MODEL_NAME, cache_dir=CACHE_DIR, num_labels=3)
model.load_model("trained/pytorch_model.bin")

The downside of this approach is that on every fresh run, the model is instantiated first using the head-less BERT, which needs to be downloaded or exist in the CACHE_DIR. Then, I use load_model to load the correct weights.

Is there a more elegant method to avoid downloading base-bert?

@poko1
Copy link

poko1 commented Jul 31, 2022

Hi, were you able to come up with a better solution? I am struggling to save and load the model as well.

@hiyamgh
Copy link

hiyamgh commented Aug 9, 2022

I think this solution is legendary: huggingface/transformers#2094

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants