1

By using numpy.array_equal(), the code below shows that even after copying the weights from model_1 to model_2, it still points out that both weights are different even though by looking at both weights, they are indeed equal to one another. Why?

import numpy as np
from keras.models import Sequential
from keras.layers import Dense

model_1 = Sequential([
  Dense(1, activation='relu', input_shape=(10,)),
])

model_2 = Sequential([
  Dense(1, activation='relu', input_shape=(10,)),
])

model_2.set_weights(model_1.get_weights())
print(model_1.get_weights())
print()
print(model_2.get_weights())
print()
print(np.array_equal(model_1.get_weights(), model_2.get_weights()))

The output I get:

[array([[-0.37920648],
       [-0.23108077],
       [ 0.43857104],
       [-0.58995485],
       [-0.7320645 ],
       [-0.65417486],
       [ 0.6509816 ],
       [-0.41319188],
       [ 0.54799384],
       [ 0.7301964 ]], dtype=float32), array([0.], dtype=float32)]

[array([[-0.37920648],
       [-0.23108077],
       [ 0.43857104],
       [-0.58995485],
       [-0.7320645 ],
       [-0.65417486],
       [ 0.6509816 ],
       [-0.41319188],
       [ 0.54799384],
       [ 0.7301964 ]], dtype=float32), array([0.], dtype=float32)]

False

Expected output: True

ihavenoidea
  • 629
  • 1
  • 7
  • 26
  • `np.all_close` is better for floats – hpaulj Apr 01 '20 at 03:18
  • @hpaulj I get the following error if I use np.allclose: `TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''` – ihavenoidea Apr 01 '20 at 03:23
  • Those look 2 element object dtype arrays (or lists) containing arrays with different shape. So you can't readily compare rhem. – hpaulj Apr 01 '20 at 04:41

1 Answers1

2

get_weights() returns a list of numpy arrays.

When you pass lists of numpy arrays to array_equal, each list is internally converted to a numpy array of object dtype, which you can't simply do equality checks on.

See here for a related question and further explanation

Bert Kellerman
  • 1,590
  • 10
  • 17