keras-team/keras · error · ValueError
Axis must be specified when shapes of a and weights differ.
Error message
Axis must be specified when shapes of a and weights differ.
What it means
Raised by keras.ops.numpy.average in the OpenVINO backend when 1-D weights are combined with a multi-dimensional input but no single reduction axis is given. Weighted averaging needs to know which axis the weights align with, so axis must be specified (or be a single-element list/tuple).
Solutions
- Pass axis (an int or single-element list/tuple) naming the dimension the 1-D weights apply to
- Use weights whose shape matches the full input shape so no axis is needed
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/openvino/numpy.py:844 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/f513d9ce88edf298.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/openvino/numpy.py:844
if weights is not None:
weights = get_ov_output(weights)
if weights_keras is not None:
x_shape = (
x_keras.shape
if hasattr(x_keras, "shape")
else x.get_partial_shape()
)
weights_shape = (
weights_keras.shape
if hasattr(weights_keras, "shape")
else weights.get_partial_shape()
)
if len(weights_shape) == 1 and len(x_shape) > 1:
if axis is None or (
isinstance(axis, (list, tuple)) and len(axis) != 1
):
raise ValueError(
"Axis must be specified when shapes of a and weights "
"differ."
)
axis_val = axis[0] if isinstance(axis, (list, tuple)) else axis
axis_val = canonicalize_axis(axis_val, len(x_shape))
if weights_shape[0] != x_shape[axis_val]:
raise ValueError(
"Shape of weights must be consistent with shape of a "
"along specified axis."
)
elif x_shape != weights_shape:
if axis is None:
raise ValueError(
"Axis must be specified when shapes of a and weights "
"differ."
)
raise ValueError(
"Shape of weights must be consistent with shape of a "View on GitHub (pinned to 7a34a03db6)