Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about Keras RNN Input shape requirement

Tags:

keras

lstm

I have read plenty of posts for this point. They are inconsistent with each other and every answer seems to have a different explanation so I thought to ask based on my analyzing of all of them.

As Keras RNN documentation states, the input shape is always in this form (batch_size, timesteps, input_dim). I am a bit confused about that but I guess, not sure though, that input_dim is always 1 while timesteps depends on your problem (could be the data dimension as well). Is that roughly correct?

The reason for this question is that I always get an error when trying to change the value of input_dim to be my dataset dimension (as input_dim sounds like that!!), so I made an assumption that input_dim represent the shape of the input vector to LSTM at a time. Am I wrong again?

C = C.reshape((C.shape[0], C.shape[1], 1))
tr_C, ts_C, tr_r, ts_r = train_test_split(C, r, train_size=.8)
batch_size = 1000

print('Build model...')
model = Sequential()

model.add(LSTM(8, batch_input_shape=(batch_size, C.shape[1], 1), stateful=True, activation='relu'))
model.add(Dense(1, activation='relu'))

print('Training...')
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(tr_C, tr_r,
          batch_size=batch_size, epochs=1,
          shuffle=True, validation_data=(ts_C, ts_r))

Thanks!

like image 715
Kristofer Avatar asked Nov 13 '17 16:11

Kristofer


People also ask

What should be the dimension of the input to a LSTM layer in Keras?

The input of the LSTM is always is a 3D array. (batch_size, time_steps, seq_len) . The output of the LSTM could be a 2D array or 3D array depending upon the return_sequences argument.

What is input shape in Keras?

In a Keras layer, the input shape is generally the shape of the input data provided to the Keras model while training. The model cannot know the shape of the training data. The shape of other tensors(layers) is computed automatically.

Which task can be considered many to many in RNN types?

Many-to-Many sequence learning can be used for machine translation where the input sequence is in some language, and the output sequence is in some other language. It can be used for Video Classification as well, where the input sequence is the feature representation of each frame of the video at different time steps.

What are the various inputs accepted in a LSTM cell?

The LSTM input layer must be 3D. The meaning of the 3 input dimensions are: samples, time steps, and features. The LSTM input layer is defined by the input_shape argument on the first hidden layer. The input_shape argument takes a tuple of two values that define the number of time steps and features.


1 Answers

Indeed, input_dim is the shape of the input vector at a time. In other words, input_dim is the number of the input features.

It's not necessarily 1, though. If you're working with more than one var, it can be any number.

Suppose you have 10 sequences, each sequence has 200 time steps, and you're measuring just a temperature. Then you have one feature:

  • input_shape = (200,1) -- notice that the batch size (number of sequences) is ignored here
  • batch_input_shape = (10,200,1) -- only in specific cases, like stateful = True, you will need a batch input shape.

Now suppose you're measuring not only temperature, but also pressure and volume. Now you've got three input features:

  • input_shape = (200,3)
  • batch_input_shape = (10,200,3)

In other words, the first dimension is the number of different sequences. The second is the length of the sequence (how many measures along time). And the last is how many vars at each time.

like image 86
Daniel Möller Avatar answered Sep 21 '22 19:09

Daniel Möller