Keras Expected 3 dimensions, but got array with shape - dense model
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Keras is a widely used deep learning library that simplifies building and training neural networks. When using Keras, one common error that many developers encounter is the "Expected 3 dimensions, but got array with shape" error. This article delves into the causes of this error, its implications, and how to resolve it when working with dense models.
Understanding Dimensions in Keras
Input Dimensions in Dense Layers
In Keras, a `Dense` layer is a fully connected layer, typically used in feedforward networks. A dense layer expects a two-dimensional input, with the following shape:
- (number of samples, number of features)
Each row corresponds to a single data sample, and each column corresponds to a feature of the data sample. For example, if you have 1000 samples and each sample has 10 features, your input shape should be (1000, 10).
When Dimensions Mismatch
The error "Expected 3 dimensions, but got array with shape" indicates that there is a misalignment between the expected dimensionality of the input data and the dimensionality that is being provided. This is often caused by confusion when transitioning between different types of layers or misconfiguring the model architecture.
Typical Causes of the Error
Mismatched Layer Expectations
While dense layers expect 2-D input, it is common to mistakenly pass a 3-D input when working with sequence models such as LSTMs or Conv1D layers. Such models inherently handle 3-D data with shape:
- (number of samples, time steps, number of features)
When transitioning from a sequence model to a dense model, it is essential to reshape the data appropriately.
Incorrect Input Shape Declaration
Another potential source of this error arises from incorrect input shape declarations in the initial `Input` or `Dense` layer. Ensuring the input shape aligns with your data is crucial to avoid this issue.
How to Resolve the Error
Reshaping the Input
If you have a 3-D input and need to switch to a dense layer:
- Flattening: Use a `Flatten` layer to convert 3-D tensors to 2-D:
- Verify Input Shape: Check the input shape specified upon model initialization. It should match the data structure.
- Data Preprocessing: Ensure data is correctly preprocessed and normalized to the expected number of dimensions for each layer type.
Related reading
- Keras find out the number of layers
- Keras fit_generator - How does batch for time series work?
- keras fit_generator 'zip' object has no attribute 'shape
- Keras fit model TypeError unhashable type 'numpy.ndarray
- Keras flow_from_directory read only from selected sub-directories
- Keras GaussianNoise layer no effect?
- Keras predict loop memory leak using tf.data.Dataset but not with a numpy array
- Key existence check in HashMap

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.