-1

I want to load the weights from model A to model B. Model B last few layers are a bit different, thus the weights for those layers should not be loaded. I'm using load_weights with parameters such that layer that are different are skipped.

load_weights(    filepath, by_name=True, skip_mismatch=True)

Is there a way to know the portion of weights that have correctly been loaded ? I just want to make sure than ~90% of the weights get correctly loaded.

n0tis
  • 750
  • 1
  • 6
  • 27

1 Answers1

0

You can use numpy.testingto check the difference between the weights from both models. For example,

np.testing.assert_allclose(
    A_model.layers[0].bias.numpy(), B_model.layers[0].bias.numpy()
)

np.testing.assert_allclose can give % mismatched elements:

a = [1.0, 2, 3] 
b = np.array([1.0, 8, 9])
try:
    np.testing.assert_allclose(a, b)
except Exception as e:
    print(e)

exception prints

Not equal to tolerance rtol=1e-07, atol=0
Mismatched elements: 2 / 3 (66.7%)
Max absolute difference: 6.
Max relative difference: 0.75
 x: array([1., 2., 3.])
 y: array([1., 8., 9.])
Vijay Mariappan
  • 16,921
  • 3
  • 40
  • 59