# Unable to load checkpoint after finetuning

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-load-checkpoint-after-finetuning%2F50693%3E%5C
**Category:** Intermediate\
**Created:** [August 14, 2023, 8:38am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-load-checkpoint-after-finetuning%2F50693 "2023-08-14T08:38:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sifal](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Sifal](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FSifal)%5C
**Post date:** [August 14, 2023, 8:38am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-load-checkpoint-after-finetuning%2F50693%2F1 "2023-08-14T08:38:57Z")

</div>

Hi,  
So I’m running into trouble when trying to load the model weights after finetuning a pretrained base model, steps I’m taking:

```auto
model= BertForSequenceClassification.from_pretrained("model/repo")

model.classifier = torch.nn.Sequential(
    torch.nn.Dropout(0.4),
    torch.nn.Linear(model.config.hidden_size, 256),
    torch.nn.ReLU(),
    torch.nn.Dropout(0.4),
    torch.nn.Linear(256, 2),
)

```

getting a warning here which I guess is fine for the moment:

```auto
Some weights of the model checkpoint at model/repo were not used when initializing BertForSequenceClassification: ['cls.predictions.transform.LayerNorm.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.dense.bias', 'cls.predictions.transform.dense.weight', 'cls.predictions.bias']
- This IS expected if you are initializing BertForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing BertForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Some weights of BertForSequenceClassification were not initialized from the model checkpoint at model/repo and are newly initialized: ['bert.pooler.dense.bias', 'bert.pooler.dense.weight', 'classifier.weight', 'classifier.bias']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.

```

But after finetuning using hyperparameter search :

```auto
trainer.hyperparameter_search(
    direction="maximize",
    backend="optuna",
    hp_space=optuna_hp_space,
    n_trials=10,
)

```

and trying to load the model with the following config I’m still getting the same waring as before:

```auto
from transformers import AutoConfig, AutoModel

model = AutoModel.from_pretrained("./run-3/checkpoint-1880",config=model.config)

```

Is there something I’m missing?  
Thanks for looking into this.

---

<div class="post-metadata">

**Author:** ![lakshmi97](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@lakshmi97](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flakshmi97)%5C
**Post date:** [November 16, 2023, 11:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-load-checkpoint-after-finetuning%2F50693%2F2 "2023-11-16T23:06:40Z")

</div>

I have the same issue. I try to resume my model training and I get a similar behavior. I don’t see why only classification head weights aren’t saved while all other weights are saved.  
_**There were missing keys in the checkpoint model loaded: [‘cls.predictions.decoder.weight’,‘cls.predictions.decoder.bias’].**_

---

<div class="post-metadata">

**Author:** ![MohammedAljahdali](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@MohammedAljahdali](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FMohammedAljahdali)%5C
**Post date:** [November 23, 2023, 4:07pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-load-checkpoint-after-finetuning%2F50693%2F3 "2023-11-23T16:07:38Z")

</div>

I have the same issue, when resuming from a checkpoint, I get this warning: `There were missing keys in the checkpoint model loaded: ['lm_head.weight'].`. I am not sure what is the cause, but trying to figure it out.

---

<div class="post-metadata">

**Author:** ![MohammedAljahdali](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@MohammedAljahdali](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FMohammedAljahdali)%5C
**Post date:** [November 23, 2023, 4:57pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-load-checkpoint-after-finetuning%2F50693%2F4 "2023-11-23T16:57:30Z")

</div>

I solved the issue on my end by setting `save_safetensors=False,` in the TrainingArguments.

---

<div class="post-metadata">

**Author:** ![Rudrashk](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/rudrashk/32/23282_2.png) [@Rudrashk](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FRudrashk)%5C
**Post date:** [January 24, 2024, 7:19pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-load-checkpoint-after-finetuning%2F50693%2F6 "2024-01-24T19:19:55Z")

</div>

When using `save_safetensors=False`, it throws errors while loading the prior checkpoints as it cannot detect the pytorch\_model.bin file. Any suggestion to restore?

---

<div class="post-metadata">

**Author:** ![alimotahharynia](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/alimotahharynia/32/25986_2.png) [@alimotahharynia](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Falimotahharynia)%5C
**Post date:** [February 21, 2024, 9:15pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-load-checkpoint-after-finetuning%2F50693%2F7 "2024-02-21T21:15:59Z")

</div>

I also did this and it worked. Wonder what’s the problem with model.safetensors, cause when I check the model.state\_dict of safetensors, the lm\_head.weight was present.
