#NEWLINE# def stochastic_max_pool_bc01( bc01 , pool_shape , pool_stride , image_shape , rng = None ) : #NEWLINE# #INDENT# '\n    .. todo::\n\n        WRITEME properly\n\n    Stochastic max pooling for training as defined in:\n\n    Stochastic Pooling for Regularization of Deep Convolutional Neural Networks\n    Matthew D. Zeiler, Rob Fergus\n\n    Parameters\n    ----------\n    bc01 : theano 4-tensor\n        in format (batch size, channels, rows, cols),\n        IMPORTANT: All values should be positive\n    pool_shape : tuple\n        shape of the pool region (rows, cols)\n    pool_stride : tuple\n        strides between pooling regions (row stride, col stride)\n    image_shape : tuple\n        avoid doing some of the arithmetic in theano\n    rng : theano random stream\n    ' #NEWLINE# ( r , c ) = image_shape #NEWLINE# ( pr , pc ) = pool_shape #NEWLINE# ( rs , cs ) = pool_stride #NEWLINE# batch = bc01 . shape [ 0 ] #NEWLINE# channel = bc01 . shape [ 1 ] #NEWLINE# rng = make_theano_rng ( rng , 2022 , which_method = 'multinomial' ) #NEWLINE# def last_pool( im_shp , p_shp , p_strd ) : #NEWLINE# #INDENT# rval = int ( numpy . ceil ( ( float ( ( im_shp - p_shp ) ) / p_strd ) ) ) #NEWLINE# assert ( ( ( p_strd * rval ) + p_shp ) >= im_shp ) #NEWLINE# assert ( ( ( p_strd * ( rval - 1 ) ) + p_shp ) < im_shp ) #NEWLINE# return rval #NEWLINE# #UNINDENT# last_pool_r = ( last_pool ( image_shape [ 0 ] , pool_shape [ 0 ] , pool_stride [ 0 ] ) * pool_stride [ 0 ] ) #NEWLINE# required_r = ( last_pool_r + pr ) #NEWLINE# last_pool_c = ( last_pool ( image_shape [ 1 ] , pool_shape [ 1 ] , pool_stride [ 1 ] ) * pool_stride [ 1 ] ) #NEWLINE# required_c = ( last_pool_c + pc ) #NEWLINE# res_r = ( int ( numpy . floor ( ( last_pool_r / rs ) ) ) + 1 ) #NEWLINE# res_c = ( int ( numpy . floor ( ( last_pool_c / cs ) ) ) + 1 ) #NEWLINE# for bc01v in get_debug_values ( bc01 ) : #NEWLINE# #INDENT# assert ( not contains_inf ( bc01v ) ) #NEWLINE# assert ( bc01v . shape [ 2 ] == image_shape [ 0 ] ) #NEWLINE# assert ( bc01v . shape [ 3 ] == image_shape [ 1 ] ) #NEWLINE# #UNINDENT# padded = tensor . alloc ( 0.0 , batch , channel , required_r , required_c ) #NEWLINE# name = bc01 . name #NEWLINE# if ( name is None ) : #NEWLINE# #INDENT# name = 'anon_bc01' #NEWLINE# #UNINDENT# bc01 = tensor . set_subtensor ( padded [ : , : , 0 : r , 0 : c ] , bc01 ) #NEWLINE# bc01 . name = ( 'zero_padded_' + name ) #NEWLINE# window = tensor . alloc ( 0.0 , batch , channel , res_r , res_c , pr , pc ) #NEWLINE# window . name = ( 'unravlled_winodows_' + name ) #NEWLINE# for row_within_pool in xrange ( pool_shape [ 0 ] ) : #NEWLINE# #INDENT# row_stop = ( ( last_pool_r + row_within_pool ) + 1 ) #NEWLINE# for col_within_pool in xrange ( pool_shape [ 1 ] ) : #NEWLINE# #INDENT# col_stop = ( ( last_pool_c + col_within_pool ) + 1 ) #NEWLINE# win_cell = bc01 [ : , : , row_within_pool : row_stop : rs , col_within_pool : col_stop : cs ] #NEWLINE# window = tensor . set_subtensor ( window [ : , : , : , : , row_within_pool , col_within_pool ] , win_cell ) #NEWLINE# #UNINDENT# #UNINDENT# norm = window . sum ( axis = [ 4 , 5 ] ) #NEWLINE# norm = tensor . switch ( tensor . eq ( norm , 0.0 ) , 1.0 , norm ) #NEWLINE# norm = ( window / norm . dimshuffle ( 0 , 1 , 2 , 3 , 'x' , 'x' ) ) #NEWLINE# prob = rng . multinomial ( pvals = norm . reshape ( ( ( ( ( batch * channel ) * res_r ) * res_c ) , ( pr * pc ) ) ) , dtype = 'float32' ) #NEWLINE# res = ( window * prob . reshape ( ( batch , channel , res_r , res_c , pr , pc ) ) ) . max ( axis = 5 ) . max ( axis = 4 ) #NEWLINE# res . name = ( 'pooled_' + name ) #NEWLINE# return tensor . cast ( res , theano . config . floatX )