tf.keras.losses.dice

Computes the Dice loss value between y_true and y_pred.

Formula:

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

y_true tensor of true targets.
y_pred tensor of predicted targets.
axis tuple for which dimensions the loss is calculated

Dice loss value.

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)