Hungry fool
Hungry fool

Reputation: 27

How to feed multiple outputs of previous layer to a successive lstm layer

I've just started using Keras. I encountered a problem that how to feed multiple outputs of previous layers to a successive lstm layer. My model(part) is just like below:

batch_size = 64

output_1 = Dense(output_dim=(128, ), input_dim=(200, ))

output_2 = Dense(output_dim=(128,), input_dim=(200, ))

output_3 = Dense(output_dim=(128,), input_dim=(200, ))

output_4 = Dense(output_dim=(128,), input_dim=(200, ))

Now my problem is how to feed all above outputs to a lstm ? the input_dim should be like (batch_size, number_of_previous_outputs, 128)

Upvotes: 1

Views: 774

Answers (1)

Thomas Pinetz
Thomas Pinetz

Reputation: 7148

You can do a merge layer (https://keras.io/getting-started/sequential-model-guide/#the-merge-layer). Either with concat or with sum to and then feed the merge layer into the lstm.

code example:

merged_output = merge([output_1, output_2, output_3, output_4], mode='concat', concat_axis=1)
lstm = LSTM(...)(merged_output)

Upvotes: 1

Related Questions