Search code examples
pythontensorflowkerasneural-network

How to make separate Keras model with dependent weights?


I have to create a network with keras like in the picture below, where NN - individual neural networks.

enter image description here

The problem is, that they all must have same weights

I can't use shared layers (at least to my understanding), because then one network will get all the inputs in it and I need each to get specially one

Is there any way of doing this?


Solution

  • Use the functional api. You can reuse a layer for different inputs. For example:

    inp1 = Input(...)
    inp2 = Input(...)
    layer1 = Dense(...)
    a1 = layer1(inp1)
    a2 = layer1(inp2)
    

    layer1 will be applied on inp1 and on inp2. It is just one layer instance, the same weights will be used for inp1 and inp2.