Keras自编码器瓶颈层特征提取咨询及API报错解决
Hey there! Let's work through this feature extraction problem you're facing with your Keras autoencoder. That warning you ran into is a classic Keras version mismatch issue—let's fix that first, then cover two solid ways to pull features from your bottleneck layer.
Fixing the Keras 2 API Warning
The error message you saw is telling you that you're using the old Keras 1.x syntax for creating a Model. In Keras 2 and later, we use inputs and outputs (plural) instead of input and output (singular).
So instead of writing:
Model(input=[inputs], output=[intermediate_layer])
You need to update it to:
Model(inputs=[inputs], outputs=[intermediate_layer])
If you're working with a single input and single bottleneck layer, you can even simplify it further by dropping the lists:
Model(inputs=input_layer, outputs=bottleneck_layer)
That should get rid of the warning right away.
Two Reliable Ways to Extract Bottleneck Features
1. Build a Dedicated Feature Extractor Model
This is the most straightforward and widely used method. Here's how to do it step-by-step:
First, let's assume your autoencoder is structured like this (I'll add a name to the bottleneck layer for clarity):
from keras.layers import Input, Dense from keras.models import Model # Encoder input_layer = Input(shape=(784,)) encoder_hidden = Dense(128, activation='relu')(input_layer) # Name the bottleneck layer to make it easy to reference later bottleneck_layer = Dense(32, activation='relu', name='bottleneck')(encoder_hidden) # Decoder decoder_hidden = Dense(128, activation='relu')(bottleneck_layer) output_layer = Dense(784, activation='sigmoid')(decoder_hidden) # Full autoencoder model autoencoder = Model(inputs=input_layer, outputs=output_layer) autoencoder.compile(optimizer='adam', loss='mse') # Train your autoencoder as usual first! autoencoder.fit(x_train, x_train, epochs=50, batch_size=256)
Once your autoencoder is trained, create a new model that maps inputs directly to the bottleneck layer's output:
# Create feature extractor model feature_extractor = Model( inputs=autoencoder.input, outputs=autoencoder.get_layer('bottleneck').output ) # Now use this model to extract features from your data features = feature_extractor.predict(your_input_data)
The features variable will now hold the compressed bottleneck representations of your input data.
2. Use a Keras Function to Get Intermediate Outputs
If you don't want to build a separate model, you can use Keras backend functions to directly fetch the bottleneck layer's output:
from keras import backend as K # Define a function that takes input data and returns bottleneck features get_bottleneck_features = K.function( [autoencoder.input], [autoencoder.get_layer('bottleneck').output] ) # Extract features features = get_bottleneck_features([your_input_data])[0]
Note: If you're using TensorFlow 2.x with eager execution enabled (default), this should work out of the box. If you run into issues, wrapping the function with tf.function can help.
Either of these methods should work smoothly. If your "other method" ran into specific problems, feel free to share more details and we can troubleshoot that too!
内容的提问来源于stack exchange,提问作者user9690450

