<commit_before>from __future__ import absolute_import
import theano
import theano.tensor as T
import numpy as np
from six.moves import range

epsilon = 1.0e-15

def mean_squared_error(y_true, y_pred, weight=None):
    if weight is not None:
        return T.sqr(weight*(y_pred - y_true)).mean()
    else:
        return T.sqr(y_pred - y_true).mean()

def mean_absolute_error(y_true, y_pred, weight=None):
    if weight is not None:
        return T.abs_(weight*(y_pred - y_true)).mean()
    else:
        return T.abs_(y_pred - y_true).mean()

def squared_hinge(y_true, y_pred, weight=None):
    if weight is not None:
        return T.sqr(T.maximum(1. - weight*(y_true * y_pred), 0.)).mean()
    else:
        return T.sqr(T.maximum(1. - y_true * y_pred, 0.)).mean()

def hinge(y_true, y_pred, weight=None):
    if weight is not None:
        return T.maximum(1. - weight*(y_true * y_pred), 0.).mean()
    else:
        return T.maximum(1. - y_true * y_pred, 0.).mean()

def categorical_crossentropy(y_true, y_pred, weight=None):
    '''Expects a binary class matrix instead of a vector of scalar classes
    '''
    y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)
    # scale preds so that the class probas of each sample sum to 1
    y_pred /= y_pred.sum(axis=1, keepdims=True) 
    cce = T.nnet.categorical_crossentropy(y_pred, y_true)
    if weight is not None:
        # return avg. of scaled cat. crossentropy
        return (weight*cce).mean()
    else:
        return cce.mean()

def binary_crossentropy(y_true, y_pred, weight=None):
    y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)
    bce = T.nnet.binary_crossentropy(y_pred, y_true).mean()
    if weight is not None:
        return (weight*bce).mean()
    else:
        return bce.mean()

# aliases
mse = MSE = mean_squared_error<commit_msg>fixed error in binary crossentropy obj<commit_after>from __future__ import absolute_import
import theano
import theano.tensor as T
import numpy as np
from six.moves import range

epsilon = 1.0e-15

def mean_squared_error(y_true, y_pred, weight=None):
    if weight is not None:
        return T.sqr(weight*(y_pred - y_true)).mean()
    else:
        return T.sqr(y_pred - y_true).mean()

def mean_absolute_error(y_true, y_pred, weight=None):
    if weight is not None:
        return T.abs_(weight*(y_pred - y_true)).mean()
    else:
        return T.abs_(y_pred - y_true).mean()

def squared_hinge(y_true, y_pred, weight=None):
    if weight is not None:
        return T.sqr(T.maximum(1. - weight*(y_true * y_pred), 0.)).mean()
    else:
        return T.sqr(T.maximum(1. - y_true * y_pred, 0.)).mean()

def hinge(y_true, y_pred, weight=None):
    if weight is not None:
        return T.maximum(1. - weight*(y_true * y_pred), 0.).mean()
    else:
        return T.maximum(1. - y_true * y_pred, 0.).mean()

def categorical_crossentropy(y_true, y_pred, weight=None):
    '''Expects a binary class matrix instead of a vector of scalar classes
    '''
    y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)
    # scale preds so that the class probas of each sample sum to 1
    y_pred /= y_pred.sum(axis=1, keepdims=True) 
    cce = T.nnet.categorical_crossentropy(y_pred, y_true)
    if weight is not None:
        # return avg. of scaled cat. crossentropy
        return (weight*cce).mean()
    else:
        return cce.mean()

def binary_crossentropy(y_true, y_pred, weight=None):
    y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)
    bce = T.nnet.binary_crossentropy(y_pred, y_true)
    if weight is not None:
        return (weight*bce).mean()
    else:
        return bce.mean()

# aliases
mse = MSE = mean_squared_error
{'fraction_non_alphanumeric': 0.11818181818181818, 'fraction_numerical': 0.009917355371900827, 'mean_word_length': 3.0210409745293467, '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': '5244154', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1307, 'n_words': 352}