# Model.save\_pretrained() does not save layer changes

**URL:** <https://discuss.huggingface.co/t/model-save-pretrained-does-not-save-layer-changes/55418>\
**Category:** Beginners\
**Created:** [September 18, 2023, 9:55am UTC](https://discuss.huggingface.co/t/model-save-pretrained-does-not-save-layer-changes/55418 "2023-09-18T09:55:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![DuanXR](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@DuanXR](https://discuss.huggingface.co/u/DuanXR)\
**Post date:** [September 18, 2023, 9:55am UTC](https://discuss.huggingface.co/t/model-save-pretrained-does-not-save-layer-changes/55418/1 "2023-09-18T09:55:59Z")

</div>

I trained a model based on BeitForImageClassification, and for some reason I needed to remove the classifier layer,I did it by calling this, and it worked fine.

```auto
pretrained_model.classifier = torch.nn.Identity()

```

And this is the current model structure:

```auto
========================================================================================================================
BeitForImageClassification [1, 768] --
├─BeitModel: 1-1 [1, 768] --
│ └─BeitEmbeddings: 2-1 [1, 197, 768] 768
│ │ └─BeitPatchEmbeddings: 3-1 [1, 196, 768] 590,592
│ │ └─Dropout: 3-2 [1, 197, 768] --
│ └─BeitEncoder: 2-2 [1, 197, 768] --
│ │ └─ModuleList: 3-3 -- 85,169,088
│ └─Identity: 2-3 [1, 197, 768] --
│ └─BeitPooler: 2-4 [1, 768] --
│ │ └─LayerNorm: 3-4 [1, 768] 1,536
├─Identity: 1-2 [1, 768] --
========================================================================================================================

```

However it seems this change was not saved to the local model file with save\_pretrain(), and when I called BeitForImageClassification.from\_pretrained to read my saved model, the classifier went back to its previous state:

```auto
========================================================================================================================
BeitForImageClassification [1, 589] --
├─BeitModel: 1-1 [1, 768] --
│ └─BeitEmbeddings: 2-1 [1, 197, 768] 768
│ │ └─BeitPatchEmbeddings: 3-1 [1, 196, 768] 590,592
│ │ └─Dropout: 3-2 [1, 197, 768] --
│ └─BeitEncoder: 2-2 [1, 197, 768] --
│ │ └─ModuleList: 3-3 -- 85,169,088
│ └─Identity: 2-3 [1, 197, 768] --
│ └─BeitPooler: 2-4 [1, 768] --
│ │ └─LayerNorm: 3-4 [1, 768] 1,536
├─Linear: 1-2 [1, 589] 452,941
========================================================================================================================

```

Here’s my code:

```auto
new_model_path = model_path + "_new"
    pretrained_model = BeitForImageClassification.from_pretrained(model_path, ignore_mismatched_sizes=True)
    pretrained_model.classifier = torch.nn.Identity()
    summary(model=pretrained_model, input_size=(1, 3, 224, 224))
    pretrained_model.save_pretrained(new_model_path)

    new_model = BeitForImageClassification.from_pretrained(new_model_path, ignore_mismatched_sizes=True)
    summary(model=new_model, input_size=(1, 3, 224, 224))

```

Did I miss something?
