#NEWLINE# def grad( self , inp , grads ) : #NEWLINE# #INDENT# ( x , b , y_idx ) = inp #NEWLINE# ( g_nll , g_sm , g_am ) = grads #NEWLINE# dx_terms = [ ] #NEWLINE# db_terms = [ ] #NEWLINE# d_idx_terms = [ ] #NEWLINE# if ( not isinstance ( g_nll . type , DisconnectedType ) ) : #NEWLINE# #INDENT# ( nll , sm ) = crossentropy_softmax_1hot_with_bias ( x , b , y_idx ) #NEWLINE# dx = crossentropy_softmax_1hot_with_bias_dx ( g_nll , sm , y_idx ) #NEWLINE# db = tensor . sum ( dx , axis = [ 0 ] ) #NEWLINE# dx_terms . append ( dx ) #NEWLINE# db_terms . append ( db ) #NEWLINE# #UNINDENT# if ( not isinstance ( g_sm . type , DisconnectedType ) ) : #NEWLINE# #INDENT# ( dx , db ) = softmax_with_bias . grad ( ( x , b ) , ( g_sm , ) ) #NEWLINE# dx_terms . append ( dx ) #NEWLINE# db_terms . append ( db ) #NEWLINE# #UNINDENT# if ( not isinstance ( g_am . type , DisconnectedType ) ) : #NEWLINE# #INDENT# dx_terms . append ( x . zeros_like ( ) ) #NEWLINE# db_terms . append ( b . zeros_like ( ) ) #NEWLINE# d_idx_terms . append ( y_idx . zeros_like ( ) ) #NEWLINE# #UNINDENT# def fancy_sum( terms ) : #NEWLINE# #INDENT# if ( len ( terms ) == 0 ) : #NEWLINE# #INDENT# return DisconnectedType ( ) ( ) #NEWLINE# #UNINDENT# rval = terms [ 0 ] #NEWLINE# for term in terms [ 1 : ] : #NEWLINE# #INDENT# rval = ( rval + term ) #NEWLINE# #UNINDENT# return rval #NEWLINE# #UNINDENT# return [ fancy_sum ( terms ) for terms in [ dx_terms , db_terms , d_idx_terms ] ]