#NEWLINE# def categorical_crossentropy( coding_dist , true_dist ) : #NEWLINE# #INDENT# '\n    Return the cross-entropy between an approximating distribution and a true\n    distribution.\n\n    .. warning:: THIS FUNCTION IS UNNECESSARILY POLYMORPHIC.\n    We ultimately don\'t want the polymorphism, and will move this function\n    to pylearn.algorithms.cost. The 1hot version will be removed.\n    The length of the documentation here is a form of code smell.\n\n    The cross entropy between two probability distributions measures the average\n    number of bits needed to identify an event from a set of possibilities, if a\n    coding scheme is used based on a given probability distribution q, rather\n    than the "true" distribution p.\n\n    Mathematically it is defined as follows:\n\n    .. math::\n\n        H(p,q) = - \\sum_x p(x) \\log(q(x))\n\n    Parameters\n    ----------\n    coding_dist : a dense matrix\n        Each slice along axis represents one distribution.\n    true_dist : a dense matrix or sparse matrix or integer vector\n        In the case of a matrix argument, each slice along axis represents one\n        distribution. In the case of an integer vector argument, each element\n        represents the position of the \'1\' in a 1-of-N encoding.\n\n    Returns\n    -------\n    tensor of rank one-less-than `coding_dist`\n        The cross entropy between each coding and true distribution.\n\n    Notes\n    -----\n    axis : int\n        The dimension over which each distribution runs\n        (1 for row distributions, 0 for column distributions).\n\n    ' #NEWLINE# if ( true_dist . ndim == coding_dist . ndim ) : #NEWLINE# #INDENT# return ( - tensor . sum ( ( coding_dist * tensor . log ( coding_dist ) ) , axis = ( coding_dist . ndim - 1 ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( true_dist . ndim == ( coding_dist . ndim - 1 ) ) : #NEWLINE# #INDENT# return crossentropy_categorical_1hot ( coding_dist , true_dist ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( 'rank mismatch between coding and true distributions' )