# Average each weight of two models

**URL:** <https://discuss.pytorch.org/t/average-each-weight-of-two-models/77008>\
**Category:** nlp\
**Created:** [April 16, 2020, 9:12am UTC](https://discuss.pytorch.org/t/average-each-weight-of-two-models/77008 "2020-04-16T09:12:02Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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 16, 2020, 11:57pm UTC](https://discuss.pytorch.org/t/average-each-weight-of-two-models/77008/2 "2020-04-16T23:57:56Z")

</div>

Yes, you could get the `state_dicts` of both models, average the parameters and reload the new `state_dict`.  
Here is a small dummy example:

```python
# Setup
modelA = nn.Linear(1, 1)
modelB = nn.Linear(1, 1)

sdA = modelA.state_dict()
sdB = modelB.state_dict()

# Average all parameters
for key in sdA:
    sdB[key] = (sdB[key] + sdA[key]) / 2.

# Recreate model and load averaged state_dict (or use modelA/B)
model = nn.Linear(1, 1)
model.load_state_dict(sdB)

```

---

_[View the full topic](https://discuss.pytorch.org/t/average-each-weight-of-two-models/77008)._
