#NEWLINE# @ classmethod #NEWLINE# def cost_wrapper( cls , name , cost ) : #NEWLINE# #INDENT# '\n        This layer wraps cost methods by reshaping the tensor (merging\n        the time and batch axis) and then taking out all the masked\n        values before applying the cost method.\n        ' #NEWLINE# @ functools . wraps ( cost ) #NEWLINE# def outer( self , Y , Y_hat ) : #NEWLINE# #INDENT# if self . _requires_reshape : #NEWLINE# #INDENT# if self . _requires_unmask : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ( Y , Y_mask ) = Y #NEWLINE# ( Y_hat , Y_hat_mask ) = Y_hat #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# log . warning ( "Lost the mask when wrapping cost. This can happen if this function is called from within another wrapped function. Most likely this won't cause any problem" ) #NEWLINE# return cost ( self , Y , Y_hat ) #NEWLINE# #UNINDENT# #UNINDENT# input_shape = ( [ ( Y . shape [ 0 ] * Y . shape [ 1 ] ) ] + [ Y . shape [ i ] for i in xrange ( 2 , Y . ndim ) ] ) #NEWLINE# reshaped_Y = Y . reshape ( input_shape ) #NEWLINE# if isinstance ( Y_hat , tuple ) : #NEWLINE# #INDENT# input_shape = [ ( [ ( Y_hat [ j ] . shape [ 0 ] * Y_hat [ j ] . shape [ 1 ] ) ] + [ Y_hat [ j ] . shape [ i ] for i in xrange ( 2 , Y_hat [ j ] . ndim ) ] ) for j in xrange ( len ( Y_hat ) ) ] #NEWLINE# reshaped_Y_hat = [ ] #NEWLINE# for i in xrange ( len ( Y_hat ) ) : #NEWLINE# #INDENT# reshaped_Y_hat . append ( Y_hat [ i ] . reshape ( input_shape [ i ] ) ) #NEWLINE# #UNINDENT# reshaped_Y_hat = tuple ( reshaped_Y_hat ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# input_shape = ( [ ( Y_hat . shape [ 0 ] * Y_hat . shape [ 1 ] ) ] + [ Y_hat . shape [ i ] for i in xrange ( 2 , Y_hat . ndim ) ] ) #NEWLINE# reshaped_Y_hat = Y_hat . reshape ( input_shape ) #NEWLINE# #UNINDENT# if self . _requires_unmask : #NEWLINE# #INDENT# return cost ( self , reshaped_Y [ Y_mask . flatten ( ) . nonzero ( ) ] , reshaped_Y_hat [ Y_mask . flatten ( ) . nonzero ( ) ] ) #NEWLINE# #UNINDENT# return cost ( self , reshaped_Y , reshaped_Y_hat ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return cost ( self , Y , Y_hat ) #NEWLINE# #UNINDENT# #UNINDENT# return outer