<s> import numpy <EOL> from chainer import cuda <EOL> from chainer import function <EOL> from chainer . functions import sigmoid <EOL> from chainer import utils <EOL> from chainer . utils import type_check <EOL> class SigmoidCrossEntropy ( function . Function ) : <EOL> """<STR_LIT>""" <EOL> ignore_label = - <NUM_LIT:1> <EOL> def __init__ ( self , use_cudnn = True , normalize = True ) : <EOL> self . use_cudnn = use_cudnn <EOL> self . normalize = normalize <EOL> def check_type_forward ( self , in_types ) : <EOL> type_check . expect ( in_types . size ( ) == <NUM_LIT:2> ) <EOL> x_type , t_type = in_types <EOL> type_check . expect ( <EOL> x_type . dtype == numpy . float32 , <EOL> t_type . dtype == numpy . int32 , <EOL> x_type . shape == t_type . shape <EOL> ) <EOL> def forward ( self , inputs ) : <EOL> xp = cuda . get_array_module ( * inputs ) <EOL> x , t = inputs <EOL> self . ignore_mask = ( t != self . ignore_label ) <EOL> if self . normalize : <EOL> count = xp . maximum ( <NUM_LIT:1> , self . ignore_mask . sum ( ) ) <EOL> else : <EOL> count = max ( <NUM_LIT:1> , len ( x ) ) <EOL> self . count = count <EOL> loss = - xp . sum ( <EOL> self . ignore_mask * ( x * ( t - ( x >= <NUM_LIT:0> ) ) - <EOL> xp . log1p ( xp . exp ( - xp . abs ( x ) ) ) ) ) <EOL> return utils . force_array ( xp . divide ( loss , self . count , dtype = x . dtype ) ) , <EOL> def backward ( self , inputs , grad_outputs ) : <EOL> xp = cuda . get_array_module ( * inputs ) <EOL> x , t = inputs <EOL> gloss = grad_outputs [ <NUM_LIT:0> ] <EOL> y , = sigmoid . Sigmoid ( self . use_cudnn ) . forward ( ( x , ) ) <EOL> gx = xp . divide ( <EOL> gloss * self . ignore_mask * ( y - t ) , self . count , <EOL> dtype = y . dtype ) <EOL> return gx , None <EOL> def sigmoid_cross_entropy ( x , t , use_cudnn = True , normalize = True ) : <EOL> """<STR_LIT>""" <EOL> return SigmoidCrossEntropy ( use_cudnn , normalize ) ( x , t ) </s>
