如何在 keras 中建模共享层?

Ami*_*sam 7 python machine-learning keras keras-layer

我想用以下形式训练一个具有共享层的模型:

x --> F(x)
          ==> G(F(x),F(y))
y --> F(y) 
Run Code Online (Sandbox Code Playgroud)

xy是两个独立的输入层,F是一个共享层。G是连接F(x)and后的最后一层F(y)

是否可以在 Keras 中对此进行建模?如何?

tod*_*day 9

为此,您可以使用Keras 函数式 API

from keras.layers import Input, concatenate

x = Input(shape=...)
y = Input(shape=...)

shared_layer = MySharedLayer(...)
out_x = shared_layer(x)
out_y = shared_layer(y)

concat = concatenate([out_x, out_y])

# pass concat to other layers ...
Run Code Online (Sandbox Code Playgroud)

请注意,xandy可以是任何层的输出张量,不一定是输入层。