复制特定图层的权重-Keras [英] Copying weights of a specific layer - keras
问题描述
根据此,以下副本权重从一种模型转换为另一种模型:
According to this the following copies weights from one model to another:
target_model.set_weights(model.get_weights())
复制特定图层的权重该怎么办?
What about copying the weights of a specific layer, would this work?
model_1.layers[0].set_weights(source_model.layers[0].get_weights())
model_2.layers[0].set_weights(source_model.layers[0].get_weights())
如果我训练model_1
和model_2
,它们的重量会分开吗? 文档没有说明此get_weights
是否进行深拷贝或不是.如果这不起作用,该如何实现?
If I train model_1
and model_2
will they have separate weights? The documentation doesn't state whether if this get_weights
makes a deep copy or not. If this doesn't work, how can this be achieved?
推荐答案
当然,它是砝码的副本.在两个单独的模型之间共享权重对象没有意义.您可以使用以下简单示例自己检查它:
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')
测试:
>>> 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
内核权重数组的id不同,因此它们是不同的对象,但具有相同的值.
The ids of kernel weights arrays are different so they are different objects, but with the same value.
这篇关于复制特定图层的权重-Keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!