keras-team/keras · error · ValueError

`x` and `weights` BCOOs must have the same indices

Error message

`x` and `weights` BCOOs must have the same indices

What it means

Raised in sparse bincount on JAX when both x and weights are BCOO tensors but their .indices attributes differ. The sparse weighted count relies on value-by-value index alignment, so mismatched sparsity patterns fail this consistency guard.

Solutions

  1. Build weights with exactly the same BCOO indices as x (e.g. derive weights from x's structure)
  2. Densify both x and weights and call bincount without sparse=True
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/jax/numpy.py:97 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/222294f745ca6fe5. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/jax/numpy.py:97


def kaiser(x, beta):
    x = convert_to_tensor(x)
    return cast(jnp.kaiser(x, beta), config.floatx())


def bincount(x, weights=None, minlength=0, sparse=False):
    # Note: bincount is never traceable / jittable because the output shape
    # depends on the values in x.
    if sparse or isinstance(x, jax_sparse.BCOO):
        if isinstance(x, jax_sparse.BCOO):
            if weights is not None:
                if not isinstance(weights, jax_sparse.BCOO):
                    raise ValueError("`x` and `weights` must both be BCOOs")
                if x.indices is not weights.indices:
                    # This test works in eager mode only
                    if not jnp.all(jnp.equal(x.indices, weights.indices)):
                        raise ValueError(
                            "`x` and `weights` BCOOs must have the same indices"
                        )
                weights = weights.data
            x = x.data
        reduction_axis = 1 if len(x.shape) > 1 else 0
        maxlength = jnp.maximum(jnp.max(x) + 1, minlength)
        one_hot_encoding = nn.one_hot(x, maxlength, sparse=True)
        if weights is not None:
            expanded_weights = jnp.expand_dims(weights, reduction_axis + 1)
            one_hot_encoding = one_hot_encoding * expanded_weights

        outputs = jax_sparse.bcoo_reduce_sum(
            one_hot_encoding,
            axes=(reduction_axis,),
        )
        return outputs
    if len(x.shape) == 2:
        if weights is None:

View on GitHub (pinned to 7a34a03db6)