hyperdo
hyperdo

Reputation: 427

Map series of vectors to single vector using LSTM in Keras

Suppose I have a vector of features that changes over time and wanted this to map to a single vector for use as a layer in classification. How would this be implemented in Keras? Example:

Input:  <1.0, 2.0, 3.0>  ->  <1.1, 1.9, 3.2>  ->  ...
Output: <1.0, 0.0, 0.0, 2.0>

The Keras docs say that the LSTM object takes in 3 dimensions, but I'm not sure how to express that in this case.

Upvotes: 2

Views: 686

Answers (1)

parsethis
parsethis

Reputation: 8078

LSTM takes inputs in [batch_size, time_steps, input_dim]. In your case you could treat each vectors as a timestep. So on input with two timesteps would be :

[<1.0, 2.0, 3,0>, <1.1, 1.0, 3.2>] which maps to <1.0, 0.0, 0.0, 2.0>

As an example:

input_dim = 3
num_steps = 2

model = Sequential()
model.add(LSTM(16, input_shape=(num_steps, input_dim)) # output 2d [batch_size, 16]
model.add(Dense(4)) # output [batch, 4]

# ... more layers

Dont be afraid to read the docs they are done well. https://keras.io/layers/recurrent/#lstm

Upvotes: 1

Related Questions