<s> import numpy as np <EOL> from unittest import TestCase <EOL> from quagga . context import Context <EOL> from quagga . matrix import GpuMatrix <EOL> from quagga . connector import Connector <EOL> from quagga . blocks import MeanPoolingBlock <EOL> class TestMeanPoolingBlock ( TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . rng = np . random . RandomState ( seed = <NUM_LIT> ) <EOL> cls . N = <NUM_LIT:50> <EOL> @ classmethod <EOL> def get_random_array ( cls , shape = None ) : <EOL> if shape : <EOL> a = <NUM_LIT:4> * cls . rng . rand ( * shape ) - <NUM_LIT:2> <EOL> else : <EOL> nrows , ncols = cls . rng . randint ( low = <NUM_LIT:1> , high = <NUM_LIT> , size = <NUM_LIT:2> ) <EOL> a = <NUM_LIT:4> * cls . rng . rand ( nrows , ncols ) - <NUM_LIT:2> <EOL> return a . astype ( dtype = np . float32 ) <EOL> def test_fprop ( self ) : <EOL> r = [ ] <EOL> for i in xrange ( self . N ) : <EOL> a = self . get_random_array ( ) <EOL> a_gpu = Connector ( GpuMatrix . from_npa ( a , '<STR_LIT:float>' ) ) <EOL> vpooling_block = MeanPoolingBlock ( a_gpu , axis = <NUM_LIT:0> ) <EOL> hpooling_block = MeanPoolingBlock ( a_gpu , axis = <NUM_LIT:1> ) <EOL> vpooling_block . fprop ( ) <EOL> r . append ( np . allclose ( vpooling_block . output . to_host ( ) , <EOL> np . mean ( a , axis = <NUM_LIT:0> , keepdims = True ) , <EOL> atol = <NUM_LIT> ) ) <EOL> hpooling_block . fprop ( ) <EOL> r . append ( np . allclose ( hpooling_block . output . to_host ( ) , <EOL> np . mean ( a , axis = <NUM_LIT:1> , keepdims = True ) , <EOL> atol = <NUM_LIT> ) ) <EOL> self . assertEqual ( sum ( r ) , <NUM_LIT:2> * self . N ) <EOL> def test_bprop ( self ) : <EOL> r = [ ] <EOL> context = Context ( ) <EOL> for i in xrange ( self . N ) : <EOL> a = self . get_random_array ( ) <EOL> a_gpu = Connector ( GpuMatrix . from_npa ( a , '<STR_LIT:float>' ) , bu_device_id = context ) <EOL> vpooling_block = MeanPoolingBlock ( a_gpu , axis = <NUM_LIT:0> ) <EOL> voutput , dL_dvoutput = vpooling_block . output . register_usage ( context , context ) <EOL> _dL_voutput = self . get_random_array ( ( dL_dvoutput . nrows , dL_dvoutput . ncols ) ) <EOL> GpuMatrix . from_npa ( _dL_voutput , '<STR_LIT:float>' ) . copy_to ( context , dL_dvoutput ) <EOL> hpooling_block = MeanPoolingBlock ( a_gpu , axis = <NUM_LIT:1> ) <EOL> houtput , dL_dhoutput = hpooling_block . output . register_usage ( context , context ) <EOL> _dL_houtput = self . get_random_array ( ( dL_dhoutput . nrows , dL_dhoutput . ncols ) ) <EOL> GpuMatrix . from_npa ( _dL_houtput , '<STR_LIT:float>' ) . copy_to ( context , dL_dhoutput ) <EOL> vpooling_block . fprop ( ) <EOL> vpooling_block . bprop ( ) <EOL> dL_dmatrix = vpooling_block . dL_dmatrix . to_host ( ) <EOL> r . append ( np . allclose ( dL_dmatrix , <EOL> np . repeat ( _dL_voutput / a . shape [ <NUM_LIT:0> ] , a . shape [ <NUM_LIT:0> ] , <NUM_LIT:0> ) , <EOL> atol = <NUM_LIT> ) ) <EOL> hpooling_block . fprop ( ) <EOL> hpooling_block . bprop ( ) <EOL> hpooling_block . dL_dmatrix . to_host ( ) <EOL> dL_dmatrix = hpooling_block . dL_dmatrix . to_host ( ) <EOL> r . append ( np . allclose ( dL_dmatrix , <EOL> np . repeat ( _dL_houtput / a . shape [ <NUM_LIT:1> ] , a . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) , <EOL> atol = <NUM_LIT> ) ) <EOL> self . assertEqual ( sum ( r ) , <NUM_LIT:2> * self . N ) </s>
