<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/losses/losses.py#L2563-L2612) |

Computes the Dice loss value between `y_true` and `y_pred`.

    tf.keras.losses.dice(
        y_true, y_pred, axis=None
    )

#### Formula:

    loss = 1 - (2 * sum(y_true * y_pred)) / (sum(y_true) + sum(y_pred))

<br />

<br />

<br />

| ## Args ||
|---|---|
| `y_true` | tensor of true targets. |
| `y_pred` | tensor of predicted targets. |
| `axis` | tuple for which dimensions the loss is calculated |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| Dice loss value. ||

<br />

#### Example:

    y_true = [[[[1.0], [1.0]], [[0.0], [0.0]]],
              [[[1.0], [1.0]], [[0.0], [0.0]]]]
    y_pred = [[[[0.0], [1.0]], [[0.0], [1.0]]],
              [[[0.4], [0.0]], [[0.0], [0.9]]]]
    axis = (1, 2, 3)
    loss = keras.losses.dice(y_true, y_pred, axis=axis)
    assert loss.shape == (2,)
    loss
    array([0.5, 0.75757575], shape=(2,), dtype=float32)

    loss = keras.losses.dice(y_true, y_pred)
    assert loss.shape == ()
    loss
    array(0.6164384, shape=(), dtype=float32)