user9690450
user9690450

Reputation: 57

Extracting features from the bottleneck layer in Keras Autoencoder

I am sequentially asking you for the autoencoder stuff past weeks. The question today is as follows; how to obtain features from the bottleneck layer?

I have referred this website. https://github.com/keras-team/keras/issues/2495

The error message I got was shown here; UserWarning: Update your Model call to the Keras 2 API: Model(inputs=[<tf.Tenso..., outputs=[<tf.Tenso...) Model(input=[inputs], output=[intermediate_layer])

Also, I have tried to extract the features by using this method (go see the link below) and it did not work either. https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer

Any comments should be helpful. Thank you!

X = Input(shape=(37310,))

encoded = Dense(encoding_dim, activation='tanh')(X)
decoded = Dense(37310, activation='sigmoid')(encoded)

autoencoder = Model(X, decoded)   
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = Model(encoded_input, decoder_layer(encoded_input))

autoencoder.compile(optimizer='SGD', loss='mean_squared_error')

encoded1 = Dense(500, activation='tanh')(X)
encoded2 = Dense(100, activation='tanh')(encoded1)
encoded3 = Dense(50, activation='tanh')(encoded2)

decoded = Dense(100, activation='tanh')(encoded)
decoded = Dense(500, activation='tanh')(decoded)
decoded = Dense(37310, activation='sigmoid')(decoded)

autoencoder = Model(X, decoded)
autoencoder.compile(optimizer='SGD', loss='mean_squared_error')

autoencoder.fit(X_train, X_train,
            epochs=10,
            batch_size=100,
            shuffle=True,
            validation_data=(X_test, X_test))

model = Model(input=[X], output=[encoded3])

Upvotes: 3

Views: 2216

Answers (1)

Vivek Prajapati
Vivek Prajapati

Reputation: 139

The full code would be like this

encoding_dim = 37310

input_layer = Input(shape=(encoding_dim,))

encoder = Dense(500, activation='tanh')(input_layer)
encoder = Dense(100, activation='tanh')(encoder)
encoder = Dense(50, activation='tanh', name='bottleneck_layer')(encoder)

decoder = Dense(100, activation='tanh')(encoder)
decoder = Dense(500, activation='tanh')(decoder)
decoder = Dense(37310, activation='sigmoid')(decoder)


# full model
model_full = models.Model(input_layer, decoder)

model_full.compile(optimizer='SGD', loss='mean_squared_error')

model_full.fit(X_train, X_train,
            epochs=10,
            batch_size=100,
            shuffle=True,
            validation_data=(X_test, X_test))

# bottleneck model
bottleneck_output = model_full.get_layer('bottleneck_layer').output
model_bottleneck = models.Model(inputs = model_full.input, outputs = bottleneck_output)

bottleneck_predictions = model_bottleneck.predict(X_inference)

Upvotes: 3

Related Questions