# How to save, load and use my text classification model?

**URL:** <https://discuss.huggingface.co/t/how-to-save-load-and-use-my-text-classification-model/8837>\
**Category:** Beginners\
**Created:** [July 30, 2021, 11:43am UTC](https://discuss.huggingface.co/t/how-to-save-load-and-use-my-text-classification-model/8837 "2021-07-30T11:43:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon58275033](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@anon58275033](https://discuss.huggingface.co/u/anon58275033)\
**Post date:** [July 30, 2021, 11:43am UTC](https://discuss.huggingface.co/t/how-to-save-load-and-use-my-text-classification-model/8837/1 "2021-07-30T11:43:34Z")

</div>

Hi,

I have followed this text classification tutorial: [notebooks/text\_classification.ipynb at master · huggingface/notebooks · GitHub](https://github.com/huggingface/notebooks/blob/master/examples/text_classification.ipynb)

However, the tutorial does not show how to save, load and use my text classification model.

Any help on how to use my saved model for classifying a sentence?

Thanks!

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [July 30, 2021, 12:57pm UTC](https://discuss.huggingface.co/t/how-to-save-load-and-use-my-text-classification-model/8837/2 "2021-07-30T12:57:42Z")

</div>

Once you have pushed your model to the hub, you can use it like any other model, in a pipeline or directly with the `from_pretrained` method.

---

<div class="post-metadata">

**Author:** ![janyfe](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@janyfe](https://discuss.huggingface.co/u/janyfe)\
**Post date:** [July 30, 2021, 2:06pm UTC](https://discuss.huggingface.co/t/how-to-save-load-and-use-my-text-classification-model/8837/3 "2021-07-30T14:06:11Z")

</div>

You can save your model and also tokenizer vocabluary as simple as

```auto
model.save_pretrained(model_checkpoint_path)
tokenizer.save_vocabulary(model_checkpoint_path)

```

or just like any other PyTorch model, but using appropriate names (and save the model’s config as well) so as to be able to load your saved checkpoint with from\_pretrained method

```auto
import torch
from transformers import WEIGHTS_NAME, CONFIG_NAME
from pathlib import Path

if isinstance(model_checkpoint_path, str):
    model_checkpoint_path = Path(model_checkpoint_path)

torch.save(model.state_dict(), model_checkpoint_path/WEIGHTS_NAME)
model.config.to_json_file(model_checkpoint_path/CONFIG_NAME)
tokenizer.save_vocabulary(model_checkpoint_path)

```

loading model

```auto
from transformers import AutoModelForSequenceClassification, AutoTokenizer

model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint_path, num_labels=num_labels)
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint_path)

```

using model

```auto
inputs = ['example text number 1', 'another example',]
enc = tokenizer(inputs)
model.eval();
with torch.no_grad():
    output = model(**enc)
    _, pred = torch.max(output[0], dim=1)

```
