Good way to calculate element-wise difference between two models of the same structure

Suppose I have two models of the same structure, what is the best way to calculate the sum of difference between corresponding weights from these two models? (pseudo code: L1_loss(model1,model2))

If the two models have the exact same structure (i.e. their state dicts have the same layers in the same order) then the following should work:

sum((x - y).abs().sum() for x, y in zip(model1.state_dict().values(), model2.state_dict().values()))
2 Likes