Search code examples
pythontensorflowkerasdeep-learningkeras-layer

How to do some calculation between the results from the middle layers in keras


If I constructed a keras neural network models look like:

model 1: input -> layer1

model 2: input -> layer1 -> layer2 -> operation -> layer3 -> output

I want to do some calculation between result from layer1 of model1 and result from layer2 of model2, what can I do? I tried to use some Tensorflow codes to do the calculation but failed. Is there any way I can reach my goal?


Solution

  • You can use Keras functional API for this purpose:

    from keras import layers
    from keras import models
    
    # define model 1 architecture
    m1_input = layers.Input(shape=('arbitrary shape'))
    m1_layer1 = layers.SomeLayer()(m1_input)
    
    # define model 2 architecture
    m2_input = layers.Input(shape=('arbitrary shape'))
    m2_layer1 = layers.SomeLayer()(m2_input)
    m2_layer2 = layers.SomeLayer()(m2_layer1)
    merge_m1 = layers.Lambda(SomeOperation)([m1_layer1, m2_layer2])
    m2_layer3 = layers.SomeLayer()(merge_m1)
    m2_output = layers.SomeLayer()(m2_layer3)
    
    # define the final model
    model = models.Model(inputs=[m1_input, m2_input], outputs=[m2_output])
    

    In the above code, SomeOperation should be a function. Take a look at the documentation for more information about Lambda layer in Keras. Alternatively, Keras has some simple built-in merge layers like add or concatenate, which you can use instead of a Lambda layer.