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