[英]How to remove layer from pre-trained TensorFlow model?
I trained the following model with three hidden layers in TensorFlow:我在 TensorFlow 中用三个隐藏层训练了以下 model:
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)
Now, I would like to use the model but skip the second hidden layer, ie directly pass the output from the first layer to the third layer without going through the second layer.现在,我想使用 model 但跳过第二个隐藏层,即直接将 output 从第一层传递到第三层,而不经过第二层。 I understand that I can get a hold of the output from the first layer by:我知道我可以通过以下方式从第一层获取 output:
output = model.layers[idx].Output
But how could I feed this output to the third layer now?但是我现在怎么能把这个 output 喂到第三层呢? Many thanks for any help!非常感谢您的帮助!
One approach is to use layer names to create a new model.一种方法是使用层名称来创建新的 model。
The example below uses specified names.下面的示例使用指定的名称。 You can also use the default names given by Keras.您还可以使用 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 = 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.