view cost.py @ 523:111e547ffa7b

modified to use the new implecement of ops and use the new interface to theano.function
author Frederic Bastien <bastienf@iro.umontreal.ca>
date Fri, 14 Nov 2008 16:39:59 -0500
parents f13847478c6d
children
line wrap: on
line source

"""
Cost functions.

@note: All of these functions return one cost per example. So it is your
job to perform a tensor.sum over the individual example losses.

@todo: Make a Cost class, with a particular contract.

@todo: It would be nice to implement a hinge loss, with a particular margin.
"""

import theano.tensor as T
from xlogx import xlogx

def quadratic(target, output, axis=1):
    return T.mean(T.sqr(target - output), axis=axis)

def cross_entropy(target, output, axis=1):
    """
    @todo: This is essentially duplicated as nnet_ops.binary_crossentropy
    @warning: OUTPUT and TARGET are reversed in nnet_ops.binary_crossentropy
    """
    return -T.mean(target * T.log(output) + (1 - target) * T.log(1 - output), axis=axis)

def KL_divergence(target, output):
    """
    @note: We do not compute the mean, because if target and output have
    different shapes then the result will be garbled.
    """
    return -(target * T.log(output) + (1 - target) * T.log(1 - output)) \
            + (xlogx(target) + xlogx(1 - target))
#    return cross_entropy(target, output, axis) - cross_entropy(target, target, axis)