<s> from nose . plugins . skip import SkipTest <EOL> import sys <EOL> import time <EOL> import unittest <EOL> import theano . sparse <EOL> if not theano . sparse . enable_sparse : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> import scipy . sparse <EOL> from scipy . signal import convolve2d <EOL> import scipy . sparse as sparse <EOL> import numpy <EOL> from six . moves import xrange <EOL> from theano import function , tensor <EOL> import theano <EOL> from theano . compat import next <EOL> from theano . sparse . sandbox import sp <EOL> from theano . sparse . tests . test_basic import random_lil <EOL> from theano . tests import unittest_tools as utt <EOL> from theano . sparse import verify_grad_sparse <EOL> from theano . sparse . tests . test_basic import sparse_random_inputs <EOL> from theano . tests . unittest_tools import attr <EOL> class TestSP ( unittest . TestCase ) : <EOL> @ attr ( '<STR_LIT>' ) <EOL> def test_convolution ( self ) : <EOL> bsize = <NUM_LIT:10> <EOL> imshp = ( <NUM_LIT> , <NUM_LIT> ) <EOL> kshp = ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> nkern = <NUM_LIT:5> <EOL> ssizes = ( ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:3> ) , ( <NUM_LIT:4> , <NUM_LIT:4> ) ) <EOL> convmodes = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> bias = tensor . dvector ( ) <EOL> kerns = tensor . dmatrix ( ) <EOL> input = tensor . dmatrix ( ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> filters = rng . randn ( nkern , numpy . prod ( kshp ) ) <EOL> biasvals = rng . randn ( nkern ) <EOL> for mode in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> ttot , ntot = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for conv_mode in convmodes : <EOL> for ss in ssizes : <EOL> output , outshp = sp . convolve ( kerns , kshp , nkern , input , imshp , ss , bias = bias , mode = conv_mode ) <EOL> f = function ( [ kerns , bias , input ] , output , mode = mode ) <EOL> img2d = numpy . arange ( bsize * numpy . prod ( imshp ) ) . reshape ( ( bsize , ) + imshp ) <EOL> img1d = img2d . reshape ( bsize , - <NUM_LIT:1> ) <EOL> filtersflipped = numpy . zeros ( ( nkern , ) + kshp ) <EOL> for k in range ( nkern ) : <EOL> it = reversed ( filters [ k , : ] ) <EOL> for i in range ( kshp [ <NUM_LIT:0> ] ) : <EOL> for j in range ( kshp [ <NUM_LIT:1> ] ) : <EOL> filtersflipped [ k , i , j ] = next ( it ) <EOL> if conv_mode == '<STR_LIT>' : <EOL> fulloutshp = numpy . array ( imshp ) - numpy . array ( kshp ) + <NUM_LIT:1> <EOL> else : <EOL> fulloutshp = numpy . array ( imshp ) + numpy . array ( kshp ) - <NUM_LIT:1> <EOL> ntime1 = time . time ( ) <EOL> refout = numpy . zeros ( ( bsize , ) + tuple ( fulloutshp ) + ( nkern , ) ) <EOL> for b in range ( bsize ) : <EOL> for n in range ( nkern ) : <EOL> refout [ b , ... , n ] = convolve2d ( img2d [ b , : , : ] , <EOL> filtersflipped [ n , ... ] , <EOL> conv_mode ) <EOL> ntot += time . time ( ) - ntime1 <EOL> bench1 = refout [ : , <NUM_LIT:0> : : ss [ <NUM_LIT:0> ] , <NUM_LIT:0> : : ss [ <NUM_LIT:1> ] , : ] . reshape ( bsize , - <NUM_LIT:1> , nkern ) <EOL> bench1 += biasvals . reshape ( <NUM_LIT:1> , <NUM_LIT:1> , nkern ) <EOL> bench1 = numpy . swapaxes ( bench1 , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> ttime1 = time . time ( ) <EOL> out1 = f ( filters , biasvals , img1d ) <EOL> ttot += time . time ( ) - ttime1 <EOL> temp = bench1 . flatten ( ) - out1 . flatten ( ) <EOL> assert ( temp < <NUM_LIT> ) . all ( ) <EOL> @ attr ( '<STR_LIT>' ) <EOL> def test_sparse ( self ) : <EOL> bsize = <NUM_LIT:10> <EOL> imshp = ( <NUM_LIT:8> , <NUM_LIT:8> ) <EOL> kshp = ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> nkern = <NUM_LIT:1> <EOL> ssizes = ( ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> convmodes = ( '<STR_LIT>' , '<STR_LIT>' , ) <EOL> bias = tensor . dvector ( ) <EOL> kerns = tensor . dvector ( ) <EOL> input = tensor . dmatrix ( ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> import theano . gof as gof <EOL> for mode in ( None , ) : <EOL> ntot , ttot = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for conv_mode in convmodes : <EOL> for ss in ssizes : <EOL> output , outshp = sp . applySparseFilter ( kerns , kshp , nkern , input , imshp , ss , bias = bias , mode = conv_mode ) <EOL> f = function ( [ kerns , bias , input ] , output , mode = mode ) <EOL> img2d = numpy . arange ( bsize * numpy . prod ( imshp ) ) . reshape ( ( bsize , ) + imshp ) <EOL> img1d = img2d . reshape ( bsize , - <NUM_LIT:1> ) <EOL> zeropad_img = numpy . zeros ( ( bsize , img2d . shape [ <NUM_LIT:1> ] + <NUM_LIT:2> * ( kshp [ <NUM_LIT:0> ] - <NUM_LIT:1> ) , img2d . shape [ <NUM_LIT:2> ] + <NUM_LIT:2> * ( kshp [ <NUM_LIT:1> ] - <NUM_LIT:1> ) ) ) <EOL> zeropad_img [ : , kshp [ <NUM_LIT:0> ] - <NUM_LIT:1> : kshp [ <NUM_LIT:0> ] - <NUM_LIT:1> + img2d . shape [ <NUM_LIT:1> ] , <EOL> kshp [ <NUM_LIT:1> ] - <NUM_LIT:1> : kshp [ <NUM_LIT:1> ] - <NUM_LIT:1> + img2d . shape [ <NUM_LIT:2> ] ] = img2d <EOL> filters = numpy . arange ( numpy . prod ( outshp ) * numpy . prod ( kshp ) ) . reshape ( nkern , numpy . prod ( outshp [ <NUM_LIT:1> : ] ) , numpy . prod ( kshp ) ) <EOL> spfilt = filters . flatten ( ) <EOL> biasvals = numpy . arange ( numpy . prod ( outshp ) ) <EOL> ntime1 = time . time ( ) <EOL> refout = numpy . zeros ( ( bsize , nkern , outshp [ <NUM_LIT:1> ] , outshp [ <NUM_LIT:2> ] ) ) <EOL> patch = numpy . zeros ( ( kshp [ <NUM_LIT:0> ] , kshp [ <NUM_LIT:1> ] ) ) <EOL> for b in xrange ( bsize ) : <EOL> for k in xrange ( nkern ) : <EOL> pixi = <NUM_LIT:0> <EOL> for j in xrange ( outshp [ <NUM_LIT:1> ] ) : <EOL> for i in xrange ( outshp [ <NUM_LIT:2> ] ) : <EOL> n = j * ss [ <NUM_LIT:0> ] <EOL> m = i * ss [ <NUM_LIT:1> ] <EOL> patch = zeropad_img [ b , n : n + kshp [ <NUM_LIT:0> ] , m : m + kshp [ <NUM_LIT:1> ] ] <EOL> refout [ b , k , j , i ] = numpy . dot ( filters [ k , pixi , : ] , patch . flatten ( ) ) <EOL> pixi += <NUM_LIT:1> <EOL> refout = refout . reshape ( bsize , - <NUM_LIT:1> ) + biasvals <EOL> ntot += time . time ( ) - ntime1 <EOL> ttime1 = time . time ( ) <EOL> out1 = f ( spfilt , biasvals , img1d ) <EOL> ttot += time . time ( ) - ttime1 <EOL> temp = refout - out1 <EOL> assert ( temp < <NUM_LIT> ) . all ( ) <EOL> vis = tensor . grad ( <NUM_LIT:0.5> * tensor . sqr ( output ) . sum ( ) , input ) <EOL> downprop = function ( [ kerns , output ] , vis ) <EOL> temp1 = time . time ( ) <EOL> for zz in range ( <NUM_LIT:100> ) : <EOL> visval = downprop ( spfilt , out1 ) <EOL> indices , indptr , spmat_shape , sptype , outshp , kmap = sp . convolution_indices . sparse_eval ( imshp , kshp , nkern , ss , conv_mode ) <EOL> spmat = sparse . csc_matrix ( ( spfilt [ kmap ] , indices , indptr ) , spmat_shape ) <EOL> visref = numpy . dot ( out1 , spmat . todense ( ) ) <EOL> assert numpy . all ( visref == visval ) , ( visref , visval ) <EOL> @ attr ( '<STR_LIT>' ) <EOL> def test_multilayer_sparse ( self ) : <EOL> bsize = <NUM_LIT:10> <EOL> imshp = ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> kshp = ( ( <NUM_LIT:3> , <NUM_LIT:3> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> nkerns = ( <NUM_LIT:10> , <NUM_LIT:20> ) <EOL> ssizes = ( ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> convmodes = ( '<STR_LIT>' , '<STR_LIT>' , ) <EOL> kerns = [ tensor . dvector ( ) , tensor . dvector ( ) ] <EOL> input = tensor . dmatrix ( ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> img2d = numpy . arange ( bsize * numpy . prod ( imshp ) ) . reshape ( ( bsize , ) + imshp ) <EOL> img1d = img2d . reshape ( bsize , - <NUM_LIT:1> ) <EOL> for mode in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for conv_mode in convmodes : <EOL> for ss in ssizes : <EOL> l1hid , l1outshp = sp . applySparseFilter ( kerns [ <NUM_LIT:0> ] , kshp [ <NUM_LIT:0> ] , nkerns [ <NUM_LIT:0> ] , input , imshp , ss , mode = conv_mode ) <EOL> l2hid , l2outshp = sp . applySparseFilter ( kerns [ <NUM_LIT:1> ] , kshp [ <NUM_LIT:1> ] , nkerns [ <NUM_LIT:1> ] , l1hid , l1outshp , ss , mode = conv_mode ) <EOL> l1propup = function ( [ kerns [ <NUM_LIT:0> ] , input ] , l1hid , mode = mode ) <EOL> l2propup = function ( [ kerns [ <NUM_LIT:1> ] , l1hid ] , l2hid , mode = mode ) <EOL> l1kernvals = numpy . arange ( numpy . prod ( l1outshp ) * numpy . prod ( kshp [ <NUM_LIT:0> ] ) ) <EOL> l2kernvals = numpy . arange ( numpy . prod ( l2outshp ) * numpy . prod ( kshp [ <NUM_LIT:1> ] ) * nkerns [ <NUM_LIT:0> ] ) <EOL> l1hidval = l1propup ( l1kernvals , img1d ) <EOL> l2hidval = l2propup ( l2kernvals , l1hidval ) <EOL> def test_multilayer_conv ( self ) : <EOL> bsize = <NUM_LIT:10> <EOL> imshp = ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> kshp = ( ( <NUM_LIT:3> , <NUM_LIT:3> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> nkerns = ( <NUM_LIT:3> , <NUM_LIT:6> ) <EOL> ssizes = ( ( ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) , ) <EOL> convmodes = ( '<STR_LIT>' , ) <EOL> kerns = [ tensor . dmatrix ( ) , tensor . dmatrix ( ) ] <EOL> input = tensor . dmatrix ( ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> img2d = numpy . arange ( bsize * numpy . prod ( imshp ) ) . reshape ( ( bsize , ) + imshp ) <EOL> img1d = img2d . reshape ( bsize , - <NUM_LIT:1> ) <EOL> for mode in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for conv_mode in convmodes : <EOL> for ss in ssizes : <EOL> l1hid , l1shp = sp . convolve ( kerns [ <NUM_LIT:0> ] , kshp [ <NUM_LIT:0> ] , nkerns [ <NUM_LIT:0> ] , input , imshp , ss [ <NUM_LIT:0> ] , mode = conv_mode ) <EOL> l1propup = function ( [ kerns [ <NUM_LIT:0> ] , input ] , l1hid , mode = mode ) <EOL> l1kernvals = numpy . arange ( nkerns [ <NUM_LIT:0> ] * numpy . prod ( kshp [ <NUM_LIT:0> ] ) ) . reshape ( nkerns [ <NUM_LIT:0> ] , numpy . prod ( kshp [ <NUM_LIT:0> ] ) ) <EOL> l1hidval = l1propup ( l1kernvals , img1d ) <EOL> l2hid , l2shp = sp . convolve ( kerns [ <NUM_LIT:1> ] , kshp [ <NUM_LIT:1> ] , nkerns [ <NUM_LIT:1> ] , l1hid , l1shp , ss [ <NUM_LIT:1> ] , mode = conv_mode ) <EOL> l2propup = function ( [ kerns [ <NUM_LIT:1> ] , l1hid ] , l2hid , mode = mode ) <EOL> l2kernvals = numpy . arange ( nkerns [ <NUM_LIT:1> ] * numpy . prod ( kshp [ <NUM_LIT:1> ] ) * nkerns [ <NUM_LIT:0> ] ) . reshape ( nkerns [ <NUM_LIT:1> ] , numpy . prod ( kshp [ <NUM_LIT:1> ] ) * nkerns [ <NUM_LIT:0> ] ) <EOL> l1hidval = numpy . arange ( numpy . size ( l1hidval ) ) . reshape ( l1hidval . shape ) <EOL> l2hidval = l2propup ( l2kernvals , l1hidval ) <EOL> def test_maxpool ( self ) : <EOL> maxpoolshps = ( ( <NUM_LIT:2> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:3> ) , ( <NUM_LIT:4> , <NUM_LIT:4> ) , ( <NUM_LIT:5> , <NUM_LIT:5> ) , ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> imval = numpy . random . rand ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> images = tensor . dmatrix ( ) <EOL> for maxpoolshp in maxpoolshps : <EOL> output , outshp = sp . max_pool ( images , imval . shape [ <NUM_LIT:1> : ] , maxpoolshp ) <EOL> f = function ( [ images , ] , [ output , ] ) <EOL> output_val = f ( imval . reshape ( imval . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) ) <EOL> my_output_val = numpy . zeros ( ( imval . shape [ <NUM_LIT:0> ] , imval . shape [ <NUM_LIT:1> ] , <EOL> imval . shape [ <NUM_LIT:2> ] // maxpoolshp [ <NUM_LIT:0> ] , <EOL> imval . shape [ <NUM_LIT:3> ] // maxpoolshp [ <NUM_LIT:1> ] ) ) <EOL> assert numpy . prod ( my_output_val . shape [ <NUM_LIT:1> : ] ) == numpy . prod ( numpy . r_ [ imval . shape [ <NUM_LIT:1> ] , outshp ] ) <EOL> for n in range ( imval . shape [ <NUM_LIT:0> ] ) : <EOL> for k in range ( imval . shape [ <NUM_LIT:1> ] ) : <EOL> for i in range ( imval . shape [ <NUM_LIT:2> ] // maxpoolshp [ <NUM_LIT:0> ] ) : <EOL> for j in range ( imval . shape [ <NUM_LIT:3> ] // maxpoolshp [ <NUM_LIT:1> ] ) : <EOL> ii , jj = i * maxpoolshp [ <NUM_LIT:0> ] , j * maxpoolshp [ <NUM_LIT:1> ] <EOL> patch = imval [ n , k , ii : ii + maxpoolshp [ <NUM_LIT:0> ] , jj : jj + maxpoolshp [ <NUM_LIT:1> ] ] <EOL> my_output_val [ n , k , i , j ] = numpy . max ( patch ) <EOL> my_output_val = my_output_val . reshape ( imval . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) <EOL> assert numpy . all ( output_val == my_output_val ) <EOL> def mp ( input ) : <EOL> output , outshp = sp . max_pool ( input , imval . shape [ <NUM_LIT:1> : ] , maxpoolshp ) <EOL> return output <EOL> utt . verify_grad ( mp , [ imval . reshape ( imval . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) ] ) <EOL> def test_CSMGrad ( self ) : <EOL> imshp = ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> nkern = <NUM_LIT:1> <EOL> kshp = ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> ssizes = ( ( <NUM_LIT:1> , <NUM_LIT:1> ) , ) <EOL> convmodes = ( '<STR_LIT>' , ) <EOL> kerns = tensor . dvector ( ) <EOL> indices = tensor . ivector ( ) <EOL> indptr = tensor . ivector ( ) <EOL> spmat_shape = tensor . ivector ( ) <EOL> for mode in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> for conv_mode in convmodes : <EOL> for ss in ssizes : <EOL> indvals , indptrvals , spshapevals , sptype , outshp , kmap = sp . convolution_indices . sparse_eval ( imshp , kshp , nkern , ss , conv_mode ) <EOL> kvals = numpy . random . random ( nkern * numpy . prod ( kshp ) * numpy . prod ( outshp ) ) . flatten ( ) <EOL> def d ( kerns ) : <EOL> return theano . sparse . dense_from_sparse ( <EOL> theano . sparse . CSM ( sptype , kmap ) ( <EOL> kerns , indvals , indptrvals , spshapevals ) ) <EOL> utt . verify_grad ( d , [ kvals ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if <NUM_LIT:0> : <EOL> test_remove0 ( ) <EOL> exit ( ) <EOL> if <NUM_LIT:1> : <EOL> testcase = TestSP <EOL> suite = unittest . TestLoader ( ) <EOL> suite = suite . loadTestsFromTestCase ( testcase ) <EOL> unittest . TextTestRunner ( verbosity = <NUM_LIT:2> ) . run ( suite ) <EOL> else : <EOL> unittest . main ( ) </s>
