# How to save and load fine-tune model

**URL:** <https://discuss.huggingface.co/t/how-to-save-and-load-fine-tune-model/1595>\
**Category:** 🤗Transformers\
**Created:** [October 17, 2020, 4:31am UTC](https://discuss.huggingface.co/t/how-to-save-and-load-fine-tune-model/1595 "2020-10-17T04:31:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathor](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mathor/32/480_2.png) [@mathor](https://discuss.huggingface.co/u/mathor)\
**Post date:** [October 17, 2020, 4:31am UTC](https://discuss.huggingface.co/t/how-to-save-and-load-fine-tune-model/1595/1 "2020-10-17T04:31:12Z")

</div>

Hi, everyone~ I have defined my model via huggingface, but I don’t know how to save and load the model, hopefully someone can help me out, thanks!

```
class MyModel(nn.Module):
  def __init__ (self, num_classes):
    super(MyModel, self). __init__ ()
    self.bert = BertModel.from_pretrained('hfl/chinese-roberta-wwm-ext', return_dict=True).to(device)
    self.fc = nn.Linear(768, num_classes, bias=False)
  
  def forward(self, x_input_ids, x_type_ids, attn_mask):
    outputs = self.bert(x_input_ids, token_type_ids=x_type_ids, attention_mask=attn_mask)
    pred = self.fc(outputs.pooler_output)
    return pred

model = MyModel(num_classes).to(device)
# save 

# load
```

---

<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:** [October 19, 2020, 3:03pm UTC](https://discuss.huggingface.co/t/how-to-save-and-load-fine-tune-model/1595/2 "2020-10-19T15:03:45Z")

</div>

If you make your model a subclass of `PreTrainedModel`, then you can use our methods `save_pretrained` and `from_pretrained`. Otherwise it’s regular PyTorch code to save and load (using `torch.save` and `torch.load`).

---

<div class="post-metadata">

**Author:** ![Tushar-Faroque](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/tushar-faroque/32/2277_2.png) [@Tushar-Faroque](https://discuss.huggingface.co/u/Tushar-Faroque)\
**Post date:** [July 14, 2021, 2:06pm UTC](https://discuss.huggingface.co/t/how-to-save-and-load-fine-tune-model/1595/3 "2021-07-14T14:06:28Z")

</div>

What if the pre-trained model is saved by using `torch.save(model.state_dict())`. How can I use that model like the `BertTokenizer` for creating tokens and also embeddings?

---

<div class="post-metadata">

**Author:** ![milesc](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@milesc](https://discuss.huggingface.co/u/milesc)\
**Post date:** [August 12, 2021, 8:53am UTC](https://discuss.huggingface.co/t/how-to-save-and-load-fine-tune-model/1595/4 "2021-08-12T08:53:09Z")

</div>

Hi,  
Is there a difference between loading a model via torch.load and using from\_pretrained in terms of downstream tasks?  
Does either method have an advantage over the other for fine-tuning or inference?

---

<div class="post-metadata">

**Author:** ![vpkprasanna](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/vpkprasanna/32/26436_2.png) [@vpkprasanna](https://discuss.huggingface.co/u/vpkprasanna)\
**Post date:** [October 25, 2021, 7:48am UTC](https://discuss.huggingface.co/t/how-to-save-and-load-fine-tune-model/1595/5 "2021-10-25T07:48:14Z")

</div>

If i do torch.save it will save only the model file but it won’t save the config.json file . How to achieve that using torch.save method
