0
votes

I created the following network. The idea is to combine the outputs of the left and right, then send to a LSTM model.

EMBED_DIM = 4
look_back = 6
feature_num = 2
ENCODE_DIM = 676

left = Sequential()
left.add(Dense(EMBED_DIM,input_shape=(ENCODE_DIM,)))
left.add(RepeatVector(look_back))
left.add(Reshape((look_back,EMBED_DIM)))

right = Sequential()
right.add(Lambda(lambda x: x,input_shape=(look_back,feature_num)))


# create and fit the LSTM network
model = Sequential()
model.add(Concatenate([left, right], axis = 2,input_shape=(look_back, EMBED_DIM + feature_num) ))
model.add(LSTM(8, input_shape=(look_back,feature_num + EMBED_DIM)))

model.add(Dense(2))
model.compile(loss='mean_squared_error', optimizer='adam')

I am trying to concatenate the output from left and right, then send the new tensor to the LSTM model.

However, I got the following error:


TypeError                                 Traceback (most recent call last)
<ipython-input-156-275f5597cdad> in <module>()

---> 37 model.add(Concatenate([left, right], axis = 2,input_shape=(look_back, EMBED_DIM + feature_num) ))
     38 model.add(LSTM(8, input_shape=(look_back,feature_num + EMBED_DIM)))
     39 

TypeError: __init__() got multiple values for argument 'axis'

Any idea what I did wrong? Can I add a Concatenate layer as the first layer of a model? Thanks!

1

1 Answers

2
votes

Sequential models are not meant to have branches. Use the functional API model.

Let's get the "tensors" from the left and right side:

leftOutput = left.output    
rightOutput = right.output

Now, the Concatenate is a layer that follows the same logic of all other layers. (First you create it, then you call it with the input tensors):

#first parentheses: create the layer / second parentheses: call the layer with inputs
output = Concatenate(axis=2)([leftOutput,rightOutput])

Let's keep the rest of the model as a functional API as well:

output = LSTM(8)(output)
output = Dense(2)(output)

Now we create the model, telling it what the inputs and outputs are:

inputTensorLeft = left.input
inputTensorRight = right.input    

fullModel = Model([inputTensorLeft,inputTensorRight], output)

Notice you ended up with three models, but one of them contains the other two. They share the same weights. Training one will train the others if you're training a shared path.