#NEWLINE# def test_sparse( ) : #NEWLINE# #INDENT# print ( '\n\n*************************************************' ) #NEWLINE# print ( '           TEST SPARSE' ) #NEWLINE# print ( '*************************************************' ) #NEWLINE# bsize = 10 #NEWLINE# imshp = ( 28 , 28 ) #NEWLINE# kshp = ( 5 , 5 ) #NEWLINE# nkern = 1 #NEWLINE# ssizes = ( ( 1 , 1 ) , ( 2 , 2 ) ) #NEWLINE# convmodes = ( 'full' , 'valid' ) #NEWLINE# bias = T . dvector ( ) #NEWLINE# kerns = T . dvector ( ) #NEWLINE# input = T . dmatrix ( ) #NEWLINE# rng = N . random . RandomState ( 3423489 ) #NEWLINE# import theano.gof as gof #NEWLINE# ( ntot , ttot ) = ( 0 , 0 ) #NEWLINE# for conv_mode in convmodes : #NEWLINE# #INDENT# for ss in ssizes : #NEWLINE# #INDENT# ( output , outshp ) = sp . applySparseFilter ( kerns , kshp , nkern , input , imshp , ss , bias = bias , mode = conv_mode ) #NEWLINE# f = function ( [ kerns , bias , input ] , output ) #NEWLINE# img2d = N . arange ( ( bsize * N . prod ( imshp ) ) ) . reshape ( ( ( bsize , ) + imshp ) ) #NEWLINE# img1d = img2d . reshape ( bsize , ( - 1 ) ) #NEWLINE# zeropad_img = N . zeros ( ( bsize , ( img2d . shape [ 1 ] + ( 2 * ( kshp [ 0 ] - 1 ) ) ) , ( img2d . shape [ 2 ] + ( 2 * ( kshp [ 1 ] - 1 ) ) ) ) ) #NEWLINE# zeropad_img [ : , ( kshp [ 0 ] - 1 ) : ( ( kshp [ 0 ] - 1 ) + patch . shape [ 1 ] ) , ( kshp [ 1 ] - 1 ) : ( ( kshp [ 1 ] - 1 ) + img2d . shape [ 2 ] ) ] = img2d #NEWLINE# filters = N . arange ( ( N . prod ( outshp ) * N . prod ( kshp ) ) ) . reshape ( nkern , N . prod ( outshp [ 1 : ] ) , N . prod ( kshp ) ) #NEWLINE# spfilt = filters . flatten ( ) #NEWLINE# biasvals = N . arange ( N . prod ( outshp ) ) #NEWLINE# ntime1 = time . time ( ) #NEWLINE# refout = N . zeros ( ( bsize , nkern , outshp [ 1 ] , outshp [ 2 ] ) ) #NEWLINE# patch = N . zeros ( ( kshp [ 0 ] , kshp [ 1 ] ) ) #NEWLINE# for b in xrange ( bsize ) : #NEWLINE# #INDENT# for k in xrange ( nkern ) : #NEWLINE# #INDENT# pixi = 0 #NEWLINE# for j in xrange ( outshp [ 1 ] ) : #NEWLINE# #INDENT# for i in xrange ( outshp [ 2 ] ) : #NEWLINE# #INDENT# n = ( j * ss [ 0 ] ) #NEWLINE# m = ( i * ss [ 1 ] ) #NEWLINE# patch = zeropad_img [ b , n : ( n + kshp [ 0 ] ) , m : ( m + kshp [ 1 ] ) ] #NEWLINE# refout [ ( b , k , j , i ) ] = N . dot ( filters [ k , pixi , : ] , patch . flatten ( ) ) #NEWLINE# pixi += 1 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# refout = ( refout . reshape ( bsize , ( - 1 ) ) + biasvals ) #NEWLINE# ntot += ( time . time ( ) - ntime1 ) #NEWLINE# ttime1 = time . time ( ) #NEWLINE# out1 = f ( spfilt , biasvals , img1d ) #NEWLINE# ttot += ( time . time ( ) - ttime1 ) #NEWLINE# temp = ( refout - out1 ) #NEWLINE# assert ( temp < 1e-10 ) . all ( ) #NEWLINE# vis = T . grad ( output , input , output ) #NEWLINE# downprop = function ( [ kerns , output ] , vis ) #NEWLINE# temp1 = time . time ( ) #NEWLINE# for zz in range ( 100 ) : #NEWLINE# #INDENT# visval = downprop ( spfilt , out1 ) #NEWLINE# #UNINDENT# ( indices , indptr , spmat_shape , sptype , outshp , kmap ) = sp . convolution_indices . sparse_eval ( imshp , kshp , nkern , ss , conv_mode ) #NEWLINE# spmat = sparse . csc_matrix ( ( spfilt [ kmap ] , indices , indptr ) , spmat_shape ) #NEWLINE# visref = N . dot ( out1 , spmat . todense ( ) ) #NEWLINE# assert N . all ( ( visref == visval ) ) #NEWLINE# #UNINDENT# print ( '**** Sparse Profiling Results ****' ) #NEWLINE# print ( 'Numpy processing time: ' , ntot ) #NEWLINE# print ( 'Theano processing time: ' , ttot )