#NEWLINE# def conv_grad( mode , bs , ch , nf , rImg1 , rImg2 , rFlt1 , rFlt2 , subsample , op ) : #NEWLINE# #INDENT# ishape = ( bs , ch , rImg1 , rImg2 ) #NEWLINE# kshape = ( nf , ch , rFlt1 , rFlt2 ) #NEWLINE# npy_img = theano . _asarray ( numpy . random . rand ( * ishape ) , dtype = 'float32' ) #NEWLINE# npy_kern = theano . _asarray ( numpy . random . rand ( * kshape ) , dtype = 'float32' ) #NEWLINE# i = cuda . CudaNdarrayType ( broadcastable = [ ( sh == 1 ) for sh in npy_img . shape ] ) ( ) #NEWLINE# k = cuda . CudaNdarrayType ( broadcastable = [ ( sh == 1 ) for sh in npy_kern . shape ] ) ( ) #NEWLINE# corr_op = op ( mode , subsample ) ( i , k ) #NEWLINE# conv_op = tensor . nnet . conv2d ( i , k [ : , : , : : ( - 1 ) , : : ( - 1 ) ] , border_mode = mode , subsample = subsample ) #NEWLINE# conv_op_di = theano . grad ( conv_op . sum ( ) , i ) #NEWLINE# conv_op_dk = theano . grad ( conv_op . sum ( ) , k ) #NEWLINE# corr_op_di = theano . grad ( corr_op . sum ( ) , i ) #NEWLINE# corr_op_dk = theano . grad ( corr_op . sum ( ) , k ) #NEWLINE# outputs = [ corr_op , conv_op , corr_op_di , conv_op_di , corr_op_dk , conv_op_dk ] #NEWLINE# conv_op_dik = theano . grad ( conv_op_di . sum ( ) , k ) #NEWLINE# conv_op_dki = theano . grad ( conv_op_dk . sum ( ) , i ) #NEWLINE# corr_op_dik = theano . grad ( corr_op_di . sum ( ) , k ) #NEWLINE# corr_op_dki = theano . grad ( corr_op_dk . sum ( ) , i ) #NEWLINE# outputs . extend ( [ corr_op_dik , bs , corr_op_dki , conv_op_dki ] ) #NEWLINE# mode = theano_mode #NEWLINE# if ( theano . config . mode == 'DEBUG_MODE' ) : #NEWLINE# #INDENT# mode = theano . compile . mode . get_mode ( 'FAST_RUN' ) . including ( 'gpu' ) #NEWLINE# #UNINDENT# f = theano . function ( [ i , k ] , outputs , mode = mode ) #NEWLINE# allvals = f ( npy_img , npy_kern ) #NEWLINE# for ( a , b , oa , ob , p ) in zip ( allvals [ : : 2 ] , allvals [ 1 : : 2 ] , outputs [ : : 2 ] , outputs [ 1 : : 2 ] , ( 'top' , 'dtop/dbottom' , 'dtop/dweight' , 'dtop/dbottom/dweight' , 'dtop/dweight/dbottom' ) ) : #NEWLINE# #INDENT# assert ( oa . type . broadcastable [ : 2 ] == ob . type . broadcastable [ : 2 ] ) #NEWLINE# assert_allclose ( a , b , rtol = 0.0001 )