首页 > 解决方案 > Keras 1.2.2 model inside other model

问题描述

I have a working piece of Keras 2.3 code which builds : an encoder, a decoder and merge them into a single Model (autoencoder) using Functional API and this method :

def merge_encoder_decoder(encoderModel, decoderModel):
    input_img = encoderModel.input
    output = decoderModel(encoderModel.output)
    return Model(input_img, output)

Doing this, when I train the merged model, the weights are also updated in the encoder and decoder models. I need this behaviour as I use the encoder alone in a later process.

Now I want to try to distribute the train process of this model over a Hadoop cluster. For this, I've chosen the Analytics Zoo (0.6.0) framework, enabling distributed training using Spark and providing some high level APIs to build models.

My problem is : Analytics Zoo supports up to Keras 1.2.2 and I can't find a way to reproduce the behaving of my merge_encoder_decoder function in this version. More specifically, in Keras 1.2.2, Functional models don't seem to have ".input" nor ".output" attributes (that are supposed to return corresponding Tensors), and I havn't found any documentation about these attributes in the Keras 2.3's doc either.

So is there a way to do what I want in Keras 1.2.2 ?

Thanks

标签: pythonkerasneural-network

解决方案


Try using your own explicit Input layer:

def merge_encoder_decoder(encoderModel, decoderModel):
    input_img = layers.Input(image_shape)
    z = encoderModel(input_img)
    output = decoderModel(z)
    return models.Model(input_img, output)

推荐阅读