Keras
Dense Model
3D Array Error
Machine Learning
Neural Networks

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.

Practice ML system design

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:

  1. 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design

All Rights Reserved.