#NEWLINE# def test_pooling( ) : #NEWLINE# #INDENT# if ( not dnn . dnn_available ( test_ctx_name ) ) : #NEWLINE# #INDENT# raise SkipTest ( dnn . dnn_available . msg ) #NEWLINE# #UNINDENT# if ( dnn . version ( ) < 4004 ) : #NEWLINE# #INDENT# modes = ( 'max' , 'average_inc_pad' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# modes = ( 'max' , 'average_inc_pad' , 'average_exc_pad' ) #NEWLINE# #UNINDENT# x = T . ftensor4 ( ) #NEWLINE# for ( mode , pad ) in product ( modes , ( ( 0 , 0 ) , ( 1 , 0 ) , ( 1 , 0 ) , ( 2 , 3 ) , ( 3 , 2 ) ) ) : #NEWLINE# #INDENT# if ( mode == 'max' ) : #NEWLINE# #INDENT# func = T . max #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# func = T . mean #NEWLINE# #UNINDENT# if ( ( pad != ( 0 , 0 ) ) and ( func is T . mean ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# for ws in ( 4 , 2 , 5 ) : #NEWLINE# #INDENT# for stride in ( 2 , 3 ) : #NEWLINE# #INDENT# if ( stride > ws ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( ( pad [ 0 ] > stride ) or ( pad [ 1 ] > stride ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# out1 = pool_2d ( x , ( ws , ws ) , st = ( stride , stride ) , ignore_border = True , padding = pad , mode = mode ) #NEWLINE# out2 = pool_2d_i2n ( x , ds = ( ws , ws ) , strides = ( stride , stride ) , pad = pad , pool_function = func ) #NEWLINE# mode_without_gpu2 = mode_without_gpu . including ( ) #NEWLINE# mode_without_gpu2 . check_isfinite = False #NEWLINE# f1 = theano . function ( [ x ] , out1 , mode = mode_with_gpu ) #NEWLINE# assert any ( [ isinstance ( node . op , dnn . GpuDnnPool ) for node in f1 . maker . fgraph . apply_nodes ] ) #NEWLINE# f2 = theano . function ( [ x ] , out2 , mode = mode_without_gpu2 ) #NEWLINE# assert ( not any ( [ isinstance ( node . op , dnn . GpuDnnPool ) for node in f2 . maker . fgraph . apply_nodes ] ) ) #NEWLINE# for shp in [ ( 1 , 10 , 100 , 100 ) , ( 1 , 3 , 99 , 99 ) , ( 32 , 1 , 147 , 197 ) ] : #NEWLINE# #INDENT# data = numpy . random . normal ( 0 , 1 , shp ) . astype ( 'float32' ) #NEWLINE# a = f1 ( data ) #NEWLINE# b = f2 ( data ) #NEWLINE# utt . assert_allclose ( a , b ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for shp in [ ( 1 , 1 , 2 , 2 ) , ( 1 , 1 , 3 , 3 ) ] : #NEWLINE# #INDENT# data = ( numpy . random . normal ( 0 , 1 , shp ) . astype ( 'float32' ) * 10 ) #NEWLINE# ws = 2 #NEWLINE# stride = 2 #NEWLINE# if ( ( pad [ 0 ] > stride ) or ( pad [ 1 ] > stride ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# def fn( x ) : #NEWLINE# #INDENT# return pool_2d ( x , ( ws , ws ) , ignore_border = True , padding = pad , mode = mode ) #NEWLINE# #UNINDENT# utt . verify_grad ( fn , [ data ] , cast_to_output_type = False , mode = mode_with_gpu ) #NEWLINE# fg = theano . function ( [ x ] , theano . grad ( fn ( x ) . sum ( ) , x ) , mode = mode_with_gpu ) #NEWLINE# assert any ( [ isinstance ( node . op , dnn . GpuDnnPoolGrad ) for node in fg . maker . fgraph . toposort ( ) ] ) #NEWLINE# def fn( x ) : #NEWLINE# #INDENT# dnn_op = dnn . dnn_pool ( x , ws = ( ws , ws ) , stride = ( stride , stride ) , pad = pad , mode = mode ) #NEWLINE# return dnn_op #NEWLINE# #UNINDENT# utt . verify_grad ( fn , [ data ] , cast_to_output_type = False , mode = mode_with_gpu ) #NEWLINE# fg = theano . function ( [ x ] , theano . grad ( fn ( x ) . sum ( ) , x ) , mode = mode_with_gpu ) #NEWLINE# assert any ( [ isinstance ( node . op , dnn . GpuDnnPoolGrad ) for node in fg . maker . fgraph . toposort ( ) ] ) #NEWLINE# g_out = fg ( data ) #NEWLINE# out = pool_2d ( x , ( ws , ws ) , padding = pad , ignore_border = True , mode = mode ) #NEWLINE# fc = theano . function ( [ x ] , theano . grad ( out . sum ( ) , x ) , mode = mode_without_gpu ) #NEWLINE# if ( mode == 'max' ) : #NEWLINE# #INDENT# assert any ( [ isinstance ( node . op , MaxPoolGrad ) for node in fc . maker . fgraph . toposort ( ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# assert any ( [ isinstance ( node . op , AveragePoolGrad ) for node in fc . maker . fgraph . toposort ( ) ] ) #NEWLINE# #UNINDENT# c_out = fc ( data ) #NEWLINE# utt . assert_allclose ( c_out , g_out )