# Save and Load model

**URL:** <https://discuss.pytorch.org/t/save-and-load-model/6206>\
**Category:** Uncategorized\
**Created:** [August 14, 2017, 1:58pm UTC](https://discuss.pytorch.org/t/save-and-load-model/6206 "2017-08-14T13:58:02Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![MisBah\_Awan](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@MisBah\_Awan](https://discuss.pytorch.org/u/MisBah_Awan)\
**Post date:** [April 19, 2019, 11:57am UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/21 "2019-04-19T11:57:12Z")

</div>

here i save my model:

 ![image](https://discuss.pytorch.org/uploads/default/original/2X/2/2d6eba500bcfcbfb567060009f2b0ec4fa9dc71b.png)  
and now i am stuck here  
 ![image](https://discuss.pytorch.org/uploads/default/original/2X/6/64f355b2f46476b2ee2ff672af0bef761e166bda.png)

---

<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 19, 2019, 1:16pm UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/22 "2019-04-19T13:16:21Z")

</div>

This code works:

```python
x = torch.randn(1, 3, 64, 64) # (batch size or #of images,channels RGB,width,height)
model = Net()
output = model(x)
torch.save({'state_dict': model.state_dict()}, 'tmp.pt')

model = Net()
state_dict = torch.load('tmp.pt')['state_dict']
model.load_state_dict(state_dict)
print(model)

```

Could you try to find differences between your code and mine?

---

<div class="post-metadata">

**Author:** ![MisBah\_Awan](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@MisBah\_Awan](https://discuss.pytorch.org/u/MisBah_Awan)\
**Post date:** [April 19, 2019, 1:25pm UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/23 "2019-04-19T13:25:48Z")

</div>

you mean first i save my model according to ur code and then load 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:** [April 19, 2019, 1:28pm UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/24 "2019-04-19T13:28:11Z")

</div>

I assume you have already saved your model, so my code is just to show that initializing the model and loading the `state_dict` does work and the error might still be somewhere else in your code.

---

<div class="post-metadata">

**Author:** ![Sunil\_Tapashetti](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sunil_tapashetti/32/7177_2.png) [@Sunil\_Tapashetti](https://discuss.pytorch.org/u/Sunil_Tapashetti)\
**Post date:** [October 11, 2019, 5:47pm UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/25 "2019-10-11T17:47:11Z")

</div>

If a pretrained model is saved as Dataparallel can it be loaded as nn.Sequential model?

---

<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:** [November 24, 2019, 7:17pm UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/26 "2019-11-24T19:17:32Z")

</div>

`nn.DataParallel` stores the actual model in the `.module` attribute.  
If you store the `state_dict` from the `model.module`, you’ll be able to directly use it without wrapping the model into `nn.DataParallel`.

---

<div class="post-metadata">

**Author:** ![pinocchio](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pinocchio/32/22093_2.png) [@pinocchio](https://discuss.pytorch.org/u/pinocchio)\
**Post date:** [April 29, 2020, 4:09pm UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/27 "2020-04-29T16:09:54Z")

</div>

# what are the advantages and disadvantages of just using the python pickle

@ptrblck what are the advantages and disadvantages of just using the python pickle module vs the load/save stuff pytorch has built?

> [@Advantages & Disadvantages of using pickle module to save models vs torch.save](https://discuss.pytorch.org/t/advantages-disadvantages-of-using-pickle-module-to-save-models-vs-torch-save/79016):
>
> I was reading [Save and Load model](https://discuss.pytorch.org/t/save-and-load-model/6206/27) but it wasn’t clear why I’d use torch.save over pickle.dump. What worries me is that my Neural Net modules/object have many more things inside of them besides only parameters. Since pytorch saves things using the state\_dict that worried me that something might be missing ([https://pytorch.org/tutorials/beginner/saving\_loading\_models.html](https://pytorch.org/tutorials/beginner/saving_loading_models.html)). Isn’t for my use case easier to simply use the pickle.dump function? Why would I use torch.save in general? Note I am usin…

* * *

> [@How does one pickle arbitrary pytorch models that use lambda functions?](https://discuss.pytorch.org/t/how-does-one-pickle-arbitrary-pytorch-models-that-use-lambda-functions/79026):
>
> I currently have a neural network module: import torch.nn as nn class NN(nn.Module): def \_\_init\_\_(self,args,lambda\_f,nn1, loss, opt): super().\_\_init\_\_() self.args = args self.lambda\_f = lambda\_f self.nn1 = nn1 self.loss = loss self.opt = opt # more nn.Params stuff etc... def forward(self, x): #some code using fields return out I am trying to checkpoint it but because pytorch saves using state\_dicts it means I can…

---

<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 30, 2020, 4:26am UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/28 "2020-04-30T04:26:10Z")

</div>

Internally `pickle` (or a custom pickle module you provide) will be used, so you could also manually store the data most likely.  
However, the [`_save` method](https://github.com/pytorch/pytorch/blob/e75fb4356b752097d093c7013ba85c9eb82961ef/torch/serialization.py#L439) will apply a specific format to load it properly.

I’ll answer in the other threads separately, but I would recommend to keep “similar” questions in one place. 😉

---

<div class="post-metadata">

**Author:** ![pinocchio](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pinocchio/32/22093_2.png) [@pinocchio](https://discuss.pytorch.org/u/pinocchio)\
**Post date:** [April 30, 2020, 2:49pm UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/29 "2020-04-30T14:49:31Z")

</div>

> [@ptrblck](#):
>
> I’ll answer in the other threads separately, but I would recommend to keep “similar” questions in one place. 😉

I’m trying! That’s why I try to reference questions between each other but I don’t have control that other people ask repeated questions ☹ I will try to not make this issue worse. Appreciate the advice. 🙂

---

<div class="post-metadata">

**Author:** ![saba](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/saba/32/19439_2.png) [@saba](https://discuss.pytorch.org/u/saba)\
**Post date:** [August 30, 2021, 3:40am UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/30 "2021-08-30T03:40:30Z")

</div>

Hi Ptrblck,

I want to save my data which are 2000x11x11x11 tensor. I used  
torch.save(TargetGAN,Path, \_use\_new\_zipfile\_serialization=False)

but gave me this error :

TypeError: save() got an unexpected keyword argument ‘\_use\_new\_zipfile\_serialization’

I need to use \_use\_new\_zipfile\_serialization=False, otherwise could not load the data on the GPU and will consider it as a zip file which I don’t want.

Many thanks

---

<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:** [August 30, 2021, 4:31am UTC](https://discuss.pytorch.org/t/save-and-load-model/6206/31 "2021-08-30T04:31:34Z")

</div>

This argument as added in PyTorch [`1.4.0`](https://pytorch.org/docs/1.4.0/torch.html#torch.save), so you might want to update your Pytorch version, if you want to use it.

[Previous page](https://discuss.pytorch.org/t/save-and-load-model/6206.md?page=1)
