在第一模型的相同第一层中使用权重时训练第二模型

时间:2018-09-22 15:57:24

标签: python keras keras-layer

假设我们有两个模型,model1和model2。假设模型1和模型2的前10层相同。然后,在Keras中使用一些数据集训练model1。将模型保存在“ model1.h5”中。然后您意识到model1的权重对于开始训练model2很有用。我想做这样的事情:

# make model1  and model2
# then set weights for the first 10 layers
model1.load_model("model1.h5")
model2.set_weights[:10] = model1.get_weights[:10]
model2.compile(...)
model2.fit(...)
model2.save("model2.h5")

2 个答案:

答案 0 :(得分:1)

使用model.load_weights(file, by_name=True)选项的一种干净方法。您需要为共享的图层分配相同的名称:

# model 1 ...
model1.add(Dense(64, name="dense1"))
model1.add(Dense(2, name="dense2"))

# model 2 ...
model2.add(Dense(64, name="dense1"))
model2.add(Dense(32, name="notshared"))

# train model 1 and save
model1.save(filename) # or model.save_weights(filename)

# Load shared layers into model 2
model2.load_weights(filename, by_name=True) # will skip non-matching layers
# So it will only load "dense1"

要点是从模型1权重文件中加载模型2权重,但仅加载匹配的图层名称。图层必须具有相同的形状和类型。

答案 1 :(得分:1)

As a complement to @nuric's answer and in response to your comment, If you have not set names on the layers before training and storing the model, you can also set them after loading the model:

# load the model
model = load_model(...)

model.layers[index_of_layer].name = 'layer_name'

Then you can use @nuric's solution to load the weights. Further, to find the index of layers you can use model.summary(). The layers are numbered from top to bottom in that list starting from zero.