machine learning
neural networks
error debugging
TensorFlow
Conv1D layer

Input 0 of layer conv1d is incompatible with the layer expected min_ndim3, found ndim2. Full shape received None, 30

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 means a Conv1D layer received a 2D tensor, but Conv1D expects a 3D tensor. For one-dimensional convolution, the model needs data shaped like (batch, steps, channels), so an input shaped (None, 30) is missing the final feature or channel dimension.

What Conv1D Expects

A Conv1D layer slides filters across a sequence. That means it needs:

  • batch dimension
  • sequence length or time steps
  • feature or channel dimension

A valid input shape might look like (32, 30, 1) for a batch of 32 sequences, each of length 30, with one feature per step.

Why (None, 30) Fails

The error’s None means the batch size is unspecified at model-build time. That part is normal. The problem is the remaining shape only has one axis, 30, when Conv1D needs two non-batch axes.

So Keras sees:

  • batch axis
  • one sequence-like axis

but no channel axis.

Fix 1: Add a Feature Dimension

If each sequence element has exactly one feature, reshape the data to add a singleton channel dimension.

python
1import numpy as np
2import tensorflow as tf
3
4x = np.random.rand(100, 30).astype("float32")
5x = x.reshape(100, 30, 1)
6
7y = np.random.randint(0, 2, size=(100,))
8
9model = tf.keras.Sequential([
10    tf.keras.layers.Conv1D(16, kernel_size=3, activation="relu", input_shape=(30, 1)),
11    tf.keras.layers.GlobalMaxPooling1D(),
12    tf.keras.layers.Dense(1, activation="sigmoid")
13])
14
15model.compile(optimizer="adam", loss="binary_crossentropy")
16model.fit(x, y, epochs=1, batch_size=16)

That is the most common fix for tabular sequences or one-feature time series.

Fix 2: Set the Correct Input Shape in the Model

Your model definition must agree with the actual data.

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Input(shape=(30, 1)),
3    tf.keras.layers.Conv1D(16, 3, activation="relu"),
4    tf.keras.layers.GlobalMaxPooling1D(),
5    tf.keras.layers.Dense(1, activation="sigmoid")
6])

If the data has shape (samples, 30, 8), then the input shape should be (30, 8) instead.

Fix 3: Use the Right Layer for the Data Type

Sometimes the error is a signal that Conv1D is the wrong layer for the problem. If the input is plain tabular data with no sequence structure, a Dense network may be more appropriate.

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Input(shape=(30,)),
3    tf.keras.layers.Dense(64, activation="relu"),
4    tf.keras.layers.Dense(1, activation="sigmoid")
5])

Do not force a fake sequence model onto data that has no meaningful local ordering just because convolution sounds powerful. If the order of the 30 values is arbitrary rather than temporal or sequential, the right fix may be a different model family rather than a reshaped tensor.

Text and Embedding Example

For token sequences, the channel dimension often comes from an embedding layer.

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Embedding(input_dim=5000, output_dim=64, input_length=30),
3    tf.keras.layers.Conv1D(32, 3, activation="relu"),
4    tf.keras.layers.GlobalMaxPooling1D(),
5    tf.keras.layers.Dense(1, activation="sigmoid")
6])

Here, the embedding outputs a 3D tensor automatically, so Conv1D becomes appropriate. That is one reason text models using token embeddings can adopt Conv1D naturally, while flat feature vectors usually cannot without an explicit sequence interpretation.

Common Pitfalls

A common mistake is adding Conv1D to data shaped (samples, features) without adding or generating a channel dimension. Another is setting input_shape=(30,) for a convolution layer and assuming Keras will guess the missing axis. Developers also sometimes reshape tabular data to (samples, features, 1) mechanically without asking whether neighboring features actually have a sequence meaning.

Summary

  • 'Conv1D expects 3D input: (batch, steps, channels).'
  • '(None, 30) is only 2D, so it is missing the channel or feature axis.'
  • Reshape one-feature sequences to (samples, 30, 1).
  • Make sure the model’s declared input shape matches the real data.
  • If the data is not sequential, a dense model may be the better fix than forcing Conv1D.

Course illustration
Course illustration

All Rights Reserved.