Copying weights of a specific layer - keras

主宰稳场 提交于 2019-12-06 02:24:47

Of course, it would be a copy of the weights. It does not make sense the weights object to be shared between two separate models. You can check it for yourself with a simple example like this:

model1 = Sequential()
model1.add(Dense(10, input_dim=2))

model2 = Sequential()
model2.add(Dense(10, input_dim=2))

model1.compile(loss='mse', optimizer='adam')
model2.compile(loss='mse', optimizer='adam')

Test:

>>> model1.layers[0].get_weights()
[array([[-0.42853734,  0.18648076, -0.47137827,  0.1792168 ,  0.0373047 ,
          0.2765705 ,  0.38383502,  0.09664273, -0.4971757 ,  0.41548246],
        [ 0.0403192 , -0.01309097,  0.6656211 , -0.0536288 ,  0.58677703,
          0.21625364,  0.26447064, -0.42619988,  0.17218047, -0.39748642]],
       dtype=float32),
 array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

>>> model2.layers[0].get_weights()
[array([[-0.30062824, -0.3740575 , -0.3502644 ,  0.28050178, -0.68631136,
          0.1596322 ,  0.08288956, -0.20988202,  0.34323698,  0.2893324 ],
        [-0.29182747, -0.2754455 , -0.64082885,  0.29160154,  0.04342002,
         -0.4996035 ,  0.6608283 ,  0.10293472,  0.11375248, -0.43438092]],
       dtype=float32),
 array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

>>> model2.layers[0].set_weights(model1.layers[0].get_weights())
>>> model2.layers[0].get_weights()
[array([[-0.42853734,  0.18648076, -0.47137827,  0.1792168 ,  0.0373047 ,
          0.2765705 ,  0.38383502,  0.09664273, -0.4971757 ,  0.41548246],
        [ 0.0403192 , -0.01309097,  0.6656211 , -0.0536288 ,  0.58677703,
          0.21625364,  0.26447064, -0.42619988,  0.17218047, -0.39748642]],
       dtype=float32),
 array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

>>> id(model1.layers[0].get_weights()[0])
140494823634144

>>> id(model2.layers[0].get_weights()[0])
140494823635664

The ids of kernel weights arrays are different so they are different objects, but with the same value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!