<s> import numpy as np <EOL> import cudarray as ca <EOL> from . base import PickleMixin <EOL> _FLT_MIN = np . finfo ( ca . float_ ) . tiny <EOL> class Loss ( PickleMixin ) : <EOL> @ classmethod <EOL> def from_any ( cls , arg ) : <EOL> if isinstance ( arg , Loss ) : <EOL> return arg <EOL> elif isinstance ( arg , str ) : <EOL> if arg == '<STR_LIT>' : <EOL> return SoftmaxCrossEntropy ( ) <EOL> elif arg == '<STR_LIT>' : <EOL> return BinaryCrossEntropy ( ) <EOL> elif arg == '<STR_LIT>' : <EOL> return MeanSquaredError ( ) <EOL> raise ValueError ( '<STR_LIT>' % arg ) <EOL> def setup ( self , pred_shape , target_shape = None ) : <EOL> pass <EOL> def loss ( self , pred , target ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def grad ( self , pred , target ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class SoftmaxCrossEntropy ( Loss ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _tmp_x = None <EOL> self . _tmp_y = None <EOL> self . n_classes = None <EOL> def setup ( self , pred_shape , target_shape = None ) : <EOL> self . n_classes = pred_shape [ <NUM_LIT:1> ] <EOL> def _softmax ( self , x ) : <EOL> if self . _tmp_x is not x : <EOL> self . _tmp_y = ca . nnet . softmax ( x ) <EOL> self . _tmp_x = x <EOL> return self . _tmp_y <EOL> def loss ( self , pred , target ) : <EOL> pred = self . _softmax ( pred ) <EOL> target = ca . nnet . one_hot_encode ( target , self . n_classes ) <EOL> return ca . nnet . categorical_cross_entropy ( y_pred = pred , y_true = target ) <EOL> def grad ( self , pred , target ) : <EOL> pred = self . _softmax ( pred ) <EOL> target = ca . nnet . one_hot_encode ( target , self . n_classes ) <EOL> return - ( target - pred ) <EOL> def fprop ( self , x ) : <EOL> return ca . nnet . one_hot_decode ( self . _softmax ( x ) ) <EOL> def y_shape ( self , x_shape ) : <EOL> return ( x_shape [ <NUM_LIT:0> ] , ) <EOL> class BinaryCrossEntropy ( Loss ) : <EOL> def loss ( self , pred , target ) : <EOL> pred = ca . maximum ( pred , _FLT_MIN ) <EOL> return - ca . sum ( target * ca . log ( pred ) + ( <NUM_LIT:1> - target ) * ca . log ( <NUM_LIT:1> - pred ) , <EOL> axis = <NUM_LIT:1> ) <EOL> def grad ( self , pred , target ) : <EOL> pred = ca . maximum ( pred , _FLT_MIN ) <EOL> return - ( target / pred - ( <NUM_LIT:1> - target ) / ( <NUM_LIT:1> - pred ) ) <EOL> class MeanSquaredError ( Loss ) : <EOL> def __init__ ( self ) : <EOL> self . n_feats = None <EOL> def setup ( self , pred_shape , target_shape = None ) : <EOL> self . n_feats = pred_shape [ <NUM_LIT:1> ] <EOL> def loss ( self , pred , target ) : <EOL> return ca . mean ( ( target - pred ) ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) <EOL> def grad ( self , pred , target ) : <EOL> return <NUM_LIT> / self . n_feats * ( pred - target ) </s>
