<commit_before>from pylearn2.costs.cost import SupervisedCost, UnsupervisedCost, GeneralCost


class SumOfCosts(GeneralCost):
    """
    Combines multiple costs by summing them.
    """
    def __init__(self, costs):
        """
        Initialize the SumOfCosts object and make sure that the list of costs
        contains only Cost instances.

        Parameters
        ----------
        costs: list
            List of Cost objects
        """
        self.supervised = False
        self.costs = costs
        # Check whether the sum is a supervised cost and if all the costs are
        # Cost instances
        for cost in self.costs:
            if isinstance(cost, Cost):
                if cost.supervised:
                    self.supervised = True
            else:
                raise ValueError("one of the costs is not Cost instance")

    def __call__(self, model, X, Y=None):
        """
        Returns the sum of the costs the SumOfCosts instance was given at
        initialization.

        Parameters
        ----------
        model : pylearn2.models.model.Model
            the model for which we want to calculate the sum of costs
        X : tensor_like
            input to the model
        Y : tensor_like
            the target, if necessary
        """
        # If the sum is a supervised cost, check whether the target was 
        # provided
        if Y is None and self.supervised is True:
            raise ValueError("no targets provided while some of the " + \
                             "costs in the sum are supervised costs")
        sum_of_costs = 0
        for cost in self.costs:
            if cost.supervised:
                sum_of_costs = sum_of_costs + cost(model, X, Y)
            else:
                sum_of_costs = sum_of_costs + cost(model, X)
        return sum_of_costs


class ScaledCost(GeneralCost):
    """
    Represents a given cost scaled by a constant factor.
    """
    def __init__(self, cost, scaling):
        """
        Parameters
        ----------
        cost: Cost
            cost to be scaled
        scaling : float
            scaling of the cost
        """
        self.cost = cost
        self.supervised = cost.supervised
        self.scaling = scaling

    def __call__(self, model, X, Y=None):
        """
        Returns cost scaled by its scaling factor.
        
        Parameters
        ----------
        model : pylearn2.models.model.Model
            the model for which we want to calculate the scaled cost
        X : tensor_like
            input to the model
        Y : tensor_like
            the target, if necessary
        """
        if Y is None and self.supervised is True:
            raise ValueError("no targets provided for a supervised cost")
        if self.supervised:
            return self.scaling * self.cost(model, X, Y)
        else:
            return self.scaling * self.cost(model, X)


class LxReg(GeneralCost):
    """
    L-x regularization term for the list of tensor variables provided.
    """
    def __init__(self, variables, x):
        """
        Initialize LxReg with the variables and scaling provided.

        Parameters:
        -----------
        variables: list
            list of tensor variables to be regularized
        x: int
            the x in "L-x regularization""
        """
        self.supervised = False
        self.variables = variables
        self.x = x

    def __call__(self, model=None, X=None, Y=None):
        """
        Return the scaled L-x regularization term. The optional parameters are
        never used, they're there only to provide an interface consistent with
        both SupervisedCost and UnsupervisedCost.
        """
        Lx = 0
        for var in self.variables:
            Lx = Lx + abs(var ** self.x).sum()
        return Lx<commit_msg>Got rid of Cost references in general_cost.py<commit_after>from pylearn2.costs.cost import SupervisedCost, UnsupervisedCost, GeneralCost


class SumOfCosts(GeneralCost):
    """
    Combines multiple costs by summing them.
    """
    def __init__(self, costs):
        """
        Initialize the SumOfCosts object and make sure that the list of costs
        contains only GeneralCost instances.

        Parameters
        ----------
        costs: list
            List of GeneralCost objects
        """
        self.supervised = False
        self.costs = costs
        # Check whether the sum is a supervised cost and if all the costs are
        # GeneralCost instances
        for cost in self.costs:
            if isinstance(cost, GeneralCost):
                if cost.supervised:
                    self.supervised = True
            else:
                raise ValueError("one of the costs is not GeneralCost instance")

    def __call__(self, model, X, Y=None):
        """
        Returns the sum of the costs the SumOfCosts instance was given at
        initialization.

        Parameters
        ----------
        model : pylearn2.models.model.Model
            the model for which we want to calculate the sum of costs
        X : tensor_like
            input to the model
        Y : tensor_like
            the target, if necessary
        """
        # If the sum is a supervised cost, check whether the target was 
        # provided
        if Y is None and self.supervised is True:
            raise ValueError("no targets provided while some of the " + \
                             "costs in the sum are supervised costs")
        sum_of_costs = 0
        for cost in self.costs:
            if cost.supervised:
                sum_of_costs = sum_of_costs + cost(model, X, Y)
            else:
                sum_of_costs = sum_of_costs + cost(model, X)
        return sum_of_costs


class ScaledCost(GeneralCost):
    """
    Represents a given cost scaled by a constant factor.
    """
    def __init__(self, cost, scaling):
        """
        Parameters
        ----------
        cost: Cost
            cost to be scaled
        scaling : float
            scaling of the cost
        """
        self.cost = cost
        self.supervised = cost.supervised
        self.scaling = scaling

    def __call__(self, model, X, Y=None):
        """
        Returns cost scaled by its scaling factor.
        
        Parameters
        ----------
        model : pylearn2.models.model.Model
            the model for which we want to calculate the scaled cost
        X : tensor_like
            input to the model
        Y : tensor_like
            the target, if necessary
        """
        if Y is None and self.supervised is True:
            raise ValueError("no targets provided for a supervised cost")
        if self.supervised:
            return self.scaling * self.cost(model, X, Y)
        else:
            return self.scaling * self.cost(model, X)


class LxReg(GeneralCost):
    """
    L-x regularization term for the list of tensor variables provided.
    """
    def __init__(self, variables, x):
        """
        Initialize LxReg with the variables and scaling provided.

        Parameters:
        -----------
        variables: list
            list of tensor variables to be regularized
        x: int
            the x in "L-x regularization""
        """
        self.supervised = False
        self.variables = variables
        self.x = x

    def __call__(self, model=None, X=None, Y=None):
        """
        Return the scaled L-x regularization term. The optional parameters are
        never used, they're there only to provide an interface consistent with
        both SupervisedCost and UnsupervisedCost.
        """
        Lx = 0
        for var in self.variables:
            Lx = Lx + abs(var ** self.x).sum()
        return Lx
{'fraction_non_alphanumeric': 0.07616707616707617, 'fraction_numerical': 0.00129315918789603, 'mean_word_length': 1.6093117408906883, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1530951', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1907, 'n_words': 747}