Keras
TypeError
NoneType
debugging
machine learning error

Keras reports TypeError unsupported operand types for 'NoneType' and 'int'

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 usually means some code tried to do arithmetic with a shape dimension or value that is currently None. In model-building code, None often represents an unknown batch size or an unresolved dimension, so the real fix is usually to stop treating symbolic shape information like a concrete Python integer.

Where None Comes From in Keras

Keras uses None in shapes to mean “this dimension is not fixed yet.” For example:

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(30,))
4print(inputs.shape)

This might print something like (None, 30). The first dimension is batch size, and Keras leaves it unknown so the model can accept varying batch sizes.

Why the TypeError Happens

The error appears when code tries something like:

python
batch_size = inputs.shape[0]
value = batch_size + 1

Since inputs.shape[0] is None, Python raises a TypeError when you try to add 1 to it.

This is not a Keras arithmetic bug. It is a mismatch between symbolic model metadata and ordinary Python integer math.

Common Place 1: Custom Layers

Custom layers often trigger this when they inspect input_shape or tensor.shape too aggressively.

python
1class BadLayer(tf.keras.layers.Layer):
2    def call(self, inputs):
3        width = inputs.shape[0] + 1
4        return inputs

If that dimension is None, the code fails.

A safer pattern is to use TensorFlow runtime shape ops when you need dynamic dimensions.

python
1class GoodLayer(tf.keras.layers.Layer):
2    def call(self, inputs):
3        batch = tf.shape(inputs)[0]
4        return tf.cast(batch, inputs.dtype) * 0 + inputs

tf.shape(inputs)[0] is a tensor that can be evaluated at runtime rather than a Python None.

Common Place 2: Lambda Layers and Manual Reshaping

People also hit this inside lambda functions or custom reshape logic.

python
x = tf.keras.Input(shape=(30,))
# bad = tf.keras.layers.Lambda(lambda t: tf.reshape(t, (t.shape[0] + 1, 30)))(x)

If the batch size is unknown, t.shape[0] is not usable in Python arithmetic. Use tf.shape(t)[0] for runtime-dependent dimensions.

Common Place 3: Misconfigured Layer Arguments

Sometimes the NoneType error comes from passing a missing argument into a layer that later participates in internal shape math. For example, if a layer parameter or custom config is None when the code expects an integer, Keras may eventually reach arithmetic that fails.

That is why reading the full stack trace matters. The top-level error may mention NoneType, but the real origin could be a missing configuration value several lines earlier.

Debugging Strategy

A practical debugging checklist is:

  • print tensor shapes with print(tensor.shape)
  • identify which dimension is None
  • check whether the code is doing Python math on that dimension
  • switch dynamic-dimension logic to tf.shape(...)
  • verify custom layer arguments are not accidentally None

That usually isolates the root cause quickly.

Common Pitfalls

A common mistake is assuming every entry in tensor.shape is a real Python integer. Another is mixing symbolic Keras graph-building logic with eager Python arithmetic inside lambda layers or custom call methods. Developers also often patch around the error by hardcoding a batch size, which makes the model more brittle instead of solving the real problem.

Summary

  • In Keras, None in a shape usually means an unknown dynamic dimension, often the batch size.
  • The error occurs when code tries to use that None in normal Python arithmetic.
  • Use tf.shape(...) for runtime dimensions instead of tensor.shape[...] when the dimension may be dynamic.
  • Inspect custom layers, lambda functions, and reshape code first.
  • Treat the error as a sign that symbolic shape metadata was used like a concrete integer.

Course illustration
Course illustration

All Rights Reserved.