Keras
input_dim
sequential model
machine learning
neural networks

How to calculate input_dim for a keras sequential model?

Master System Design with Codemia

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

Introduction

In a Keras Sequential model, input_dim for a dense first layer is simply the number of features in each training example. It is not the number of rows in the dataset, not the batch size, and not the number of output classes.

For tabular data, input_dim equals the feature count

If your training matrix has shape:

text
(num_samples, num_features)

then:

  • 'num_samples is how many examples you have'
  • 'num_features is the value you use for input_dim'

Example:

python
1import numpy as np
2import tensorflow as tf
3
4x = np.random.rand(1000, 20).astype("float32")
5y = np.random.randint(0, 2, size=(1000, 1)).astype("float32")
6
7model = tf.keras.Sequential([
8    tf.keras.layers.Dense(32, activation="relu", input_dim=20),
9    tf.keras.layers.Dense(1, activation="sigmoid"),
10])

Here each sample has 20 features, so input_dim=20.

Prefer input_shape in modern Keras

input_dim still works for simple dense layers, but modern code usually uses input_shape because it generalizes more clearly:

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Dense(32, activation="relu", input_shape=(20,)),
3    tf.keras.layers.Dense(1, activation="sigmoid"),
4])

For a one-dimensional feature vector, input_shape=(20,) means exactly the same thing as input_dim=20.

The advantage is consistency. Once you move to sequences, images, or other structured inputs, input_shape is the more natural way to think.

Do not confuse features with samples or classes

This is the mistake that causes most shape errors.

Suppose your dataset has:

  • 5,000 rows
  • 12 input columns
  • 3 target classes

Then:

  • 'input_dim is 12'
  • not 5000
  • not 3

The model consumes one row at a time conceptually, and each row contains 12 values.

Sequences and images are different

input_dim is mainly a shorthand for flat dense input. For time series, text sequences, or images, you usually describe the full input shape instead.

Sequence example:

python
1model = tf.keras.Sequential([
2    tf.keras.layers.LSTM(32, input_shape=(50, 8)),
3    tf.keras.layers.Dense(1),
4])

This means:

  • 50 time steps
  • 8 features per step

Image example:

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Conv2D(16, 3, activation="relu", input_shape=(28, 28, 1)),
3    tf.keras.layers.Flatten(),
4    tf.keras.layers.Dense(10, activation="softmax"),
5])

In those cases, asking for a single input_dim is too simplistic because the data is not just one flat vector.

A quick way to compute it from NumPy or pandas

If you already have your feature matrix loaded, inspect the second dimension:

python
input_dim = x.shape[1]
print(input_dim)

For a pandas DataFrame:

python
input_dim = df.shape[1]

This works only after you have finished preprocessing. If you later one-hot encode categories or expand text features, the effective input dimension changes.

Common Pitfalls

The biggest mistake is using the number of samples as input_dim. The model does not take the whole dataset as one input vector; it takes one sample at a time.

Another mistake is forgetting that preprocessing changes the feature count. One-hot encoding, embeddings, flattening, and feature engineering all affect the final input size.

Developers also use input_dim on sequence or image models where input_shape would be clearer and less error-prone.

Finally, do not confuse the input dimension with the number of output labels. Input shape describes the data coming in, not the classes being predicted.

When in doubt, print the feature matrix shape right before model construction and derive the value from the processed tensor instead of from memory or spreadsheet notes.

Summary

  • For dense tabular input, input_dim is the number of features per sample.
  • In modern Keras, input_shape=(num_features,) is usually clearer than input_dim=num_features.
  • Do not use the number of samples or classes as the input dimension.
  • For sequences and images, specify the full shape rather than a single flat dimension.
  • Compute the value from the post-processed feature matrix, not from the raw dataset assumptions.

Course illustration
Course illustration

All Rights Reserved.