def categorical_generalized_cross_entropy(y_true, y_pred, q):
    """Computes the Generalized Cross Entropy loss.

    Generalized Cross Entropy (GCE) is a noise-robust loss function that
    provides better robustness against noisy labels than standard cross entropy.
    It generalizes both cross entropy and mean absolute error through
    the parameter q, where values closer to 1 make the loss more robust
    to noisy labels.

    Formula:
    ```python
    loss = (1 - p**q) / q
    ```
    where `p` is the predicted probability for the true class and `q`
    is the noise parameter.

    Args:
        y_true: Ground truth labels. Expected to contain *integer class indices*
            with shape `[batch_size]` or `[batch_size, 1]`.
        y_pred: The predicted class probabilities, with shape
            `[batch_size, num_classes]`.
        q: Float in range `(0, 1)`. It is the noise parameter.
           Controls the behavior of the loss:
            - As `q` approaches 0: Behaves more like cross entropy
            - As `q` approaches 1: Behaves more like mean absolute error

    Returns:
        GCE loss values with shape `[batch_size]`.
    ```

    References:
        - [Zhang, Sabuncu, 2018](https://arxiv.org/abs/1805.07836)
          ("Generalized Cross Entropy Loss for Training
            Deep Neural Networks with Noisy Labels")
    """

    # Convert y_true to integer type and one-hot encode
    y_true_one_hot = ops.one_hot(
        ops.cast(y_true, "int"), num_classes=ops.shape(y_pred)[-1]
    )
    y_true_one_hot = ops.cast(y_true_one_hot, y_pred.dtype)
    # Calculate the probability of the true class
    p = ops.sum(y_pred * y_true_one_hot, axis=-1)

    # Compute the GCE loss for q in (0,1)
    gce_loss = (1 - ops.power(p, q)) / q

    return gce_loss
Computes the Generalized Cross Entropy loss.

    Generalized Cross Entropy (GCE) is a noise-robust loss function that
    provides better robustness against noisy labels than standard cross entropy.
    It generalizes both cross entropy and mean absolute error through
    the parameter q, where values closer to 1 make the loss more robust
    to noisy labels.

    Formula:
    ```python
    loss = (1 - p**q) / q
    ```
    where `p` is the predicted probability for the true class and `q`
    is the noise parameter.

    Args:
        y_true: Ground truth labels. Expected to contain *integer class indices*
            with shape `[batch_size]` or `[batch_size, 1]`.
        y_pred: The predicted class probabilities, with shape
            `[batch_size, num_classes]`.
        q: Float in range `(0, 1)`. It is the noise parameter.
           Controls the behavior of the loss:
            - As `q` approaches 0: Behaves more like cross entropy
            - As `q` approaches 1: Behaves more like mean absolute error

    Returns:
        GCE loss values with shape `[batch_size]`.
    ```

    References:
        - [Zhang, Sabuncu, 2018](https://arxiv.org/abs/1805.07836)
          ("Generalized Cross Entropy Loss for Training
            Deep Neural Networks with Noisy Labels")
    
keras/src/losses/losses.py