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

3
  • 1
    This does not seem to work in tensorflow, I've edited my original post with a code sample and error Commented Jul 30, 2018 at 18:25
  • 1
    You want to use the functional api, as in the example, and not the sequential api.
    – sdcbr
    Commented Jul 31, 2018 at 0:26
  • checkout this answer on how to define and run encoder and decoder independently Commented Oct 15, 2020 at 12:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.