Reputation: 9346
If I want to put the inputs of two networks into an RNN in Keras, how do I accomplish this? For example, assume I have two RNNs A
and B
with their outputs going into RNN C
.
Upvotes: 1
Views: 440
Reputation: 9346
Using keras.layers.merge.concatenate
is required. See the example below:
def build_rnn(x_train, y_train, in_len):
epochs = 100
batch_size = 300
hidden_units = 256
vec_dims = 1
in_shape = (in_len, vec_dims)
inputs = [Input(shape=in_shape, name="input_a"), Input(shape=in_shape, name="input_b")]
merge_outs = []
for inp in inputs:
# stack a few RNNs
net = SimpleRNN(hidden_units, return_sequences=True)(inp)
merge_outs.append(SimpleRNN(hidden_units, return_sequences=True)(net))
merged = Concatenate(axis=-1)(merge_outs)
merged = SimpleRNN(hidden_units, input_shape=(in_len, 2*vec_dims, ), return_sequences=False,
name="pre_out")(merged)
output = Dense(vec_dims, input_shape=(vec_dims,), name='output')(merged)
model = Model(inputs=inputs, outputs=[output])
return model
Upvotes: 1