<s> import numpy as np <EOL> from quagga . matrix import Matrix <EOL> from quagga . context import Context <EOL> from quagga . connector import Connector <EOL> class SigmoidCeBlock ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , x , true_labels , mask = None , device_id = None ) : <EOL> self . context = Context ( device_id ) <EOL> device_id = self . context . device_id <EOL> if x . bpropagable : <EOL> self . x , self . dL_dx = x . register_usage ( device_id , device_id ) <EOL> else : <EOL> self . x = x . register_usage ( device_id ) <EOL> self . true_labels = true_labels . register_usage ( device_id ) <EOL> if mask : <EOL> self . mask = mask . register_usage ( device_id ) <EOL> self . probs = Connector ( Matrix . empty_like ( self . x ) ) <EOL> self . loss = None <EOL> def fprop ( self ) : <EOL> self . x . sigmoid ( self . context , self . probs ) <EOL> self . probs . fprop ( ) <EOL> def bprop ( self ) : <EOL> self . dL_dx . add_scaled_subtraction ( self . context , <EOL> <NUM_LIT:1.> / float ( self . probs . nrows ) , <EOL> self . probs , self . true_labels ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . dL_dx . hprod ( self . context , self . mask ) <EOL> def calculate_loss ( self , context ) : <EOL> true_labels_np = self . true_labels . to_host ( context ) <EOL> probs_np = self . probs . to_host ( context ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> mask = self . mask . to_host ( context ) <EOL> context . add_callback ( self . _calculate_ce_loss , <EOL> true_labels_np , probs_np , mask ) <EOL> else : <EOL> context . add_callback ( self . _calculate_ce_loss , <EOL> true_labels_np , probs_np ) <EOL> def _calculate_ce_loss ( self , true_labels_np , probs_np , mask = None ) : <EOL> logs = true_labels_np * np . log ( probs_np + <NUM_LIT> ) + ( <NUM_LIT:1.0> - true_labels_np ) * np . log ( <NUM_LIT:1.> - probs_np + <NUM_LIT> ) <EOL> if mask is not None : <EOL> logs *= mask <EOL> self . loss = - np . sum ( logs ) / ( np . sum ( mask ) * logs . shape [ <NUM_LIT:1> ] ) <EOL> else : <EOL> self . loss = - np . mean ( logs ) </s>
