<s> from layer import * <EOL> class SoftmaxLayer ( Layer ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SoftmaxLayer , self ) . __init__ ( * args , ** kwargs ) <EOL> @ classmethod <EOL> def IsLayerType ( cls , proto ) : <EOL> return proto . hyperparams . activation == deepnet_pb2 . Hyperparams . SOFTMAX <EOL> def ApplyActivation ( self ) : <EOL> self . state . apply_softmax ( ) <EOL> def Sample ( self ) : <EOL> self . state . perturb_prob_for_softmax_sampling ( target = self . sample ) <EOL> self . sample . choose_max ( axis = <NUM_LIT:0> ) <EOL> def ComputeDeriv ( self ) : <EOL> """<STR_LIT>""" <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def AllocateMemory ( self , batchsize ) : <EOL> super ( SoftmaxLayer , self ) . AllocateMemory ( batchsize ) <EOL> self . expansion_matrix = cm . CUDAMatrix ( np . eye ( self . numlabels ) ) <EOL> def AllocateBatchsizeDependentMemory ( self , batchsize ) : <EOL> super ( SoftmaxLayer , self ) . AllocateBatchsizeDependentMemory ( batchsize ) <EOL> dimensions = self . dimensions <EOL> numlabels = self . numlabels <EOL> self . data = cm . CUDAMatrix ( np . zeros ( ( dimensions , batchsize ) ) ) <EOL> self . deriv = cm . CUDAMatrix ( np . zeros ( ( numlabels * dimensions , batchsize ) ) ) <EOL> self . batchsize_temp = cm . CUDAMatrix ( np . zeros ( ( dimensions , batchsize ) ) ) <EOL> def GetData ( self ) : <EOL> self . expansion_matrix . select_columns ( self . data , target = self . state ) <EOL> def GetLoss ( self , get_deriv = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> perf = deepnet_pb2 . Metrics ( ) <EOL> perf . MergeFrom ( self . proto . performance_stats ) <EOL> perf . count = self . batchsize <EOL> tiny = self . tiny <EOL> batchsize = self . batchsize <EOL> dimensions = self . dimensions <EOL> numlabels = self . numlabels <EOL> state = self . state <EOL> data = self . data <EOL> state . reshape ( ( numlabels , dimensions * batchsize ) ) <EOL> data . reshape ( ( <NUM_LIT:1> , dimensions * batchsize ) ) <EOL> if self . loss_function == deepnet_pb2 . Layer . CROSS_ENTROPY : <EOL> temp = self . batchsize_temp <EOL> state . get_softmax_correct ( data , target = temp ) <EOL> perf . correct_preds = temp . sum ( ) <EOL> state . get_softmax_cross_entropy ( data , target = temp , tiny = tiny ) <EOL> perf . cross_entropy = temp . sum ( ) <EOL> if get_deriv : <EOL> state . apply_softmax_grad ( data , target = self . deriv ) <EOL> elif self . loss_function == deepnet_pb2 . Layer . SQUARED_LOSS : <EOL> state . apply_softmax_grad ( data , target = self . deriv ) <EOL> error = self . deriv . euclid_norm ( ) ** <NUM_LIT:2> <EOL> perf . error = error <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> state . reshape ( ( numlabels * dimensions , batchsize ) ) <EOL> data . reshape ( ( dimensions , batchsize ) ) <EOL> return perf <EOL> def GetSparsityDivisor ( self ) : <EOL> raise Exception ( '<STR_LIT>' ) </s>
