# How to load model weights that are stored as an ordereddict?

**URL:** <https://discuss.pytorch.org/t/how-to-load-model-weights-that-are-stored-as-an-ordereddict/75500>\
**Category:** Uncategorized\
**Created:** [April 6, 2020, 6:11am UTC](https://discuss.pytorch.org/t/how-to-load-model-weights-that-are-stored-as-an-ordereddict/75500 "2020-04-06T06:11:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![seankala](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/seankala/32/22144_2.png) [@seankala](https://discuss.pytorch.org/u/seankala)\
**Post date:** [April 6, 2020, 6:11am UTC](https://discuss.pytorch.org/t/how-to-load-model-weights-that-are-stored-as-an-ordereddict/75500/1 "2020-04-06T06:11:37Z")

</div>

Hello. I’m not sure if I’m just unfamiliar with saving and loading Torch models, but I’m facing this predicament and am not sure how to proceed about it.

I’m currently wanting to load someone else’s model to try and run it. I downloaded their `pt` file that contains the model, and upon performing `model = torch.load(PATH)` I noticed that `model` is a dictionary with the keys `model`, `opt`, and `optims`.

I’ve never really seen that before, but I figured that the person saved the optimizer as well as the model’s weights. I assigned `model['model']` to another variable, but when I tried to run the model via a `output = model2(input)` call I got a `TypeError: 'collections.OrderedDict' object is not callable` and realized that the weights were stored as an `ordereddict`.

How might I proceed to achieve what I want? I don’t have much experience with loading and using pre-trained models, but something tells me it should be relatively straightforward.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 6, 2020, 6:31am UTC](https://discuss.pytorch.org/t/how-to-load-model-weights-that-are-stored-as-an-ordereddict/75500/2 "2020-04-06T06:31:35Z")

</div>

The stored checkpoints contains most likely the `state_dict`s of the model and optimizer.  
You would have to create a model instance first and load the `state_dict` afterwards as explained in the [serialization docs](https://pytorch.org/docs/stable/notes/serialization.html):

```python
model = MyModel()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) # change to whatever optimizer was used

checkpoint = torch.load(path_to_checkpoint)
model.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['opt'])

```

Note that you need the model definition to create the model instance.

---

<div class="post-metadata">

**Author:** ![Vishesh\_Kaushik](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vishesh_kaushik/32/39101_2.png) [@Vishesh\_Kaushik](https://discuss.pytorch.org/u/Vishesh_Kaushik)\
**Post date:** [June 5, 2021, 4:19am UTC](https://discuss.pytorch.org/t/how-to-load-model-weights-that-are-stored-as-an-ordereddict/75500/3 "2021-06-05T04:19:02Z")

</div>

Thanks @ptrblck . How to get the model definition? I have OrderedDict for pre-trained LayouLM model. I dont know the model definition. I have one config.json file and one args.bin file with me. I have just started learning pytorch so I dont have much idea about it.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 5, 2021, 5:19am UTC](https://discuss.pytorch.org/t/how-to-load-model-weights-that-are-stored-as-an-ordereddict/75500/4 "2021-06-05T05:19:51Z")

</div>

I’m not sure how the `config.json` and `args.bin` files are used, but you would need to use the model source code in order to create an instance of it before loading the `state_dict`.  
How are you creating an object of the model at the moment?

---

<div class="post-metadata">

**Author:** ![Vishesh\_Kaushik](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vishesh_kaushik/32/39101_2.png) [@Vishesh\_Kaushik](https://discuss.pytorch.org/u/Vishesh_Kaushik)\
**Post date:** [June 5, 2021, 5:33am UTC](https://discuss.pytorch.org/t/how-to-load-model-weights-that-are-stored-as-an-ordereddict/75500/5 "2021-06-05T05:33:23Z")

</div>

I am trying to load pre-trained LayoutLM model. I have downloaded the pre-trained model from [https://drive.google.com/open?id=1Htp3vq8y2VRoTAwpHbwKM0lzZ2ByB8xM](https://drive.google.com/open?id=1Htp3vq8y2VRoTAwpHbwKM0lzZ2ByB8xM). It has only the state\_dict and not the model itself.  
I am using the classes given on the following link for model definition [transformers.models.layoutlm.modeling\_layoutlm — transformers 4.5.0.dev0 documentation](https://huggingface.co/transformers/_modules/transformers/models/layoutlm/modeling_layoutlm.html#LayoutLMModel)  
Am I on the right path?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 5, 2021, 6:34am UTC](https://discuss.pytorch.org/t/how-to-load-model-weights-that-are-stored-as-an-ordereddict/75500/6 "2021-06-05T06:34:00Z")

</div>

The latter link seems to point to the model definition, so you might create an object using:

```python
model = LayoutLMModel(...)

```

and load the `state_dict` via:

```python
model.load_state_dict(torch.load(PATH))

```

---

<div class="post-metadata">

**Author:** ![Vishesh\_Kaushik](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vishesh_kaushik/32/39101_2.png) [@Vishesh\_Kaushik](https://discuss.pytorch.org/u/Vishesh_Kaushik)\
**Post date:** [June 5, 2021, 6:55am UTC](https://discuss.pytorch.org/t/how-to-load-model-weights-that-are-stored-as-an-ordereddict/75500/7 "2021-06-05T06:55:55Z")

</div>

@ptrblck. Following your suggestions,  
I have tried doing  
model = LayoutlmModel.from\_pretrained(“microsoft/layoutlm-base-uncased”)  
then  
model.load\_state\_dict(torch.load(‘pytorch\_model.bin’, map\_location=‘cpu’))

Following error comes  
RuntimeError: Error(s) in loading state\_dict for LayoutlmModel:  
Missing key(s) in state\_dict: “embeddings.word\_embeddings.weight”, “embeddings.position\_embeddings.weight”, “embeddings.x\_position\_embeddings.weight”, “embeddings.y\_position\_embeddings.weight”, …

but both the keys in state\_dict(pytorch\_model.bin) and the keys in the model are same then why this error is shown?

PS : model = LayoutlmModel.from\_pretrained(“microsoft/layoutlm-base-uncased”,state\_dict=torch.load(‘pytorch\_model.bin’, map\_location=‘cpu’)). This has worked for me.  
Thank you @ptrblck . Your suggestions were valuable.
