<s> import copy <EOL> import theano <EOL> import numpy as np <EOL> import theano . tensor as T <EOL> from theano import config <EOL> from theano import function <EOL> from pylearn2 . expr . probabilistic_max_pooling import max_pool_c01b <EOL> from pylearn2 . sandbox . cuda_convnet . probabilistic_max_pooling import prob_max_pool_c01b <EOL> from pylearn2 . utils import float32_floatX <EOL> from pylearn2 . testing . skip import skip_if_no_gpu <EOL> skip_if_no_gpu ( ) <EOL> if theano . config . mode == '<STR_LIT>' : <EOL> mode_with_gpu = theano . compile . mode . get_mode ( '<STR_LIT>' ) . including ( '<STR_LIT>' ) <EOL> mode_without_gpu = theano . compile . mode . get_mode ( <EOL> '<STR_LIT>' ) . excluding ( '<STR_LIT>' ) <EOL> else : <EOL> mode_with_gpu = theano . compile . mode . get_default_mode ( ) . including ( '<STR_LIT>' ) <EOL> mode_without_gpu = theano . compile . mode . get_default_mode ( ) . excluding ( '<STR_LIT>' ) <EOL> mode_with_gpu = copy . copy ( mode_with_gpu ) <EOL> mode_without_gpu = copy . copy ( mode_without_gpu ) <EOL> mode_with_gpu . check_py_code = False <EOL> mode_without_gpu . check_py_code = False <EOL> @ float32_floatX <EOL> def test_correctness ( ) : <EOL> """<STR_LIT>""" <EOL> rng = np . random . RandomState ( [ <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ] ) <EOL> batch_size_list = [ <NUM_LIT:1> , <NUM_LIT:5> ] <EOL> channels = <NUM_LIT:16> <EOL> rows_list = [ <NUM_LIT:2> , <NUM_LIT> ] <EOL> pool_rows_list = [ <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> for batch_size in batch_size_list : <EOL> for rows , pool_rows in zip ( rows_list , pool_rows_list ) : <EOL> cols = rows <EOL> pool_cols = pool_rows <EOL> zv = rng . randn ( channels , rows , cols , batch_size ) . astype ( config . floatX ) <EOL> z = T . tensor4 ( ) <EOL> p , h = prob_max_pool_c01b ( z , ( pool_rows , pool_cols ) ) <EOL> func = function ( [ z ] , [ p , h ] , mode = mode_with_gpu ) <EOL> p_op , h_op = func ( zv ) <EOL> p , h = max_pool_c01b ( z , ( pool_rows , pool_cols ) ) <EOL> func = function ( [ z ] , [ p , h ] , mode = mode_without_gpu ) <EOL> p_th , h_th = func ( zv ) <EOL> assert np . allclose ( p_op , p_th ) <EOL> assert np . allclose ( h_op , h_th ) <EOL> @ float32_floatX <EOL> def test_top_donw_correctness ( ) : <EOL> """<STR_LIT>""" <EOL> rng = np . random . RandomState ( [ <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ] ) <EOL> batch_size_list = [ <NUM_LIT:1> ] <EOL> channels = <NUM_LIT:16> <EOL> rows_list = [ <NUM_LIT:2> , <NUM_LIT> ] <EOL> pool_rows_list = [ <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> for batch_size in batch_size_list : <EOL> for rows , pool_rows in zip ( rows_list , pool_rows_list ) : <EOL> cols = rows <EOL> pool_cols = pool_rows <EOL> zv = rng . randn ( channels , rows , cols , batch_size ) . astype ( config . floatX ) <EOL> tv = rng . randn ( channels , rows / pool_rows , cols / pool_cols , batch_size ) . astype ( config . floatX ) <EOL> z = T . tensor4 ( ) <EOL> t = T . tensor4 ( ) <EOL> p , h = prob_max_pool_c01b ( z , ( pool_rows , pool_cols ) , top_down = t ) <EOL> func = function ( [ z , t ] , [ p , h ] , mode = mode_with_gpu ) <EOL> p_op , h_op = func ( zv , tv ) <EOL> p , h = max_pool_c01b ( z , ( pool_rows , pool_cols ) , top_down = t ) <EOL> func = function ( [ z , t ] , [ p , h ] , mode = mode_without_gpu ) <EOL> p_th , h_th = func ( zv , tv ) <EOL> assert np . allclose ( p_op , p_th ) <EOL> assert np . allclose ( h_op , h_th ) <EOL> @ float32_floatX <EOL> def test_grad ( ) : <EOL> """<STR_LIT>""" <EOL> rng = np . random . RandomState ( [ <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ] ) <EOL> batch_size_list = [ <NUM_LIT:1> ] <EOL> channels = <NUM_LIT:16> <EOL> rows_list = [ <NUM_LIT:2> , <NUM_LIT> ] <EOL> pool_rows_list = [ <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> for batch_size in batch_size_list : <EOL> for rows , pool_rows in zip ( rows_list , pool_rows_list ) : <EOL> cols = rows <EOL> pool_cols = pool_rows <EOL> zv = rng . randn ( channels , rows , cols , <EOL> batch_size ) . astype ( config . floatX ) <EOL> tv = rng . randn ( channels , rows / pool_rows , cols / pool_cols , batch_size ) . astype ( config . floatX ) <EOL> z = T . tensor4 ( ) <EOL> t = T . tensor4 ( ) <EOL> p , h = prob_max_pool_c01b ( z , ( pool_rows , pool_cols ) , top_down = t ) <EOL> gh_t = T . grad ( h . sum ( ) , t ) <EOL> gp_t = T . grad ( p . sum ( ) , t ) <EOL> gh_z = T . grad ( h . sum ( ) , z ) <EOL> gp_z = T . grad ( p . sum ( ) , z ) <EOL> gph_z = T . grad ( p . sum ( ) + h . sum ( ) , z ) <EOL> gph_t = T . grad ( p . sum ( ) + h . sum ( ) , t ) <EOL> func = function ( [ z , t ] , [ gh_t , gp_t , gh_z , gp_z , gph_z , gph_t ] , <EOL> mode = mode_with_gpu ) <EOL> op_rval = func ( zv , tv ) <EOL> p , h = max_pool_c01b ( z , ( pool_rows , pool_cols ) , top_down = t ) <EOL> gh_t = T . grad ( h . sum ( ) , t ) <EOL> gp_t = T . grad ( p . sum ( ) , t ) <EOL> gh_z = T . grad ( h . sum ( ) , z ) <EOL> gp_z = T . grad ( p . sum ( ) , z ) <EOL> gph_z = T . grad ( p . sum ( ) + h . sum ( ) , z ) <EOL> gph_t = T . grad ( p . sum ( ) + h . sum ( ) , t ) <EOL> func = function ( [ z , t ] , [ gh_t , gp_t , gh_z , gp_z , gph_z , gph_t ] , <EOL> mode = mode_without_gpu ) <EOL> th_rval = func ( zv , tv ) <EOL> for op , th in zip ( op_rval , th_rval ) : <EOL> assert np . allclose ( op , th , rtol = <NUM_LIT> , atol = <NUM_LIT> ) </s>
