<s> from __future__ import print_function <EOL> import time <EOL> from nose . plugins . skip import SkipTest <EOL> import numpy <EOL> try : <EOL> from scipy import ndimage <EOL> except ImportError : <EOL> ndimage = None <EOL> from six . moves import xrange <EOL> import theano <EOL> from theano . tensor . nnet . conv3d2d import * <EOL> import theano . tests . unittest_tools as utt <EOL> if theano . config . mode == '<STR_LIT>' : <EOL> mode_without_gpu = theano . compile . mode . get_mode ( '<STR_LIT>' ) . excluding ( '<STR_LIT>' ) <EOL> else : <EOL> mode_without_gpu = theano . compile . mode . get_default_mode ( ) . excluding ( '<STR_LIT>' ) <EOL> def test_get_diagonal_subtensor_view ( wrap = lambda a : a ) : <EOL> x = numpy . arange ( <NUM_LIT:20> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:4> ) . astype ( '<STR_LIT>' ) <EOL> x = wrap ( x ) <EOL> xv01 = get_diagonal_subtensor_view ( x , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> assert numpy . all ( numpy . asarray ( xv01 ) == [ [ <NUM_LIT:12> , <NUM_LIT:9> , <NUM_LIT:6> , <NUM_LIT:3> ] , [ <NUM_LIT:16> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:7> ] ] ) <EOL> x = numpy . arange ( <NUM_LIT> ) . reshape ( <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> xv01 = get_diagonal_subtensor_view ( x , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> xv02 = get_diagonal_subtensor_view ( x , <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> xv12 = get_diagonal_subtensor_view ( x , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert numpy . all ( numpy . asarray ( xv01 ) == [ <EOL> [ [ <NUM_LIT:12> , <NUM_LIT> ] , [ <NUM_LIT:8> , <NUM_LIT:9> ] , [ <NUM_LIT:4> , <NUM_LIT:5> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT:15> ] , [ <NUM_LIT:10> , <NUM_LIT:11> ] ] ] ) <EOL> assert numpy . all ( numpy . asarray ( xv02 ) == [ <EOL> [ [ <NUM_LIT:6> , <NUM_LIT:1> ] , [ <NUM_LIT:8> , <NUM_LIT:3> ] , [ <NUM_LIT:10> , <NUM_LIT:5> ] ] , <EOL> [ [ <NUM_LIT:12> , <NUM_LIT:7> ] , [ <NUM_LIT> , <NUM_LIT:9> ] , [ <NUM_LIT:16> , <NUM_LIT:11> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:20> , <NUM_LIT:15> ] , [ <NUM_LIT> , <NUM_LIT> ] ] , <EOL> ] ) <EOL> for xi , xvi in zip ( x , xv12 ) : <EOL> assert numpy . all ( xvi == get_diagonal_subtensor_view ( xi , <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> def pyconv3d ( signals , filters ) : <EOL> Ns , Ts , C , Hs , Ws = signals . shape <EOL> Nf , Tf , C , Hf , Wf = filters . shape <EOL> Tf2 = Tf // <NUM_LIT:2> <EOL> Hf2 = Hf // <NUM_LIT:2> <EOL> Wf2 = Wf // <NUM_LIT:2> <EOL> rval = numpy . zeros ( ( Ns , Ts - Tf + <NUM_LIT:1> , Nf , Hs - Hf + <NUM_LIT:1> , Ws - Wf + <NUM_LIT:1> ) ) <EOL> for ns in xrange ( Ns ) : <EOL> for nf in xrange ( Nf ) : <EOL> for c in xrange ( C ) : <EOL> s_i = signals [ ns , : , c , : , : ] <EOL> f_i = filters [ nf , : , c , : , : ] <EOL> r_i = rval [ ns , : , nf , : , : ] <EOL> o_i = ndimage . convolve ( s_i , f_i , mode = '<STR_LIT>' , cval = <NUM_LIT:1> ) <EOL> o_i_sh0 = o_i . shape [ <NUM_LIT:0> ] <EOL> r_i += o_i [ Tf2 : o_i_sh0 - Tf2 , Hf2 : - Hf2 , Wf2 : - Wf2 ] <EOL> return rval <EOL> def test_conv3d ( mode = mode_without_gpu , shared = theano . tensor . _shared ) : <EOL> if ndimage is None : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> Ns , Ts , C , Hs , Ws = <NUM_LIT:3> , <NUM_LIT:10> , <NUM_LIT:3> , <NUM_LIT:32> , <NUM_LIT:32> <EOL> Nf , Tf , C , Hf , Wf = <NUM_LIT:32> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:5> <EOL> signals = numpy . arange ( Ns * Ts * C * Hs * Ws ) . reshape ( Ns , Ts , C , Hs , Ws ) . astype ( '<STR_LIT>' ) <EOL> filters = numpy . arange ( Nf * Tf * C * Hf * Wf ) . reshape ( Nf , Tf , C , Hf , Wf ) . astype ( '<STR_LIT>' ) <EOL> t0 = time . time ( ) <EOL> pyres = pyconv3d ( signals , filters ) <EOL> print ( time . time ( ) - t0 ) <EOL> s_signals = shared ( signals ) <EOL> s_filters = shared ( filters ) <EOL> s_output = shared ( signals * <NUM_LIT:0> ) <EOL> out = conv3d ( s_signals , s_filters , <EOL> signals_shape = signals . shape , <EOL> filters_shape = filters . shape ) <EOL> newconv3d = theano . function ( [ ] , [ ] , <EOL> updates = { s_output : out } , <EOL> mode = mode ) <EOL> t0 = time . time ( ) <EOL> newconv3d ( ) <EOL> print ( time . time ( ) - t0 ) <EOL> utt . assert_allclose ( pyres , s_output . get_value ( borrow = True ) ) <EOL> gsignals , gfilters = theano . grad ( out . sum ( ) , [ s_signals , s_filters ] ) <EOL> gnewconv3d = theano . function ( [ ] , [ ] , <EOL> updates = [ ( s_filters , gfilters ) , <EOL> ( s_signals , gsignals ) ] , <EOL> mode = mode , <EOL> name = '<STR_LIT>' ) <EOL> t0 = time . time ( ) <EOL> gnewconv3d ( ) <EOL> print ( '<STR_LIT>' , time . time ( ) - t0 ) <EOL> Ns , Ts , C , Hs , Ws = <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:5> <EOL> Nf , Tf , C , Hf , Wf = <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:2> <EOL> signals = numpy . random . rand ( Ns , Ts , C , Hs , Ws ) . astype ( '<STR_LIT>' ) <EOL> filters = numpy . random . rand ( Nf , Tf , C , Hf , Wf ) . astype ( '<STR_LIT>' ) <EOL> utt . verify_grad ( conv3d , [ signals , filters ] , eps = <NUM_LIT> , mode = mode ) <EOL> Ns , Ts , C , Hs , Ws = <NUM_LIT:3> , <NUM_LIT:10> , <NUM_LIT:3> , <NUM_LIT:32> , <NUM_LIT:32> <EOL> Nf , Tf , C , Hf , Wf = <NUM_LIT:32> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:5> <EOL> signals = numpy . arange ( Ns * Ts * C * Hs * Ws ) . reshape ( Ns , Ts , C , Hs , Ws ) . astype ( '<STR_LIT>' ) <EOL> filters = numpy . arange ( Nf * Tf * C * Hf * Wf ) . reshape ( Nf , Tf , C , Hf , Wf ) . astype ( '<STR_LIT>' ) <EOL> t0 = time . time ( ) <EOL> pyres = pyconv3d ( signals , filters ) <EOL> print ( time . time ( ) - t0 ) <EOL> s_signals = shared ( signals ) <EOL> s_filters = shared ( filters ) <EOL> s_output = shared ( signals * <NUM_LIT:0> ) <EOL> out = conv3d ( s_signals , s_filters , <EOL> signals_shape = signals . shape , <EOL> filters_shape = filters . shape ) <EOL> newconv3d = theano . function ( [ ] , [ ] , <EOL> updates = { s_output : out } , <EOL> mode = mode ) <EOL> t0 = time . time ( ) <EOL> newconv3d ( ) <EOL> print ( time . time ( ) - t0 ) <EOL> utt . assert_allclose ( pyres , s_output . get_value ( borrow = True ) ) <EOL> gsignals , gfilters = theano . grad ( out . sum ( ) , [ s_signals , s_filters ] ) <EOL> gnewconv3d = theano . function ( [ ] , [ ] , <EOL> updates = [ ( s_filters , gfilters ) , <EOL> ( s_signals , gsignals ) ] , <EOL> mode = mode , <EOL> name = '<STR_LIT>' ) <EOL> t0 = time . time ( ) <EOL> gnewconv3d ( ) <EOL> print ( '<STR_LIT>' , time . time ( ) - t0 ) <EOL> Ns , Ts , C , Hs , Ws = <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:5> <EOL> Nf , Tf , C , Hf , Wf = <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:2> <EOL> signals = numpy . random . rand ( Ns , Ts , C , Hs , Ws ) . astype ( '<STR_LIT>' ) <EOL> filters = numpy . random . rand ( Nf , Tf , C , Hf , Wf ) . astype ( '<STR_LIT>' ) <EOL> utt . verify_grad ( conv3d , [ signals , filters ] , eps = <NUM_LIT> , mode = mode ) </s>
