Keras LSTM: Create a daily vector of event occurences using vectors from the past days

Viewed 54

I have just started with implementing a LSTM in Python with Tensorflow / Keras to test out an idea I had, however I am struggling to properly create a model.

For each day, I want to predict which of a group of events will occur. The idea is that some events are recurring / always occur after a certain amount of time has passed, whereas other events occur only rarely or without any structure. A LSTM should be able to pick up on these recurring events, in order to predict there occurences for days in the future.

In order to display the events, I use a list with values 0 and 1 (non-occurence and occurence). So for example if I have the events ["Going to school", "Going to the gym" , "Buying a computer"] I have lists like [1, 0, 1], [1, 1, 0], [1, 0, 1], [1, 1, 0] etc. The idea is then that the LSTM will recognize that I go to school every day, the gym every other day and that buying a computer is very rare. So following the sequence of vectors, for the next day it should predict [1,0,0].

I am not sure what is the best way to implement this in a LSTM / code, but so far I have done the following:

  1. Create x_train, which is a numpy array with shape (363,193). It contains 363 lists with the same 193 events.
  2. Create y_train, which also is an array with shape (363,193). These lists are one day ahead of x_train. So e.g. x_train[1] == y_train[0] evaluates to all True.

I do not want my model to take as input day one and predict day two. I would like to be able to give it training data like day 1,2,3,...,365 and then have it create a 0-1 vector for day 366,367,... by itself. I have found many articles online, but they all seem to be focused on predicting a single value instead of a full vector.

One implementation I tried is the following:

model.add(layers.LSTM(100,input_shape=(363, 193), return_sequences=True))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(1, activation='linear'))
model.compile(loss='mse',optimizer='rmsprop')
model.fit(x_train,y_train)

ValueError: Input 0 of layer "sequential_27" is incompatible with the layer: expected shape=(None, 363, 193), found shape=(None, 193)

In order to get at least a working model I have tried to play around with the input shape the most, but to me it seems correct as my input_shape is the same as x_train.shape. I also set return_sequences as True because I believe I want to make use of a many to many relationship.

If someone could give me some help with how to properly create the LSTM, I would greatly appreciate it.

1 Answers

Multiple samples are needed to train the network, now there's only one with window size of 363. Reshaping x_train to (1, 363, 193) would fix the input shape problem, but the root problem is still the same, there's only one training sample. I suggest to give https://www.tensorflow.org/api_docs/python/tf/data/Dataset#window a try.

Related