पाठ 17 / 33

Saving and Sharing Your Models

यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।

Save a model locally with save_pretrained and publish it to a private or public Hub repo.

लेखक: · 15 साल से वेब डेवलपर

save_pretrained and push_to_hub

After fine-tuning, call model.save_pretrained(path) and tokenizer.save_pretrained(path) to write the config, weights and tokenizer files into a folder. That folder is itself a valid checkpoint: from_pretrained(path) loads it back exactly as it loads a Hub id, which is the simplest way to move a model between a training machine and a server. To share it, push_to_hub("your-username/model-name") creates the repository if needed and uploads the files; pass private=True to keep it visible only to you or your organisation. Saving the tokenizer next to the model is not optional: the ids are meaningless without it. Write a model card that states the base model, the training data, how you evaluated it and known limitations, because future you and your teammates will not remember. For large files, uploads are chunked and resumable, and the Hub keeps every version, so you can always roll back to an older commit.

Save, reload and publish

Continues from a training run where model and tokenizer are already loaded. A local folder and a Hub repo id are interchangeable in from_pretrained; pushing needs a token with write access.

from transformers import AutoTokenizer, AutoModelForSequenceClassification

save_dir = "./sentiment-hinglish-v1"
model.save_pretrained(save_dir)        # config.json + model.safetensors
tokenizer.save_pretrained(save_dir)    # tokenizer files

reloaded = AutoModelForSequenceClassification.from_pretrained(save_dir)
tok2 = AutoTokenizer.from_pretrained(save_dir)

# Upload both to a private repo under your account
model.push_to_hub("your-username/sentiment-hinglish", private=True)
tokenizer.push_to_hub("your-username/sentiment-hinglish", private=True)

A lab record with its legend

Saving weights without the tokenizer is like submitting a lab record full of readings but no legend for the columns. The numbers are all there, but nobody, including the examiner, can interpret them.

त्वरित जाँच: A teammate loads your saved folder but gets garbage predictions. The weights loaded without error. What is the most likely mistake?

  • The folder name has a hyphen
  • You used safetensors instead of pickle
  • The model was saved in eval mode
  • They used a different tokenizer than the one the model was trained with
Answer

They used a different tokenizer than the one the model was trained with — Ids only make sense with the matching vocabulary. Always save and load the tokenizer together with the model.