keras-team/keras · error · ValueError
Cannot apply softmax to a tensor that is 1D. Received input
Error message
Cannot apply softmax to a tensor that is 1D. Received input: {x} What it means
Raised by the deprecated backend.softmax when the input tensor's rank is <= 1, i.e. it is a flat vector with no batch/feature axis distinction. Softmax over a 1D tensor is considered ill-defined in this API (it would softmax over the only axis, which is usually a user mistake), so the guard rejects it before calling tf.nn.softmax.
Solutions
- Reshape the 1D vector to 2D before calling softmax, e.g. x = tf.expand_dims(x, axis=0), apply softmax, then squeeze the added axis back
- Double-check upstream data: a rank-1 tensor here often means a batch dimension was lost in an earlier reshape/reduction
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/legacy/backend.py:1910 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/e93cb3c58742d800.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/legacy/backend.py:1910
@keras_export("keras._legacy.backend.sign")
def sign(x):
"""DEPRECATED."""
return tf.sign(x)
@keras_export("keras._legacy.backend.sin")
def sin(x):
"""DEPRECATED."""
return tf.sin(x)
@keras_export("keras._legacy.backend.softmax")
def softmax(x, axis=-1):
"""DEPRECATED."""
if x.shape.rank <= 1:
raise ValueError(
f"Cannot apply softmax to a tensor that is 1D. Received input: {x}"
)
if isinstance(axis, int):
output = tf.nn.softmax(x, axis=axis)
else:
# nn.softmax does not support tuple axis.
numerator = tf.exp(x - tf.reduce_max(x, axis=axis, keepdims=True))
denominator = tf.reduce_sum(numerator, axis=axis, keepdims=True)
output = numerator / denominator
# Cache the logits to use for crossentropy loss.
output._keras_logits = x
return output
@keras_export("keras._legacy.backend.softplus")
def softplus(x):View on GitHub (pinned to 7a34a03db6)