<commit_before>""" Contains custom losses """
import tensorflow as tf

from ..layers import flatten


def dice(targets, predictions):
    """ Dice coefficient

    Parameters
    ----------
    targets : tf.Tensor
        tensor with target values

    predictions : tf.Tensor
        tensor with predicted values

    Returns
    -------
    average loss : tf.Tensor with a single element
    """
    e = 1e-6
    intersection = flatten(targets * predictions)
    loss = -tf.reduce_mean((2. * intersection + e) / (flatten(targets) + flatten(predictions) + e))
    return loss
<commit_msg>Update dice loss<commit_after>""" Contains custom losses """
import tensorflow as tf

from ..layers import flatten


def dice(targets, predictions, weights=1.0, label_smoothing=0, scope=None,
         loss_collection=tf.GraphKeys.LOSSES, reduction=tf.losses.Reduction.SUM_BY_NONZERO_WEIGHTS):
    """ Dice coefficient

    Parameters
    ----------
    targets : tf.Tensor
        tensor with target values

    predictions : tf.Tensor
        tensor with predicted logits

    Returns
    -------
    Tensor of the same type as targets.
    If reduction is NONE, this has the same shape as targets; otherwise, it is scalar.
    """
    e = 1e-6
    predictions = tf.sigmoid(predictions)
    axis = tuple(range(1, targets.shape.ndims))

    if label_smoothing > 0:
        num_classes = targets.shape[-1]
        targets = targets * (1 - label_smoothing) + label_smoothing / num_classes

    intersection = tf.reduce_sum(targets * predictions, axis=axis)
    targets = tf.reduce_sum(targets, axis=axis)
    predictions = tf.reduce_sum(predictions, axis=axis)

    loss = -(2. * intersection + e) / (targets + predictions + e)
    loss = tf.losses.compute_weighted_loss(loss, weights, scope, loss_collection, reduction)
    return loss

{'fraction_non_alphanumeric': 0.10964912280701754, 'fraction_numerical': 0.00712719298245614, 'mean_word_length': 2.8259958071278826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6942794', 'n_tokens_mistral': 543, 'n_tokens_neox': 520, 'n_words': 155}