keras-team/keras · error · ValueError
Arguments `sample_weight` and `class_weight` cannot be…
Error message
Arguments `sample_weight` and `class_weight` cannot be specified at the same time. Received: sample_weight={sample_weight}, class_weight={class_weight} What it means
Mutual-exclusion check in the JAX Trainer.train_on_batch: sample_weight and class_weight are two alternative per-sample weighting mechanisms, and supplying both would make the intended weighting ambiguous, so the trainer rejects the combination when class_weight is not None.
Solutions
- Keep exactly one of the two: per-sample float weights via sample_weight, or class-to-weight dict via class_weight
- Convert a class_weight dict into an explicit sample_weight array if you truly need both semantics
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/jax/trainer.py:760 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/7b196bbd3dd19be6.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/jax/trainer.py:760
break
self.jax_state_sync()
callbacks.on_predict_end()
self._jax_state = None
return tree.map_structure_up_to(batch_outputs, np.concatenate, outputs)
def train_on_batch(
self,
x,
y=None,
sample_weight=None,
class_weight=None,
return_dict=False,
):
self._assert_compile_called("train_on_batch")
if class_weight is not None:
if sample_weight is not None:
raise ValueError(
"Arguments `sample_weight` and `class_weight` "
"cannot be specified at the same time. "
f"Received: sample_weight={sample_weight}, "
f"class_weight={class_weight}"
)
sample_weight = data_adapter_utils.class_weight_to_sample_weights(
y, class_weight
)
def data():
yield _distribute_data((x, y, sample_weight))
# Maybe build model
self._symbolic_build(data_batch=next(data()))
self.make_train_function()
self.reset_metrics()
# Train stepView on GitHub (pinned to 7a34a03db6)