Search code examples
tensorflowkerasrankdimension

The input dimension of the LSTM layer in Keras


I'm trying keras.layers.LSTM. The following code works.

#!/usr/bin/python3
import tensorflow as tf
import numpy as np
from tensorflow import keras

data = np.array([1, 2, 3]).reshape((1, 3, 1)) 
x = keras.layers.Input(shape=(3, 1)) 
y = keras.layers.LSTM(10)(x)
model = keras.Model(inputs=x, outputs=y)

print (model.predict(data))

As shown above, the input data shape is (1, 3, 1), and the actual input shape in the Input layer is (3, 1). I'm a little bit confused about this inconsistency of the dimension. If I use the following shape in the Input layer, it doesn't work:

x = keras.layers.Input(shape=(1, 3, 1)) 

The error message is as follows:

ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 1, 3, 1]

It seems that the rank of the input must be 3, but why should we use a rank-2 shape in the Input layer?


Solution

  • Keras works with "batches" of "samples". Since most models use variable batch sizes that you define only when fitting, for convenience you don't need to care about the batch dimension, but only with the sample dimension.

    That said, when you use shape = (3,1), this is the same as defining batch_shape = (None, 3, 1) or batch_input_shape = (None, 3, 1).

    The three options mean:

    • A variable batch size: None
    • With samples of shape (3, 1).

    It's important to know this distinction especially when you are going to create custom layers, losses or metrics. The actual tensors all have the batch dimension and you should take that into account when making operations with tensors.