ValueError
LSTM
Deep Learning
Input Dimensions
Python Error

ValueError Error when checking input expected lstm_1_input to have 3 dimensions, but got array with shape 10, 1

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This Keras error appears when an LSTM layer receives a 2D array but expects a 3D tensor. The fix is not just "reshape until it works"; you need to understand what each LSTM dimension means so the model reflects the real structure of your sequence data.

Why LSTM Input Must Be 3D

An LSTM expects input shaped as:

(samples, timesteps, features)

Those dimensions mean:

  • 'samples: how many sequences you pass to the model'
  • 'timesteps: how many positions each sequence contains'
  • 'features: how many values exist at each timestep'

If your array has shape (10, 1), Keras sees only two dimensions:

  • 10 rows
  • 1 column

That is not enough for an LSTM because the layer needs to know both sequence length and feature count.

A Minimal Working Example

Suppose each training example is a sequence of five numbers, and each timestep has one feature. Ten training samples then need shape (10, 5, 1).

python
1import numpy as np
2from tensorflow import keras
3
4x = np.array([
5    [1, 2, 3, 4, 5],
6    [2, 3, 4, 5, 6],
7    [3, 4, 5, 6, 7],
8    [4, 5, 6, 7, 8],
9    [5, 6, 7, 8, 9],
10    [6, 7, 8, 9, 10],
11    [7, 8, 9, 10, 11],
12    [8, 9, 10, 11, 12],
13    [9, 10, 11, 12, 13],
14    [10, 11, 12, 13, 14],
15], dtype="float32")
16
17y = np.array([6, 7, 8, 9, 10, 11, 12, 13, 14, 15], dtype="float32")
18
19x = x.reshape((10, 5, 1))
20
21model = keras.Sequential([
22    keras.layers.Input(shape=(5, 1)),
23    keras.layers.LSTM(16),
24    keras.layers.Dense(1)
25])
26
27model.compile(optimizer="adam", loss="mse")
28model.fit(x, y, epochs=2, verbose=0)

This works because:

  • there are 10 samples
  • each sample has 5 timesteps
  • each timestep has 1 feature

Why Shape (10, 1) Usually Fails

If you pass:

python
x_bad = np.arange(10).reshape((10, 1)).astype("float32")

then Keras cannot infer whether:

  • you have 10 sequences of length 1 with a missing feature dimension
  • one sequence of length 10 with one feature
  • some other incorrect reshaping entirely

That ambiguity is why the LSTM rejects the input.

How to Reshape Correctly

The right reshape depends on the meaning of your data.

Case 1: each row is one sequence with one feature per timestep

python
x = x.reshape((num_samples, num_timesteps, 1))

Case 2: each timestep contains multiple features

python
x = x.reshape((num_samples, num_timesteps, num_features))

For example, if you have 10 samples, 4 timesteps, and 3 features per timestep:

python
1import numpy as np
2
3x = np.arange(10 * 4 * 3, dtype="float32").reshape((10, 4, 3))
4print(x.shape)

Output:

text
(10, 4, 3)

The model input definition must match:

python
keras.layers.Input(shape=(4, 3))

Choosing the Correct Model Input Shape

The Input shape for an LSTM excludes the batch dimension. So if the actual training tensor is (10, 5, 1), the model should declare:

python
keras.layers.Input(shape=(5, 1))

Do not write (10, 5, 1) there. The leading dimension is the number of samples in the current batch and should stay flexible.

Also note that many tutorials use:

python
keras.layers.LSTM(16, input_shape=(5, 1))

That is valid, but an explicit Input layer is often clearer.

Common Pitfalls

The most common mistake is confusing rows with timesteps. A 2D array may look correct in NumPy, but an LSTM does not treat it like a normal tabular model.

Another mistake is forcing a reshape that changes the meaning of the data. If you reshape (10, 1) into (1, 10, 1), the code may run, but you have changed ten samples into one sequence. That is only correct if the data truly represents one sequence.

A third issue is mixing LSTM models with tabular features that have no temporal order. If your dataset is ordinary independent rows, a dense network may be more appropriate than an LSTM.

Finally, remember that the target array y must also match the model output. Fixing the LSTM input shape does not help if the labels are incorrectly shaped for the chosen loss function.

Summary

  • LSTMs require 3D input shaped as (samples, timesteps, features).
  • An array with shape (10, 1) is only 2D, so Keras rejects it.
  • Reshape based on the real meaning of your sequence data, not by guesswork.
  • Define the model input as (timesteps, features), excluding the batch dimension.
  • If your data is not sequential, consider using a non-LSTM model instead.

Course illustration
Course illustration

All Rights Reserved.