<s> import numpy <EOL> from chainer import cuda <EOL> from chainer . functions . pooling import pooling_2d <EOL> from chainer . utils import conv <EOL> if cuda . cudnn_enabled : <EOL> cudnn = cuda . cudnn <EOL> libcudnn = cudnn . cudnn <EOL> class MaxPooling2D ( pooling_2d . Pooling2D ) : <EOL> """<STR_LIT>""" <EOL> def forward_cpu ( self , x ) : <EOL> col = conv . im2col_cpu ( <EOL> x [ <NUM_LIT:0> ] , self . kh , self . kw , self . sy , self . sx , self . ph , self . pw , <EOL> pval = - float ( '<STR_LIT>' ) , cover_all = self . cover_all ) <EOL> n , c , kh , kw , out_h , out_w = col . shape <EOL> col = col . reshape ( n , c , kh * kw , out_h , out_w ) <EOL> self . indexes = col . argmax ( axis = <NUM_LIT:2> ) <EOL> y = col . max ( axis = <NUM_LIT:2> ) <EOL> return y , <EOL> def forward_gpu ( self , x ) : <EOL> if cuda . cudnn_enabled and self . use_cudnn : <EOL> return super ( MaxPooling2D , self ) . forward_gpu ( x ) <EOL> n , c , h , w = x [ <NUM_LIT:0> ] . shape <EOL> y_h = conv . get_conv_outsize ( <EOL> h , self . kh , self . sy , self . ph , self . cover_all ) <EOL> y_w = conv . get_conv_outsize ( <EOL> w , self . kw , self . sx , self . pw , self . cover_all ) <EOL> y = cuda . cupy . empty ( ( n , c , y_h , y_w ) , dtype = x [ <NUM_LIT:0> ] . dtype ) <EOL> self . indexes = cuda . cupy . empty ( ( n , c , y_h , y_w ) , dtype = numpy . int32 ) <EOL> cuda . elementwise ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '''<STR_LIT>''' , '<STR_LIT>' ) ( x [ <NUM_LIT:0> ] . reduced_view ( ) , <EOL> h , w , y_h , y_w , self . kh , self . kw , <EOL> self . sy , self . sx , self . ph , self . pw , <EOL> y , self . indexes ) <EOL> return y , <EOL> def backward_cpu ( self , x , gy ) : <EOL> n , c , out_h , out_w = gy [ <NUM_LIT:0> ] . shape <EOL> h , w = x [ <NUM_LIT:0> ] . shape [ <NUM_LIT:2> : ] <EOL> gcol = numpy . zeros ( <EOL> ( n , c , self . kh , self . kw , out_h , out_w ) , dtype = numpy . float32 ) <EOL> gcol_r = numpy . rollaxis ( gcol . reshape ( n , c , - <NUM_LIT:1> , out_h , out_w ) , <NUM_LIT:2> ) <EOL> for i in numpy . ndindex ( n , c , out_h , out_w ) : <EOL> gcol_r [ self . indexes [ i ] ] [ i ] = gy [ <NUM_LIT:0> ] [ i ] <EOL> gx = conv . col2im_cpu ( gcol , self . sy , self . sx , self . ph , self . pw , h , w ) <EOL> return gx , <EOL> def backward_gpu ( self , x , gy ) : <EOL> if cuda . cudnn_enabled and self . use_cudnn : <EOL> return super ( MaxPooling2D , self ) . backward_gpu ( x , gy ) <EOL> n , c , h , w = x [ <NUM_LIT:0> ] . shape <EOL> y_h , y_w = gy [ <NUM_LIT:0> ] . shape [ <NUM_LIT:2> : ] <EOL> gx = cuda . cupy . empty_like ( x [ <NUM_LIT:0> ] ) <EOL> cuda . elementwise ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '''<STR_LIT>''' , <EOL> '<STR_LIT>' ) ( gy [ <NUM_LIT:0> ] . reduced_view ( ) , self . indexes . reduced_view ( ) , <EOL> h , w , y_h , y_w , self . kh , self . kw , <EOL> self . sy , self . sx , self . ph , self . pw , <EOL> gx ) <EOL> return gx , <EOL> def create_pool_desc ( self ) : <EOL> return cudnn . create_pooling_descriptor ( <EOL> ( self . kh , self . kw ) , ( self . sy , self . sx ) , ( self . ph , self . pw ) , <EOL> libcudnn . CUDNN_POOLING_MAX ) <EOL> def max_pooling_2d ( x , ksize , stride = None , pad = <NUM_LIT:0> , cover_all = True , <EOL> use_cudnn = True ) : <EOL> """<STR_LIT>""" <EOL> return MaxPooling2D ( ksize , stride , pad , cover_all , use_cudnn ) ( x ) </s>
