如何从预训练的TensorFlow模型中删除图层?

时间:2020-04-28 17:13:16

标签: tensorflow tensorflow2.0

我在TensorFlow中使用三个隐藏层训练了以下模型:

inputs = tf.keras.Input(shape=(timesteps, feature_size))

l = LSTM(units=state_size,
               return_sequences=True)(inputs)
l = LSTM(units=state_size,
               return_sequences=True)(l)
l = LSTM(units=state_size,
               return_sequences=True)(l)

output = TimeDistributed(Dense(output_size, activation='softmax'))(l)

model = tf.keras.Model(inputs=inputs, outputs=output)

现在,我想使用该模型,但跳过第二个隐藏层,即直接将输出从第一层传递到第三层,而无需通过第二层。 我知道我可以通过以下方式保留第一层的输出:

output = model.layers[idx].Output

但是我现在如何将输出输出到第三层? 非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

一种方法是使用图层名称来创建新模型。

下面的示例使用指定的名称。您还可以使用Keras给出的默认名称。

inputs = tf.keras.Input(shape=(timesteps, feature_size))

l = LSTM(units=state_size, return_sequences=True, name="lstm1")(inputs)
l = LSTM(units=state_size, return_sequences=True, name="lstm2")(l)
l = LSTM(units=state_size, return_sequences=True, name="lstm3")(l)

output = TimeDistributed(Dense(output_size, activation='softmax'))(l)

model = tf.keras.Model(inputs=inputs, outputs=output)

# Now create the second model using specific layers from the first model
reuse_layers = ["lstm1", "lstm3"]

inputs = inputs = tf.keras.Input(shape=(timesteps, feature_size))
l = inputs
for layer_name in reuse_layers:
    l = model.get_layer(layer_name)(l)

output = TimeDistributed(Dense(output_size, activation='softmax'))(l)
new_model = Model(inputs=inputs, outputs=output)