# Load/save model parameters

**URL:** <https://discuss.pytorch.org/t/load-save-model-parameters/10026>\
**Category:** Uncategorized\
**Created:** [November 16, 2017, 12:27pm UTC](https://discuss.pytorch.org/t/load-save-model-parameters/10026 "2017-11-16T12:27:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![0phoff](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/0phoff/32/2318_2.png) [@0phoff](https://discuss.pytorch.org/u/0phoff)\
**Post date:** [November 16, 2017, 12:27pm UTC](https://discuss.pytorch.org/t/load-save-model-parameters/10026/1 "2017-11-16T12:27:24Z")

</div>

I am trying to save my model parameters, together with some extra information.  
For this, I used the principle explained in this [post](https://discuss.pytorch.org/t/saving-and-loading-a-model-in-pytorch/2610/2).

Here are the methods I defined on my models, to save the parameters:  
(self.seen is an integer)

```auto
    def load_pickle_weights(self, weights_file):
        state = torch.load(weights_file)
        self.seen = state['seen']
        self.load_state_dict(state['weights'])

    def save_pickle_weights(self, weights_file):
        state = {
            'seen': self.seen,
            'weights': self.state_dict(),
        }
        torch.save(state, weights_file)

```

I am however, getting the following error:

```auto
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/app/models/objectdet.py", line 80, in __init__
    self.load_pickle_weights(weights_pickle)
  File "/app/network.py", line 85, in load_pickle_weights
    state = torch.load(weights_file)
  File "/opt/conda/lib/python3.6/site-packages/torch/serialization.py", line 261, in load
    return _load(f, map_location, pickle_module)
  File "/opt/conda/lib/python3.6/site-packages/torch/serialization.py", line 394, in _load
    return legacy_load(f)
  File "/opt/conda/lib/python3.6/site-packages/torch/serialization.py", line 329, in legacy_load
    with closing(tarfile.open(fileobj=f, mode='r:', format=tarfile.PAX_FORMAT)) as tar, \
  File "/opt/conda/lib/python3.6/tarfile.py", line 1586, in open
    return func(name, filemode, fileobj, **kwargs)
  File "/opt/conda/lib/python3.6/tarfile.py", line 1616, in taropen
    return cls(name, mode, fileobj, **kwargs)
  File "/opt/conda/lib/python3.6/tarfile.py", line 1471, in __init__
    self.offset = self.fileobj.tell()
AttributeError: 'bool' object has no attribute 'tell'

```

If I just save the state\_dict, as explained [here](http://pytorch.org/docs/master/notes/serialization.html) I get no error…

---

<div class="post-metadata">

**Author:** ![0phoff](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/0phoff/32/2318_2.png) [@0phoff](https://discuss.pytorch.org/u/0phoff)\
**Post date:** [November 24, 2017, 3:26pm UTC](https://discuss.pytorch.org/t/load-save-model-parameters/10026/2 "2017-11-24T15:26:52Z")

</div>

Okaay, so apparently filenames do matter here…  
I tried the exact same code, but called my file `weights.pt` in stead of `weights.pkl` and now it works like a charm!

Not too sure if this naming convention is mentioned somewhere, but I did not find anything about it on my searches through the web…
