I want to use an LSTM to predict the next element in a sequence, but I am having trouble wrapping my head around how to formulate the problem, and, more specifically, how to structure the data and present it to the mdel. I should say that I am fairly new to LSTMs – I have […]

Categories

## How do I present the data to a Keras LSTM?

- Post author By Full Stack
- Post date April 4, 2020
- No Comments on How do I present the data to a Keras LSTM?

- Tags ..., ... [f0_99, ('*T*-2', [f0_1, [f0_2, [f0_3, [f0_4, [f0_97, [f0_98, # item 3 = target for training sample 0 [f0_4, # item 4 = target for training sample 1 [f0_5, # item 5 = target for training sample 2 ... [f0_99, # items 0-2 = training sample 0 [[f0_1, # items 1-3 = training sample 1 [[f0_2, # items 2-4 = training sample 2 ... [[f0_96, 2) and y some 2D shape? Will the setting of the time_steps option in the LSTM layer take care of looking back the specified number of time st, also in X, and, and each element in y its target? (The whole list is later split into the training and the test set.) Or can I just add each complete sequen, and give that as input (leaving out the last item in X as it has no y), and this one - but they have only added to my confusion. It seems that this question is related to mine, and where X has shape (total_num_subsamples, and would I have to represent all items, as model.fit() expects a 2D array for the y data. But how can y be a 2D array here? Now to point 2. Here is my code, as one-hot vectors?, batch_input_shape=[batch_size, batch_size=batch_size, but beyond the exercises there have never actually gotten around to try things out. As a refresher I've been reading some of the (generally v, but I am having trouble wrapping my head around how to formulate the problem, but the same applies: I keep getting confused when I try to translate it to my own problem. Let me start out by describing my problem. I hav, but they are longer. However, cut up ... # labels for sequence 49, cut up ... # sequence 149, cut up [[f0_0, cut up [f0_3, cut up ] ... where each element in X is a sample, cut up ] y = [ # labels for sequence 0, do I need to cut up each of the 150 sequences into multiple time-steps-sized subsequences that I then collect in one superlist, epochs=1, f1_0], f1_1], f1_2], f1_3], f1_4], f1_5], f1_96], f1_97], f1_98]] # items 96-98 = training sample 99 # sequence 1, f1_98]] # sequence 1 = training sample 1 # sequence 2 = training sample 2 ... # sequence 149 = training sample 149 ] y, f1_99] # item 99 = target for training sample 96 # labels for sequence 1, f1_99]] # sequence 1 = targets 1 # sequence 2 = targets 2 ... # sequence 149 = targets 149 ] In the second case I get, f1_99]] So far, f1_i]. Let's say that the first sequence contains 100 items; this gives the following picture: [[f0_0, hence the for loop around the call to fit()): batch_size = 1 time_steps = 3 num_features = len(train_X[0][0]) # = 2 epochs = 50 model = Seq, how to structure the data and present it to the mdel. I should say that I am fairly new to LSTMs - I have followed the Coursera course on seq, I want to use an LSTM to predict the next element in a sequence, i.e., it is one of multiple classes. In this case, let's say I have two features representing each item. What I want to do is to predict the next item given an n-long sequence of previous ite, like this (example for the first sequence): X = [ # sequence 0, like this (example for the first sequence): X = [ # sequence 0 = training sample 0 [[f0_0, metrics=['MSE']) for i in range(epochs): history = model.fit(X, more specifically, my initial data consists of 150 sequences of varying length. Each item i in each sequence is represented by two features: [f0_i, num_features, num_features) and y shape (total_num_subsamples, num_features)? Or should I use the simpler format, optimizer=adam, predict the item at time step t given the items at time steps [t-n, respectively. But what do I give as X and y? Should I use the preformatted data, seed=1)) model.add(Dense(num_features)) model.compile(loss='mean_squared_error', sequence_length, shuffle=False) model.reset_states() It seems straightforward that I just set time_steps and num_features to 3 and 2, so good. Now suppose that I want to use a history of three time steps (previous items) to predict the next item - does this mean that I have, stateful=True)) model.add(Dropout(dropout, such as this one, suppose that I am mainly interested in predicting feature 0 at time step t. This feature has a limited number of values, t-1]. As mentioned, the second case breaks the code downstream (see below), there are two (related) things that I am struggling with: How to structure the data. How to feed the data to the LSTM. Let's start with po, this one, time_steps, using X and y as described above (this case is for a stateful model, validation_split=0.2, verbose=2, where X has shape (150, which has the time steps 'encoded' in it, would it make sense to represent each element of y as a one-hot vector encoding the class? Or would the LSTM not be able to learn this (becau, y