简体   繁体   English

如何从预训练的 TensorFlow model 中删除层?

[英]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.

相关问题 从预训练的 model 中移除顶层,迁移学习,tensorflow (load_model) - Remove top layer from pre-trained model, transfer learning, tensorflow (load_model) 如何在张量流预训练模型中将输入输入到一层? - How to feed input into one layer in a tensorflow pre-trained model? 如何使用TensorFlow Java API移除预训练模型的输出层? - How can I remove the output layer of pre-trained model with TensorFlow java api? 从张量流中的预训练模型推断 - Inference from a pre-trained model in tensorflow 如何在TensorFlow中使用预训练模型 - How to use pre-trained model in TensorFlow 如何在 tensorflow 中使用预训练的 model 进行预测? - how to predict with pre-trained model in tensorflow? 如何用我们自己的输入层替换预训练的输入层 tensorflow model...? - How to replace the input layer of a pre-trained tensorflow model with our own input layer...? 到logits层的Tensorflow推理-适用于预训练模型上的批次 - Tensorflow inference upto logits layer - for batches on pre-trained model Tensorflow:如何从预先训练的CNN的特定层提取图像特征? - Tensorflow: How can I extract image features from a specific layer of a pre-trained CNN? 如何在 Keras 中更改预训练 CNN model 中层的 output? - How to change output of a layer in a pre-trained CNN model in Keras?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM