# Check if models have same weights

**URL:** <https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351>\
**Category:** Uncategorized\
**Created:** [June 27, 2017, 3:32am UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351 "2017-06-27T03:32:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lwang77](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/34f0e0/32.png) [@lwang77](https://discuss.pytorch.org/u/lwang77)\
**Post date:** [June 27, 2017, 3:32am UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/1 "2017-06-27T03:32:27Z")

</div>

Hello, I want to be able to check if two models have the same weights in their layers. After poking around, I couldn’t find a function that did this, so I implemented my own.

```auto
def compareModelWeights(model_a, model_b):
    module_a = model_a._modules
    module_b = model_b._modules
    if len(list(module_a.keys())) != len(list(module_b.keys())):
        return False
    a_modules_names = list(module_a.keys())
    b_modules_names = list(module_b.keys())
    for i in range(len(a_modules_names)):
        layer_name_a = a_modules_names[i]
        layer_name_b = b_modules_names[i]
        if layer_name_a != layer_name_b:
            return False
        layer_a = module_a[layer_name_a]
        layer_b = module_b[layer_name_b]
        if (
            (type(layer_a) == nn.Module) or (type(layer_b) == nn.Module) or
            (type(layer_a) == nn.Sequential) or (type(layer_b) == nn.Sequential)
            ):
            if not compareModelWeights(layer_a, layer_b):
                return False
        if hasattr(layer_a, 'weight') and hasattr(layer_b, 'weight'):
            if not torch.equal(layer_a.weight.data, layer_b.weight.data):
                return False
    return True

```

It’s recursive because modules can have a tree structure.  
If there is already a function that does this, I would love to use it in my code. If not, this might be a useful addition. Please let me know if there are any errors.

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [July 2, 2017, 11:49pm UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/2 "2017-07-02T23:49:58Z")

</div>

you can get `model1.parameters()` and `model2.parameters()` and use:

```python
for p1, p2 = zip(model1.parameters(), model2.parameters()):
    if p1.data.ne(p2.data).sum() > 0:
        return False
return True

```

---

<div class="post-metadata">

**Author:** ![roee](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/90ced4/32.png) [@roee](https://discuss.pytorch.org/u/roee)\
**Post date:** [March 18, 2018, 5:11am UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/3 "2018-03-18T05:11:28Z")

</div>

Hey, just a small correction:  
It should be:

> [@smth](#):
>
> for p1, p2 **in** zip(model1.parameters(), model2.parameters()):  
> if p1.data.ne(p2.data).sum() \> 0:  
> return False  
> return True

---

<div class="post-metadata">

**Author:** ![roee](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/90ced4/32.png) [@roee](https://discuss.pytorch.org/u/roee)\
**Post date:** [March 18, 2018, 12:46pm UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/5 "2018-03-18T12:46:32Z")

</div>

I don’t think an abs is necessary (or wanted)… you probably want to distinguish between negative and positive values.

- 

ne (not equal) returns Boolean values which should be simply summed.

---

<div class="post-metadata">

**Author:** ![aasharma90](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@aasharma90](https://discuss.pytorch.org/u/aasharma90)\
**Post date:** [October 12, 2018, 4:11am UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/6 "2018-10-12T04:11:53Z")

</div>

I found this to be a better approach (also compares batch\_norm layers for their running\_mean and running\_var params) -

```auto
def compare_models(model_1, model_2):
    models_differ = 0
    for key_item_1, key_item_2 in zip(model_1.state_dict().items(), model_2.state_dict().items()):
        if torch.equal(key_item_1[1], key_item_2[1]):
            pass
        else:
            models_differ += 1
            if (key_item_1[0] == key_item_2[0]):
                print('Mismtach found at', key_item_1[0])
            else:
                raise Exception
    if models_differ == 0:
        print('Models match perfectly! :)')

```

---

<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:** [February 13, 2020, 8:22pm UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/7 "2020-02-13T20:22:36Z")

</div>

why isn’t this or something like this a built in method?

---

<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:** [February 14, 2020, 6:00am UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/8 "2020-02-14T06:00:47Z")

</div>

Feature requests and PR are always welcome, if you think this feature is useful and often used. 😉

---

<div class="post-metadata">

**Author:** ![Andrew\_Edmondson](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/andrew_edmondson/32/49331_2.png) [@Andrew\_Edmondson](https://discuss.pytorch.org/u/Andrew_Edmondson)\
**Post date:** [June 29, 2022, 12:43pm UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/9 "2022-06-29T12:43:54Z")

</div>

Adding an answer since I spent a 1/2 hour trying to figure out the same thing to check whether a network was really updating (spoiler, it wasn’t).

The easiest way I found to directly compare two networks is to convert the state\_dict to a string representation and compare them directly.

```auto

state_a = network.state_dict(). __str__ ()
loss.backward()
opt.step()
state_b = network.state_dict(). __str__ ()
 
if state_a == state_b:
    print("Network not updating.")

```

---

<div class="post-metadata">

**Author:** ![desaibhargav](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/desaibhargav/32/44225_2.png) [@desaibhargav](https://discuss.pytorch.org/u/desaibhargav)\
**Post date:** [November 13, 2022, 12:15am UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/10 "2022-11-13T00:15:53Z")

</div>

Thanks for this!

PS: I think `str(network.state_dict())` works too!

---

<div class="post-metadata">

**Author:** ![MaRodriguezB777](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/marodriguezb777/32/74223_2.png) [@MaRodriguezB777](https://discuss.pytorch.org/u/MaRodriguezB777)\
**Post date:** [November 27, 2024, 11:00am UTC](https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/11 "2024-11-27T11:00:32Z")

</div>

This method does not work if one of the models is a superset of the other because the zip could get rid of the differing excess
