0
$\begingroup$

I'm trying to write a model with keras, that is built as shown below:

  |                 +-----+
  +->+------+       |     |
+--->|  NN  |------>|     |
|    +------+       |     |
| |                 |     |
| +->+------+       |     |
+--->|  NN  |------>|  L  |
|    +------+       |  S  |------+-----> Output value
|                   |  T  |      |
|      ...          |  M  |      |
| |                 |     |      |
| +->+------+       |     |      |
+--->|  NN  |------>|     |      |
|    +------+       |     |      |
|                   +-----+      |
|    +-------+                   |
+----| Delay |-------------------+
     +-------+

I have several simple sequential models (marked as NN), that receive two numerical value as input, they calculate some other numeric values (one per each network). These values are passed to LSTM network, which produces a single value as an output and this value additionaly is passed to initial networks (possibly with some delay) as one of two inputs. I work with time series, so calculated final value is passed to network alongside with the next time series value.

I use LSTM to store some "state". It is not quite difficult to build separate "sub-models", but I don't realize, how to join them together as I need, i.e. how to make a final output to be passed to initial networks and how to stack them in the described way (not as a chain).

What I've found: I found keras.layers.Concatenate, but it seems not to be what I'm looking for... But maybe (I hope) I'm mistaken.

$\endgroup$
1
  • $\begingroup$ What is the challenge that you faced with keras.layers.Concatenate $\endgroup$
    – 10xAI
    Mar 3 at 7:20
1
$\begingroup$

You can use the below type code.

import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, LSTM, Concatenate
from tensorflow.keras.models import Model

# input of first NN
input_l1 = Input(shape=(2,))
out_l1 = Dense(1)(input_l1)

# input is 2nd NN
input_l2 = Input(shape=(2,))
out_l2 = Dense(1)(input_l2)

# concat layer output shape will be (None, 2) becuase we concatinated 2 dense layer outputs/ 
concat_vec = Concatenate()([out_l1, out_l2])

# we need 3d input to LSTM i.e. ( Batch_Size, no of time steps, feature space)
# We have 2 inputs so expanded dim to (None, 2, 1)
expanded_concat = tf.expand_dims(concat_vec, axis=2)

# LSTM
lstm_out = LSTM(15)(expanded_concat)

model = Model(inputs=[input_l1, input_l2], outputs=lstm_out)
$\endgroup$
1
  • $\begingroup$ I've used Concatenate in a wrong way :) Now it works, thank you! $\endgroup$ Mar 3 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.