# How to load a pretrained custom model using \`from\_pretrained\`

**URL:** <https://discuss.huggingface.co/t/how-to-load-a-pretrained-custom-model-using-from-pretrained/29927>\
**Category:** Beginners\
**Created:** [January 18, 2023, 6:28am UTC](https://discuss.huggingface.co/t/how-to-load-a-pretrained-custom-model-using-from-pretrained/29927 "2023-01-18T06:28:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nomorewords](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@nomorewords](https://discuss.huggingface.co/u/nomorewords)\
**Post date:** [January 18, 2023, 6:28am UTC](https://discuss.huggingface.co/t/how-to-load-a-pretrained-custom-model-using-from-pretrained/29927/1 "2023-01-18T06:28:14Z")

</div>

Hi there,

I wanted to create a custom model that includes a transformer and save it using the `save_pretrained` function after training for a few epochs. I would then want to load it in a different notebook using the `from_pretrained` function for inference.

Suppose I follow this [guide](https://huggingface.co/docs/transformers/custom_models) and created a custom model named `CustomModel` with something like:

```python
class CustomModel(PreTrainedModel):
     def __init__ (self, config, transformer_model_name, n_dims=1000, n_factors=50, n_classes=10):
        super(). __init__ (config)
        self.embs = nn.Embedding(n_dims, n_factors)
        self.text_transformer = AutoModelForSequenceClassification.from_pretrained(
            transformer_model_name, num_labels=512)
        self.linear_layers = nn.Sequential(
            nn.Linear(n_factors+512, 256, bias=False),
            nn.LeakyReLU(),
            nn.BatchNorm1d(256),
            nn.Dropout(),
            nn.Linear(256, n_classes)
        )

```

Suppose I have already trained and saved the model, can I later use `CustomModel.from_pretrained(model_dir)` to load the trained model in a different notebook? I tried something along these lines, but got an `AttributeError: 'NoneType' object has no attribute 'from_pretrained'` error when I called `from_pretrained`.

I’m eager to figure out what I did wrong and what would be the best approach to go about this. Any suggestions would be much appreciated.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [January 18, 2023, 1:14pm UTC](https://discuss.huggingface.co/t/how-to-load-a-pretrained-custom-model-using-from-pretrained/29927/2 "2023-01-18T13:14:11Z")

</div>

Yes you can inherit from PreTrainedModel to inherit methods like from\_pretrained, save\_pretrained and push\_to\_hub.

Alternatively, you can leverage the [PyTorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/v0.11.0/en/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) class available in the huggingface\_hub library. This allows you to get the same functionality:

```auto
from torch import nn
from huggingface_hub import PyTorchModelHubMixin

class CustomModel(nn.Module, PyTorchModelHubMixin):
          ...

```

---

<div class="post-metadata">

**Author:** ![nomorewords](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@nomorewords](https://discuss.huggingface.co/u/nomorewords)\
**Post date:** [February 6, 2023, 2:16pm UTC](https://discuss.huggingface.co/t/how-to-load-a-pretrained-custom-model-using-from-pretrained/29927/3 "2023-02-06T14:16:02Z")

</div>

> [@nielsr](#):
>
> herit from P

Thank you, this is exactly what I was looking for!

---

<div class="post-metadata">

**Author:** ![luozi](https://avatars.discourse-cdn.com/v4/letter/l/a587f6/32.png) [@luozi](https://discuss.huggingface.co/u/luozi)\
**Post date:** [March 16, 2023, 7:32am UTC](https://discuss.huggingface.co/t/how-to-load-a-pretrained-custom-model-using-from-pretrained/29927/4 "2023-03-16T07:32:31Z")

</div>

nice，thank

---

<div class="post-metadata">

**Author:** ![Eran](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Eran](https://discuss.huggingface.co/u/Eran)\
**Post date:** [June 21, 2023, 3:14pm UTC](https://discuss.huggingface.co/t/how-to-load-a-pretrained-custom-model-using-from-pretrained/29927/5 "2023-06-21T15:14:59Z")

</div>

The reason for this error is that the class definition is missing the `config_class`

```auto
class CustomModel(PreTrainedModel):
    config_class = CustomConfig

    def __init__ (self, config, ...
    ...

```

There is an example in the guide in the original question.
