#NEWLINE# @ application ( inputs = [ 'y' , 'x' ] , outputs = [ 'output' ] ) #NEWLINE# def categorical_cross_entropy( self , application_call , y , x ) : #NEWLINE# #INDENT# 'Computationally stable cross-entropy for pre-softmax values.\n\n        Parameters\n        ----------\n        y : :class:`~tensor.TensorVariable`\n            In the case of a matrix argument, each row represents a\n            probabilility distribution. In the vector case, each element\n            represents a distribution by specifying the position of 1 in a\n            1-hot vector.\n        x : :class:`~tensor.TensorVariable`\n            A matrix, each row contains unnormalized probabilities of a\n            distribution.\n\n        Returns\n        -------\n        cost : :class:`~tensor.TensorVariable`\n            A vector of cross-entropies between respective distributions\n            from y and x.\n\n        ' #NEWLINE# x = self . log_probabilities ( x ) #NEWLINE# application_call . add_auxiliary_variable ( x . copy ( name = 'log_probabilities' ) ) #NEWLINE# if ( y . ndim == ( x . ndim - 1 ) ) : #NEWLINE# #INDENT# indices = ( ( tensor . arange ( y . shape [ 0 ] ) * x . shape [ 1 ] ) + self ) #NEWLINE# cost = ( - x . flatten ( ) [ indices ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( y . ndim == x . ndim ) : #NEWLINE# #INDENT# cost = ( - ( x * y ) . sum ( axis = 1 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( 'rank mismatch between x and y' ) #NEWLINE# #UNINDENT# #UNINDENT# return cost