# Saving torch models

**URL:** <https://discuss.pytorch.org/t/saving-torch-models/838>\
**Category:** Uncategorized\
**Created:** [March 2, 2017, 3:10pm UTC](https://discuss.pytorch.org/t/saving-torch-models/838 "2017-03-02T15:10:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Russel\_Russel](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/ebca7d/32.png) [@Russel\_Russel](https://discuss.pytorch.org/u/Russel_Russel)\
**Post date:** [March 2, 2017, 3:10pm UTC](https://discuss.pytorch.org/t/saving-torch-models/838/1 "2017-03-02T15:10:09Z")

</div>

```
class VAE(nn.Module):
    def __init__ (self):
        super(VAE, self). __init__ ()

        self.fc1 = nn.Linear(784, 400)

model = VAE()

with open('model_save', 'wb') as f: 
torch.save(model, f)

```

When I open `model_save` file, I see the following: mode\_save is not UTF encoded. Saving Disabled. See console for more details. And, When I try to use torch.load, I see this error: "`module" object has no attribute VAE`. Would someone please advise me? Thanks.

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [March 2, 2017, 8:04pm UTC](https://discuss.pytorch.org/t/saving-torch-models/838/2 "2017-03-02T20:04:32Z")

</div>

I have no idea when are you getting the first error and what does it mean. I’ve never seen that.

About the second thing, the problem with saving modules is that they will break if you move the model code into a different file or directory, because they don’t save the class, but a path where pickle will try to look it up at load time. I’d recommend using `state_dict()` for serialization and `load_state_dict()` for reloading the weights.

---

<div class="post-metadata">

**Author:** ![Russel\_Russel](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/ebca7d/32.png) [@Russel\_Russel](https://discuss.pytorch.org/u/Russel_Russel)\
**Post date:** [March 4, 2017, 3:11pm UTC](https://discuss.pytorch.org/t/saving-torch-models/838/3 "2017-03-04T15:11:04Z")

</div>

Thanks for your reply. Should I be pass to torch.save the `model` object like I did above or `VAE` which is the class itself? When I save the model object add try to load it again, I get “‘str’ object is not callable” as I fail at `"result = map_location(storage, location)"` and for the exact same call, sometime I get a different error: " Attribute error" saying that it Couldn’t locate/load the VAE class.

---

<div class="post-metadata">

**Author:** ![Veril](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@Veril](https://discuss.pytorch.org/u/Veril)\
**Post date:** [March 4, 2017, 3:18pm UTC](https://discuss.pytorch.org/t/saving-torch-models/838/4 "2017-03-04T15:18:58Z")

</div>

```
net = Net(...).cuda()
torch.save(net.state_dict(), './net.pth')
net.load_state_dict(torch.load('./net.pth'))
```

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [March 4, 2017, 11:23pm UTC](https://discuss.pytorch.org/t/saving-torch-models/838/5 "2017-03-04T23:23:54Z")

</div>

Are you using the `map_fn` argument of `torch.load`? Do you remap the location of the storages?

---

<div class="post-metadata">

**Author:** ![x0s](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/x/e480ec/32.png) [@x0s](https://discuss.pytorch.org/u/x0s)\
**Post date:** [December 8, 2017, 1:51pm UTC](https://discuss.pytorch.org/t/saving-torch-models/838/6 "2017-12-08T13:51:09Z")

</div>

Hey,  
The first error about _Encoding_ probably appeared when you tried to open a file (ie `model_save`) with a format not editable with Jupyter Notebook (as Jupyter [outputs this error when trying to edit non-text files](https://github.com/jupyter/notebook/blob/master/notebook/static/tree/js/notebooklist.js#L548))

Indeed, the model is saved in binary format (see [source](https://github.com/pytorch/pytorch/blob/master/torch/serialization.py#L134)), therefore is not editable, so you get the jupyter corresponding error.  
Nevertheless, you still are able to open the model in Python with `torch.load`, even if it has been recommended to save/load parameters.

---

<div class="post-metadata">

**Author:** ![jphreid](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jphreid/32/4070_2.png) [@jphreid](https://discuss.pytorch.org/u/jphreid)\
**Post date:** [March 6, 2018, 4:36pm UTC](https://discuss.pytorch.org/t/saving-torch-models/838/7 "2018-03-06T16:36:39Z")

</div>

I am implementing these exact same lines of code, but I get an error.

Code : (On a different computer, with GPU)

> > model = Classifier()  
> > … training…  
> > torch.save(model.state\_dict(), ‘./model\_Q2.pth’)

Code: (on my laptop)

> > model = Classifier()  
> > model.load\_state\_dict(torch.load(‘./model\_Q2.pth’))

Error : module ‘torch.\_C’ has no attribute ‘\_cuda\_getDevice’

Note : I trained my model with cuda = True on a virtual environment on a network (with GPUs), but I want to evaluate my model on my laptop.

I believe we need to ‘switch off’ the cuda before saving.

Could you help?

---

<div class="post-metadata">

**Author:** ![jphreid](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jphreid/32/4070_2.png) [@jphreid](https://discuss.pytorch.org/u/jphreid)\
**Post date:** [March 6, 2018, 4:52pm UTC](https://discuss.pytorch.org/t/saving-torch-models/838/8 "2018-03-06T16:52:42Z")

</div>

Ok, got it!

> model = Classifier()  
> torch.save(model.state\_dict(), ‘./model\_Q2.pth’)  
> model.load\_state\_dict(torch.load(‘./model\_Q2.pth’, map\_location=lambda storage, loc: storage))

… map\_location=lambda storage, loc: storage) : load the model on CPU.

See this: [On a cpu device, how to load checkpoint saved on gpu device](https://discuss.pytorch.org/t/on-a-cpu-device-how-to-load-checkpoint-saved-on-gpu-device/349)
