Search code examples
pythontensorflowkeraslstmkeras-layer

Keras 3D target prediction


I need a model to output regression targets of shape (3,2), where 3 is number of events, and each event has X and Y coordinates. The input data can span 1 or more time steps. Here is an examples of my model for the case when input has one time step.

model = Sequential()
model.add(Embedding(vocab_size + 1, embedding, input_length=1))
model.add(LSTM(hidden, recurrent_dropout=0.1, return_sequences=True))
model.add(Flatten())
model.add(RepeatVector(3))
model.add(LSTM(2, return_sequences=True))

The model compiles and runs, however it produces the same value for X, Y across 3 events. I think this is because of RepeatVector layer. How else can I make sure that my output is (None, 3, 2)?


Solution

  • First a hint: if one "sentence" is supposed to generate the 3 events, I think you should not use input_length=1, but input_length=lengthOfTheSentences.

    It's not an advantage to have an LSTM layer that processes a sequence of length 1.

    You're right to say that the RepeatVector is causing identical results 3 times.

    Now, depending on "how is your model supposed to detect those events", a different approach may be better.

    • Are these sentences "a sequence where the events may be found 'along'"?
    • Are these sentences "an idea that determines the events statically"?

    Following the second approach, I'd say you could use the first LSTM like this:

    LSTM(hidden, return_sequence=False)    
    

    Where hidden must be 6. (You may use more hidden layers whith return_sequences=True, but it's important that the last has the amount of outputs compatible with 6).

    Then you reshape the result in the form of your events:

    Reshape((3,2))
    

    Example:

    model = Sequential()
    model.add(Embedding(vocab_size + 1, embedding, input_length=sentenceLength))
    model.add(LSTM(hidden1, recurrent_dropout=0.1, return_sequences=True))
    model.add(LSTM(hidden2, recurrent_dropout=0.1, return_sequences=True))
    model.add(LSTM(hidden3, recurrent_dropout=0.1, return_sequences=True))
    model.add(LSTM(6, recurrent_dropout=0.1, return_sequences=False))
    model.add(Reshape((3,2)))