keras-team/keras · error · ValueError
axis size must be divisible by 2. Received: x.shape=
Error message
axis size must be divisible by 2. Received: x.shape={x.shape} with axis={axis} What it means
Raised by the shape guard in keras/src/backend/tensorflow/nn.py glu (gated linear unit): GLU splits the tensor along axis into two halves (value and gate), so the size of that axis must be even. The axis dimension of x is odd, and tf.split into 2 equal parts is impossible, hence the error naming x.shape and the offending axis.
Solutions
- Make x.shape[axis] even, e.g. set the layer's units to an even number before GLU
- Use axis=-1 and ensure the last dim (e.g. 2*units) is even
- Check the preceding dense layer output width
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/tensorflow/nn.py:145 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/da83a0665f133a71.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/tensorflow/nn.py:145
return tf.nn.selu(x)
def gelu(x, approximate=True):
x = convert_to_tensor(x)
return tf.nn.gelu(x, approximate=approximate)
def celu(x, alpha=1.0):
return tf.maximum(x, 0.0) + alpha * tf.math.expm1(
tf.minimum(x, 0.0) / alpha
)
def glu(x, axis=-1):
x = convert_to_tensor(x)
canonicalize_axis(axis, len(x.shape))
if x.shape[axis] % 2 != 0:
raise ValueError(
"axis size must be divisible by 2. "
f"Received: x.shape={x.shape} with axis={axis}"
)
x1, x2 = tf.split(x, num_or_size_splits=2, axis=axis)
return x1 * tf.sigmoid(x2)
def hard_tanh(x):
return tf.clip_by_value(x, clip_value_min=-1.0, clip_value_max=1.0)
def hard_shrink(x, threshold=0.5):
return tf.where(tf.abs(x) > threshold, x, tf.zeros_like(x))
def threshold(x, threshold, default_value):
return tf.where(x > threshold, x, default_value)
View on GitHub (pinned to 7a34a03db6)