#NEWLINE# def test_matrixmul( ) : #NEWLINE# #INDENT# '\n    Tests matrix multiplication for a range of different\n    dtypes. Checks both normal and transpose multiplication\n    using randomly generated matrices.\n    ' #NEWLINE# rng = np . random . RandomState ( 222 ) #NEWLINE# dtypes = [ 'int16' , 'int32' , 'int64' , 'float64' , 'float32' ] #NEWLINE# tensor_x = [ tensor . wmatrix ( ) , tensor . imatrix ( ) , tensor . lmatrix ( ) , tensor . dmatrix ( ) , tensor . fmatrix ( ) ] #NEWLINE# ( np_W , np_x , np_x_T ) = ( [ ] , [ ] , [ ] ) #NEWLINE# for dtype in dtypes : #NEWLINE# #INDENT# if ( 'int' in dtype ) : #NEWLINE# #INDENT# np_W . append ( rng . randint ( ( - 10 ) , 10 , rng . random_integers ( 5 , size = 2 ) ) . astype ( dtype ) ) #NEWLINE# np_x . append ( rng . randint ( ( - 10 ) , 10 , ( rng . random_integers ( 5 ) , np_W [ ( - 1 ) ] . shape [ 0 ] ) ) . astype ( dtype ) ) #NEWLINE# np_x_T . append ( rng . randint ( ( - 10 ) , 10 , ( rng . random_integers ( 5 ) , np_W [ ( - 1 ) ] . shape [ 1 ] ) ) . astype ( dtype ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( 'float' in dtype ) : #NEWLINE# #INDENT# np_W . append ( rng . uniform ( ( - 1 ) , 1 , rng . random_integers ( 5 , size = 2 ) ) . astype ( dtype ) ) #NEWLINE# np_x . append ( rng . uniform ( ( - 10 ) , 10 , ( rng . random_integers ( 5 ) , np_W [ ( - 1 ) ] . shape [ 0 ] ) ) . astype ( dtype ) ) #NEWLINE# np_x . append ( rng . uniform ( ( - 10 ) , 10 , ( rng . random_integers ( 5 ) , np_W [ ( - 1 ) ] . shape [ 1 ] ) ) . astype ( dtype ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# assert False #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# def sharedW( value , dtype ) : #NEWLINE# #INDENT# return theano . shared ( theano . _asarray ( value , dtype = dtype ) ) #NEWLINE# #UNINDENT# tensor_W = [ sharedW ( W , dtype ) for W in np_W ] #NEWLINE# matrixmul = [ MatrixMul ( W ) for W in tensor_W ] #NEWLINE# assert all ( ( ( mm . get_params ( ) [ 0 ] == W ) for ( mm , W ) in zip ( matrixmul , tensor_W ) ) ) #NEWLINE# fn = [ theano . function ( [ x ] , mm . lmul ( x ) ) for ( x , mm ) in zip ( tensor_x , matrixmul ) ] #NEWLINE# fn_T = [ theano . function ( [ x ] , mm . lmul_T ( x ) ) for ( x , mm ) in zip ( tensor_x , matrixmul ) ] #NEWLINE# for ( W , x , x_T , f , f_T ) in zip ( np_W , np_x , np_x_T , fn , fn_T ) : #NEWLINE# #INDENT# np . testing . assert_allclose ( f ( x ) , np . dot ( x , W ) ) #NEWLINE# np . testing . assert_allclose ( f_T ( x_T ) , np . dot ( x_T , W . T ) )