Search code examples
pythontensorflowkerasneural-networkkeras-layer

Keras: How to Combine two Layers, but not Elementwise, into bigger shape


So I want to combine multiple Layers into 1 in my keras-nn. The difference is, that I don't want to combine them like in the Add()-layer, but I want to combine multiple Layers with different shape yet the same dimension into one bigger layer, whose shape is the sum of the input-layers. Here is my very crudly draw structure (The Dots represent a node):

Overview

And here is some code how i would imagine it:

[IN]
input_1 = Input(shape=(4,))
input_2 = Input(shape=(6,))

combined = Combined()([input_1, input_2])
print(input_1.shape, input_2.shape, input_3.shape)


[OUT]
(4,)  (6,)  (10,)

There might be already a Layer in keras that has the functionality, but I browsed the Internet for some time and couldn’t find any answer to this problem

~Okaghana


Solution

  • What you want is the Concatenate layer:

    input_1 = Input(shape=(4,))
    input_2 = Input(shape=(6,))
    
    combined = Concatenate()([input_1, input_2])
    print(input_1.shape, input_2.shape, combined.shape)
    

    This outputs:

    (?, 4) (?, 6) (?, 10)