1

I'm messing around with the Keras api in tensorflow, attempting to implement an autoencoder. The sequential model works, but I want to be able to use the encoder (first two layers) and the decoder (last two layers) separately, but using the weights of my already trained model. Is there a way to do this? Do I have to make a custom model?

model = keras.Sequential()
model.add(encoder_1)
model.add(leaky_relu)
model.add(encoder_2)
model.add(leaky_relu2)
model.add(decoder_1)
model.add(leaky_relu3)
model.add(decoder_2)

encoder_model = keras.Sequential()
encoder_model.add(encoder_1)
encoder_model.add(leaky_relu)
encoder_model.add(encoder_2)
encoder_model.add(leaky_relu2)

decoder_model = keras.Sequential()
decoder_model.add(decoder_1)
model.add(leaky_relu3)
decoder_model.add(decoder_2)

I define my models like this but trying to run predict on either the encoder or decoder outputs

'Sequential' object has no attribute '_feed_input_names'

1 Answer 1

2

Yes, you should wrap the encoding and decoding layers in separate Model instances that you call separately. The Keras blogporst on autoencoders should contain everything you need to know: https://blog.keras.io/building-autoencoders-in-keras.html

Sign up to request clarification or add additional context in comments.

3 Comments

This does not seem to work in tensorflow, I've edited my original post with a code sample and error
You want to use the functional api, as in the example, and not the sequential api.
checkout this answer on how to define and run encoder and decoder independently

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.