<commit_before>import theano
import theano.tensor as T
from theano.tensor.nnet import binary_crossentropy, categorical_crossentropy


def mse(x, t):
    """Calculates the MSE mean across all dimensions, i.e. feature
     dimension AND minibatch dimension.

    :parameters:
        - x : predicted values
        - t : target values

    :returns:
        - output : the mean square error across all dimensions
    """
    return (x - t) ** 2


class Objective(object):
    _valid_aggregation = {None, 'mean', 'sum'}

    """
    Training objective

    The  `get_loss` method returns cost expression useful for training or
    evaluating a network.
    """
    def __init__(self, input_layer, loss_function=mse, aggregation='mean'):
        """
        Constructor

        :parameters:
            - input_layer : a `Layer` whose output is the networks prediction
                given its input
            - loss_function : a loss function of the form `f(x, t)` that
                returns a scalar loss given tensors that represent the
                predicted and true values as arguments..
            - aggregation : either:
                - `'mean'` or `None` : the mean of the the elements of the
                loss will be returned
                - `'sum'` : the sum of the the elements of the loss will be
                returned
        """
        self.input_layer = input_layer
        self.loss_function = loss_function
        self.target_var = T.matrix("target")
        if aggregation not in self._valid_aggregation:
            raise ValueError('aggregation must be \'mean\', \'sum\', '
                             'or None, not {0}'.format(aggregation))
        self.aggregation = aggregation

    def get_loss(self, input=None, target=None, aggregation=None, **kwargs):
        """
        Get loss scalar expression

        :parameters:
            - input : (default `None`) an expression that results in the
                input data that is passed to the network
            - target : (default `None`) an expression that results in the
                desired output that the network is being trained to generate
                given the input
            - aggregation : None to use the value passed to the
                constructor or a value to override it
            - kwargs : additional keyword arguments passed to `input_layer`'s
                `get_output` method

        :returns:
            - output : loss expressions
        """
        network_output = self.input_layer.get_output(input, **kwargs)
        if target is None:
            target = self.target_var
        if aggregation not in self._valid_aggregation:
            raise ValueError('aggregation must be \'mean\', \'sum\', '
                             'or None, not {0}'.format(aggregation))
        if aggregation is None:
            aggregation = self.aggregation

        losses = self.loss_function(network_output, target)

        if aggregation is None or aggregation == 'mean':
            return losses.mean()
        elif aggregation == 'sum':
            return losses.sum()
        else:
            raise RuntimeError('This should have been caught earlier')


class MaskedObjective(object):<commit_msg>Enhance Objective with the ability to add a regularization function<commit_after>import theano
import theano.tensor as T
from theano.tensor.nnet import binary_crossentropy, categorical_crossentropy


def mse(x, t):
    """Calculates the MSE mean across all dimensions, i.e. feature
     dimension AND minibatch dimension.

    :parameters:
        - x : predicted values
        - t : target values

    :returns:
        - output : the mean square error across all dimensions
    """
    return (x - t) ** 2


class Objective(object):
    _valid_aggregation = {None, 'mean', 'sum'}

    """
    Training objective

    The  `get_loss` method returns cost expression useful for training or
    evaluating a network.
    """
    def __init__(self, input_layer, loss_function=mse, aggregation='mean', regularization_function=None):
        """
        Constructor

        :parameters:
            - input_layer : a `Layer` whose output is the networks prediction
                given its input
            - loss_function : a loss function of the form `f(x, t)` that
                returns a scalar loss given tensors that represent the
                predicted and true values as arguments..
            - aggregation : either:
                - `'mean'` or `None` : the mean of the the elements of the
                loss will be returned
                - `'sum'` : the sum of the the elements of the loss will be
                returned
        """
        self.input_layer = input_layer
        self.loss_function = loss_function
        self.target_var = T.matrix("target")
        if aggregation not in self._valid_aggregation:
            raise ValueError('aggregation must be \'mean\', \'sum\', '
                             'or None, not {0}'.format(aggregation))
        self.aggregation = aggregation
        self.regularization_function = regularization_function

    def get_loss(self, input=None, target=None, aggregation=None, **kwargs):
        """
        Get loss scalar expression

        :parameters:
            - input : (default `None`) an expression that results in the
                input data that is passed to the network
            - target : (default `None`) an expression that results in the
                desired output that the network is being trained to generate
                given the input
            - aggregation : None to use the value passed to the
                constructor or a value to override it
            - kwargs : additional keyword arguments passed to `input_layer`'s
                `get_output` method

        :returns:
            - output : loss expressions
        """
        network_output = self.input_layer.get_output(input, **kwargs)
        if target is None:
            target = self.target_var
        if aggregation not in self._valid_aggregation:
            raise ValueError('aggregation must be \'mean\', \'sum\', '
                             'or None, not {0}'.format(aggregation))
        if aggregation is None:
            aggregation = self.aggregation

        losses = self.loss_function(network_output, target)

        if aggregation is None or aggregation == 'mean':
            loss_fn = losses.mean()
        elif aggregation == 'sum':
            loss_fn = losses.sum()
        else:
            raise RuntimeError('This should have been caught earlier')

        if self.regularization_function is not None:
            loss_fn = loss_fn + self.regularization_function

        return loss_fn


class MaskedObjective(object):
{'fraction_non_alphanumeric': 0.07670706473544192, 'fraction_numerical': 0.0008867868755542418, 'mean_word_length': 1.985002205558006, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1510489', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1628, 'n_words': 623}