# Error while loading state\_dict

**URL:** <https://discuss.pytorch.org/t/error-while-loading-state-dict/49086>\
**Category:** vision\
**Created:** [June 27, 2019, 6:26am UTC](https://discuss.pytorch.org/t/error-while-loading-state-dict/49086 "2019-06-27T06:26:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surya1212](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/surya1212/32/13525_2.png) [@Surya1212](https://discuss.pytorch.org/u/Surya1212)\
**Post date:** [June 27, 2019, 6:26am UTC](https://discuss.pytorch.org/t/error-while-loading-state-dict/49086/1 "2019-06-27T06:26:18Z")

</div>

When I am trying to load a model :

# Load the trained model from file

trained\_model = Net() #Net is myModelClass

m = torch.load(’./modell.pth’, map\_location=lambda storage, loc: storage)

trained\_model.load\_state\_dict(m)

trained\_model.load\_state\_dict(m)  
File “/home/surya/myenv/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 751, in load\_state\_dict  
**state\_dict = state\_dict.copy()**  
File “/home/surya/myenv/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 539, in **getattr**  
type(self). **name** , name))  
AttributeError: ‘Net’ object has no attribute ‘copy’

Next I have used **copy.copy(state\_dict)**. Then I got a new error.

trained\_model.load\_state\_dict(m)  
File “/home/surya/myenv/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 763, in load\_state\_dict  
load(self)  
File “/home/surya/myenv/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 758, in load  
state\_dict, prefix, local\_metadata, True, missing\_keys, unexpected\_keys, error\_msgs)  
File “/home/surya/myenv/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 720, in \_load\_from\_state\_dict  
for key in state\_dict.keys():  
File “/home/surya/myenv/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 539, in **getattr**  
type(self). **name** , name))  
AttributeError: ‘Net’ object has no attribute ‘keys’

---

<div class="post-metadata">

**Author:** ![alex.veuthey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alex.veuthey/32/12176_2.png) [@alex.veuthey](https://discuss.pytorch.org/u/alex.veuthey)\
**Post date:** [June 27, 2019, 6:31am UTC](https://discuss.pytorch.org/t/error-while-loading-state-dict/49086/2 "2019-06-27T06:31:07Z")

</div>

My guess is, you used `torch.save(model)` (saves the entire model with architecture and weights and probably other parameters) instead of `torch.save(model.state_dict())` (saves only the state dict, i.e. the weights).

If you need to stick to the first approach, then you must load it as follows: `model = torch.load(PATH)`.

[More info here](https://pytorch.org/tutorials/beginner/saving_loading_models.html).

---

<div class="post-metadata">

**Author:** ![Jayant\_Parashar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jayant_parashar/32/21173_2.png) [@Jayant\_Parashar](https://discuss.pytorch.org/u/Jayant_Parashar)\
**Post date:** [March 6, 2020, 6:06am UTC](https://discuss.pytorch.org/t/error-while-loading-state-dict/49086/3 "2020-03-06T06:06:09Z")

</div>

What if we saved a torch.save(model) but now need to load the state dict because torch.load(modelpath) code breaks as warned in official documentation ?

---

<div class="post-metadata">

**Author:** ![alex.veuthey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alex.veuthey/32/12176_2.png) [@alex.veuthey](https://discuss.pytorch.org/u/alex.veuthey)\
**Post date:** [March 6, 2020, 7:07am UTC](https://discuss.pytorch.org/t/error-while-loading-state-dict/49086/4 "2020-03-06T07:07:00Z")

</div>

Do newer versions of PyTorch block you from loading a model directly? In that case, load the model directly from an older version of PyTorch, and then save its `state_dict`, which you should be able to load with a more recent version.

---

<div class="post-metadata">

**Author:** ![Jayant\_Parashar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jayant_parashar/32/21173_2.png) [@Jayant\_Parashar](https://discuss.pytorch.org/u/Jayant_Parashar)\
**Post date:** [March 9, 2020, 12:33am UTC](https://discuss.pytorch.org/t/error-while-loading-state-dict/49086/5 "2020-03-09T00:33:01Z")

</div>

Thanks, I will double check the pytorch version. It most likely is the same though. The problem seems to be something else. It gave a size mismatch error.

---

<div class="post-metadata">

**Author:** ![ByShui](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/byshui/32/39908_2.png) [@ByShui](https://discuss.pytorch.org/u/ByShui)\
**Post date:** [July 2, 2021, 10:53pm UTC](https://discuss.pytorch.org/t/error-while-loading-state-dict/49086/6 "2021-07-02T22:53:35Z")

</div>

thank you very much, i just ignore this problem after i modify my save way
