如何使 keras 模型中给定层的启用可训练?
How to make the enabling of a given layer, in a keras model, trainable?
我有以下连体模型:
我想让 enabling/disabling 层 a-L1
和 b-L1
可训练。即:a-L1
and/or b-L1
如有必要,应对当前输入透明(未使用或禁用)。因此,训练后的模型将在应该 enable/disable 一层或两层 a-L1
和 b-L1
.
的时候学习
我设法用 4 个案例训练了这个模型,所以我相应地得到了 4 个不同的模型:
- 模型 1:没有 a-L1 和 b-L1
- model-2: 没有 a-L1
- model-3: 无 b-L1
- model-4: a-L1 和 b-L1
这些模型的性能相辅相成,我想将它们结合起来。请问你有什么建议吗?
假设您训练了四个模型,我们称它们为 m1、m2、m3 和 m4
首先定义所有输入层通用的输入层。
inputs = Input(shape=your_inputs_shape)
model_1_output = m1(inputs)
model_2_output = m2(inputs)
model_3_output = m3(inputs)
model_4_output = m4(inputs)
merged_layer = Concatenate(axis=your_concatanation_axis)([model_1_output, model_2_output, model_3_output,model_4_output)
new_model = Model(inputs=inputs, outputs=merged_layer)
希望这能解决您的问题。
编辑:
为了回答你的评论问题,可以只合并 L2 之前的层。但是您必须决定要使用从 L2 开始的模型层(因为您没有组合从 L2 开始的层)。假设您想在 L2 之后使用 m1 模型的层。此外,我想添加我在上面的答案评论中指定的加权机制。
首先让我们用共同的新输入定义新模型
new_inputs = Input(shape=(inputs_shape))
new_m1 = keras.models.Model(inputs = new_inputs, outputs = m1(new_inputs))
new_m2 = keras.models.Model(inputs = new_inputs, outputs = m2(new_inputs))
new_m3 = keras.models.Model(inputs = new_inputs, outputs = m3(new_inputs))
new_m4 = keras.models.Model(inputs = new_inputs, outputs = m4(new_inputs))
现在获取所有模型的 L2 层
model1_l2 = new_m1.layers[1].get_layer("L2").output
model2_l2 = new_m2.layers[1].get_layer("L2").output
model3_l2 = new_m3.layers[1].get_layer("L2").output
model4_l2 = new_m4.layers[1].get_layer("L2").output
加权合并
merged = Concatenate(axis=your_concatanation_axis)([model1_l2, model2_l2, model3_l2,model4_l2])
merged_layer_shape = merged.get_shape().as_list()
# specify number of channels you want the output to have after merging
desired_output_channels = 32
new_trainable_weights = keras.backend.random_normal_variable(shape=(merged_layer_shape[-1], desired_output_channels),mean=0,scale=1)
weighted_output = keras.backend.dot(merged,new_trainable_weights)
现在用这个新的 weighted_output
连接 L2 旁边的 model1(m1) 层
# I'm using some protected properties of layer. But it is not recommended way to do it.
# get the index of l2 layer in new_m1
for i in range(len(new_m1.layers[1].layers)):
if new_m1.layers[1].layers[i].name=="L2":
index = i
x = weighted_output
for i in range(index+1, len(new_m1.layers[1].layers)):
x = new_m1.layers[1].layers[i](x)
new_model = keras.models.Model(inputs=new_inputs, outputs=x)
我有以下连体模型:
我想让 enabling/disabling 层 a-L1
和 b-L1
可训练。即:a-L1
and/or b-L1
如有必要,应对当前输入透明(未使用或禁用)。因此,训练后的模型将在应该 enable/disable 一层或两层 a-L1
和 b-L1
.
的时候学习
我设法用 4 个案例训练了这个模型,所以我相应地得到了 4 个不同的模型:
- 模型 1:没有 a-L1 和 b-L1
- model-2: 没有 a-L1
- model-3: 无 b-L1
- model-4: a-L1 和 b-L1
这些模型的性能相辅相成,我想将它们结合起来。请问你有什么建议吗?
假设您训练了四个模型,我们称它们为 m1、m2、m3 和 m4
首先定义所有输入层通用的输入层。
inputs = Input(shape=your_inputs_shape)
model_1_output = m1(inputs)
model_2_output = m2(inputs)
model_3_output = m3(inputs)
model_4_output = m4(inputs)
merged_layer = Concatenate(axis=your_concatanation_axis)([model_1_output, model_2_output, model_3_output,model_4_output)
new_model = Model(inputs=inputs, outputs=merged_layer)
希望这能解决您的问题。
编辑:
为了回答你的评论问题,可以只合并 L2 之前的层。但是您必须决定要使用从 L2 开始的模型层(因为您没有组合从 L2 开始的层)。假设您想在 L2 之后使用 m1 模型的层。此外,我想添加我在上面的答案评论中指定的加权机制。
首先让我们用共同的新输入定义新模型
new_inputs = Input(shape=(inputs_shape))
new_m1 = keras.models.Model(inputs = new_inputs, outputs = m1(new_inputs))
new_m2 = keras.models.Model(inputs = new_inputs, outputs = m2(new_inputs))
new_m3 = keras.models.Model(inputs = new_inputs, outputs = m3(new_inputs))
new_m4 = keras.models.Model(inputs = new_inputs, outputs = m4(new_inputs))
现在获取所有模型的 L2 层
model1_l2 = new_m1.layers[1].get_layer("L2").output
model2_l2 = new_m2.layers[1].get_layer("L2").output
model3_l2 = new_m3.layers[1].get_layer("L2").output
model4_l2 = new_m4.layers[1].get_layer("L2").output
加权合并
merged = Concatenate(axis=your_concatanation_axis)([model1_l2, model2_l2, model3_l2,model4_l2])
merged_layer_shape = merged.get_shape().as_list()
# specify number of channels you want the output to have after merging
desired_output_channels = 32
new_trainable_weights = keras.backend.random_normal_variable(shape=(merged_layer_shape[-1], desired_output_channels),mean=0,scale=1)
weighted_output = keras.backend.dot(merged,new_trainable_weights)
现在用这个新的 weighted_output
连接 L2 旁边的 model1(m1) 层# I'm using some protected properties of layer. But it is not recommended way to do it.
# get the index of l2 layer in new_m1
for i in range(len(new_m1.layers[1].layers)):
if new_m1.layers[1].layers[i].name=="L2":
index = i
x = weighted_output
for i in range(index+1, len(new_m1.layers[1].layers)):
x = new_m1.layers[1].layers[i](x)
new_model = keras.models.Model(inputs=new_inputs, outputs=x)