2

I’ve get trouble for understand the LSTM input in keras. How I can transform a very simple serie 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 in a input matrix for it can predict next number?

I’m found that diagram

enter image description here

And I read some tutorials that they say matrix shape it’s like

[[0 1 2 3 4]
 [1 2 3 4 5]
 [2 3 4 5 6]
 [3 4 5 6 7]
 [4 5 6 7 8]]

I’ve made it with

list1 = []
for i in range(0, 10):
    list1.append(i)
list2 = []
list3 = []

for i in range(0, len(list1) - 5):
    list2.append(list1[i:i + 5])

list2 = np.array(list2)

But what does rows and columns represent respect that diagram?

mb0850
  • 574
  • 4
  • 13
Caeta
  • 431
  • 3
  • 14

1 Answers1

0

The Keras LSTM layer has the following call argument doc :

Call arguments:

inputs: A 3D tensor with shape [batch, timesteps, feature].

The Matrix that you gave is of 2D shape (5, 5) i.e. 5 rows and 5 columns.

Since you are using LSTM, let me give you an example with text data:

Cat sat on the dog

The above sentence has 5 words, each word can be vectorized into 5 dimensions (Word Embedding). This makes your sentence become a tensor of 2D shape of (5,5) Now you can have multiple sentences.

  • Dog sat on the cat
  • Cat ate the dog food
  • Dog ate the cat food

So now you have total 4 sentences which is your batch. Each of these 4 sentence will have a tensor of (5,5) since all have 5 words in them and you are encoding each word with 5 dimensions.

Thus your whole dataset has the shape:

[batch, timesteps, feature] == [4, 5, 5]

You can now feed this 3D tensor into your LSTM layer.

In case you just want to feed that 2D matrix you will have to expand the dimension using np.expand_dims() to become (1, 5, 5) 3D shape

list2 = np.expand_dim(list2, axis=0)
print(list2)
print(list2.shape)
[[[0 1 2 3 4]
  [1 2 3 4 5]
  [2 3 4 5 6]
  [3 4 5 6 7]
  [4 5 6 7 8]]]
(1, 5, 5)

Also refer to Understanding Keras LSTMs

mb0850
  • 574
  • 4
  • 13