keras-team/keras · error · ValueError
q must be in the interval (0, 1)
Error message
q must be in the interval (0, 1)
What it means
Constructor guard on CategoricalGeneralizedCrossEntropy: the q parameter must be strictly between 0 and 1. q blends cross-entropy with MAE to handle noisy labels (q -> 1 gives CE, q -> 0 gives MAE-like behavior); the bounds keep the (1 - f^q)/q formulation well-defined and meaningful.
Solutions
- Choose q in the open interval (0, 1), commonly 0.7 for noisy-label robustness
- Pass the default q=0.5 if unsure
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/losses/losses.py:1593 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/e40ad77c39742f51.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/losses/losses.py:1593
y_pred = np.array([[0.7, 0.3], [0.2, 0.8], [0.6, 0.4], [0.4, 0.6]])
keras.losses.CategoricalGeneralizedCrossEntropy()(y_true, y_pred)
```
References:
- [Zhang, Sabuncu, 2018](https://arxiv.org/abs/1805.07836)
("Generalized Cross Entropy Loss for Training
Deep Neural Networks with Noisy Labels")
"""
def __init__(
self,
q=0.5,
reduction="sum_over_batch_size",
name="categorical_generalized_cross_entropy",
dtype=None,
):
if not 0 < q < 1:
raise ValueError("q must be in the interval (0, 1)")
super().__init__(
categorical_generalized_cross_entropy,
name=name,
reduction=reduction,
dtype=dtype,
q=q,
)
self.q = q
def get_config(self):
config = Loss.get_config(self)
config.update(
{
"q": self.q,
}
)
return config
View on GitHub (pinned to 7a34a03db6)