<s> from __future__ import print_function <EOL> import copy <EOL> import logging <EOL> import time <EOL> from theano . compat import izip <EOL> from nose . plugins . skip import SkipTest <EOL> import numpy <EOL> from six . moves import xrange <EOL> import theano <EOL> from theano . compile . sharedvalue import shared <EOL> from theano . compile . pfunc import pfunc <EOL> from theano import tensor <EOL> from theano import config <EOL> import theano . tensor . nnet . conv as conv <EOL> import theano . tensor . signal . pool as pool <EOL> import theano . sandbox . cuda as tcn <EOL> import theano . tests . unittest_tools as utt <EOL> if theano . config . mode not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise SkipTest ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if tcn . cuda_available == False : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . INFO ) <EOL> def my_rand ( * shape ) : <EOL> return theano . _asarray ( numpy . random . rand ( * shape ) , dtype = '<STR_LIT>' ) <EOL> def my_randn ( * shape ) : <EOL> return theano . _asarray ( numpy . random . randn ( * shape ) , dtype = '<STR_LIT>' ) <EOL> def my_zeros ( * shape ) : <EOL> return theano . _asarray ( numpy . zeros ( * shape ) , dtype = '<STR_LIT>' ) <EOL> def get_mode ( use_gpu , check_isfinite = True ) : <EOL> if theano . config . mode != '<STR_LIT>' : <EOL> ret = theano . compile . get_default_mode ( ) <EOL> else : <EOL> ret = theano . compile . mode . get_mode ( '<STR_LIT>' ) <EOL> if isinstance ( ret , theano . compile . ProfileMode ) : <EOL> ret = copy . copy ( ret ) <EOL> if isinstance ( ret , theano . compile . DebugMode ) : <EOL> ret = copy . copy ( ret ) <EOL> ret . check_isfinite = check_isfinite <EOL> if use_gpu : <EOL> ret = ret . including ( '<STR_LIT>' ) <EOL> else : <EOL> ret = ret . excluding ( '<STR_LIT>' ) <EOL> return ret <EOL> def print_mode ( mode ) : <EOL> if mode is not None and isinstance ( mode , ( theano . compile . ProfileMode , ) ) : <EOL> mode . print_summary ( ) <EOL> def print_diff_mode ( a , b ) : <EOL> if ( a is not None and <EOL> isinstance ( a , ( theano . compile . ProfileMode , ) ) and <EOL> isinstance ( b , ( theano . compile . ProfileMode , ) ) ) : <EOL> a . print_diff_summary ( b ) <EOL> def run_nnet ( use_gpu , n_batch = <NUM_LIT> , n_in = <NUM_LIT> , n_hid = <NUM_LIT> , n_out = <NUM_LIT:10> , <EOL> n_train = <NUM_LIT:100> ) : <EOL> if config . mode == '<STR_LIT>' : <EOL> n_train = <NUM_LIT:1> <EOL> if use_gpu : <EOL> w = tcn . shared_constructor ( <NUM_LIT> * ( my_rand ( n_in , n_hid ) - <NUM_LIT:0.5> ) , '<STR_LIT:w>' ) <EOL> b = tcn . shared_constructor ( my_zeros ( n_hid ) , '<STR_LIT:b>' ) <EOL> v = tcn . shared_constructor ( my_zeros ( ( n_hid , n_out ) ) , '<STR_LIT:c>' ) <EOL> c = tcn . shared_constructor ( my_zeros ( n_out ) , '<STR_LIT:c>' ) <EOL> else : <EOL> w = shared ( <NUM_LIT> * ( my_rand ( n_in , n_hid ) - <NUM_LIT:0.5> ) , '<STR_LIT:w>' ) <EOL> b = shared ( my_zeros ( n_hid ) , '<STR_LIT:b>' ) <EOL> v = shared ( my_zeros ( ( n_hid , n_out ) ) , '<STR_LIT:c>' ) <EOL> c = shared ( my_zeros ( n_out ) , '<STR_LIT:c>' ) <EOL> x = tensor . fmatrix ( '<STR_LIT:x>' ) <EOL> y = tensor . fmatrix ( '<STR_LIT:y>' ) <EOL> lr = tensor . fscalar ( '<STR_LIT>' ) <EOL> hid = tensor . tanh ( tensor . dot ( x , w ) + b ) <EOL> out = tensor . tanh ( tensor . dot ( hid , v ) + c ) <EOL> loss = tensor . sum ( <NUM_LIT:0.5> * ( out - y ) ** <NUM_LIT:2> * lr ) <EOL> if <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' , loss . type ) <EOL> params = [ w , b , v , c ] <EOL> gparams = tensor . grad ( loss , params ) <EOL> mode = get_mode ( use_gpu ) <EOL> train = pfunc ( [ x , y , lr ] , [ loss ] , mode = mode , <EOL> updates = [ ( p , p - g ) for p , g in izip ( params , gparams ) ] ) <EOL> if <NUM_LIT:0> : <EOL> for i , n in enumerate ( train . maker . fgraph . toposort ( ) ) : <EOL> print ( i , n ) <EOL> xval = my_rand ( n_batch , n_in ) <EOL> yval = my_rand ( n_batch , n_out ) <EOL> lr = theano . _asarray ( <NUM_LIT> , dtype = '<STR_LIT>' ) <EOL> t0 = time . time ( ) <EOL> rval = [ ] <EOL> for i in xrange ( n_train ) : <EOL> rval . append ( train ( xval , yval , lr ) ) <EOL> dt = time . time ( ) - t0 <EOL> print_mode ( mode ) <EOL> return numpy . asarray ( rval ) , dt <EOL> @ utt . AttemptManyTimes ( n_attempts = <NUM_LIT:3> , n_req_successes = <NUM_LIT:1> ) <EOL> def test_run_nnet ( ) : <EOL> for n_in in <NUM_LIT> , <NUM_LIT> , <NUM_LIT> : <EOL> for n_hid in <NUM_LIT> , <NUM_LIT> , <NUM_LIT> : <EOL> utt . seed_rng ( ) <EOL> rval_cpu , tc = run_nnet ( False , n_in = n_in , n_hid = n_hid ) <EOL> utt . seed_rng ( ) <EOL> rval_gpu , tg = run_nnet ( True , n_in = n_in , n_hid = n_hid ) <EOL> abs_diff , rel_diff = theano . gradient . numeric_grad . abs_rel_err ( rval_gpu , <EOL> rval_cpu ) <EOL> max_abs_diff = abs_diff . max ( ) <EOL> rtol = <NUM_LIT> <EOL> if n_in * n_hid >= <NUM_LIT> * <NUM_LIT> : <EOL> rtol = <NUM_LIT> <EOL> assert numpy . allclose ( rval_cpu , rval_gpu , rtol = rtol , atol = <NUM_LIT> ) , ( "<STR_LIT>" , max_abs_diff , <EOL> rel_diff . max ( ) , n_in , n_hid ) <EOL> def test_run_nnet_med ( ) : <EOL> utt . seed_rng ( ) <EOL> rval_cpu = run_nnet ( False , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT:4> , n_train = <NUM_LIT> ) <EOL> def test_run_nnet_small ( ) : <EOL> utt . seed_rng ( ) <EOL> rval_cpu = run_nnet ( False , <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:4> , <NUM_LIT:4> , n_train = <NUM_LIT> ) <EOL> def run_conv_nnet1 ( use_gpu ) : <EOL> if use_gpu : <EOL> shared_fn = tcn . shared_constructor <EOL> else : <EOL> shared_fn = shared <EOL> n_batch = <NUM_LIT:16> <EOL> n_kern = <NUM_LIT:20> <EOL> shape_img = ( n_batch , <NUM_LIT:1> , <NUM_LIT:32> , <NUM_LIT:32> ) <EOL> shape_kern = ( n_kern , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> n_train = <NUM_LIT:10> <EOL> if config . mode == '<STR_LIT>' : <EOL> n_train = <NUM_LIT:1> <EOL> logical_hid_shape = tcn . blas . GpuConv . logical_output_shape_2d ( <EOL> shape_img [ <NUM_LIT:2> : ] , shape_kern [ <NUM_LIT:2> : ] , '<STR_LIT>' ) <EOL> n_hid = n_kern * logical_hid_shape [ <NUM_LIT:0> ] * logical_hid_shape [ <NUM_LIT:1> ] <EOL> n_out = <NUM_LIT:10> <EOL> w = shared_fn ( <NUM_LIT> * ( my_rand ( * shape_kern ) - <NUM_LIT:0.5> ) , '<STR_LIT:w>' ) <EOL> b = shared_fn ( my_zeros ( ( n_kern , ) ) , '<STR_LIT:b>' ) <EOL> v = shared_fn ( my_zeros ( ( n_hid , n_out ) ) , '<STR_LIT:c>' ) <EOL> c = shared_fn ( my_zeros ( n_out ) , '<STR_LIT:c>' ) <EOL> x = tensor . Tensor ( dtype = '<STR_LIT>' , broadcastable = ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) ( '<STR_LIT:x>' ) <EOL> y = tensor . fmatrix ( '<STR_LIT:y>' ) <EOL> lr = tensor . fscalar ( '<STR_LIT>' ) <EOL> conv_op = conv . ConvOp ( shape_img [ <NUM_LIT:2> : ] , shape_kern [ <NUM_LIT:2> : ] , n_kern , n_batch , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> hid = tensor . tanh ( conv_op ( x , w ) + b . dimshuffle ( ( <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' ) ) ) <EOL> hid_flat = hid . reshape ( ( n_batch , n_hid ) ) <EOL> out = tensor . tanh ( tensor . dot ( hid_flat , v ) + c ) <EOL> loss = tensor . sum ( <NUM_LIT:0.5> * ( out - y ) ** <NUM_LIT:2> * lr ) <EOL> params = [ w , b , v , c ] <EOL> gparams = tensor . grad ( loss , params ) <EOL> mode = get_mode ( use_gpu ) <EOL> train = pfunc ( [ x , y , lr ] , [ loss ] , mode = mode , updates = [ ( p , p - g ) for p , <EOL> g in zip ( params , gparams ) ] ) <EOL> xval = my_rand ( * shape_img ) <EOL> yval = my_rand ( n_batch , n_out ) <EOL> lr = theano . _asarray ( <NUM_LIT> , dtype = '<STR_LIT>' ) <EOL> for i in xrange ( n_train ) : <EOL> rval = train ( xval , yval , lr ) <EOL> print_mode ( mode ) <EOL> return rval <EOL> def test_conv_nnet1 ( ) : <EOL> utt . seed_rng ( ) <EOL> rval_cpu = run_conv_nnet1 ( False ) <EOL> utt . seed_rng ( ) <EOL> rval_gpu = run_conv_nnet1 ( True ) <EOL> utt . assert_allclose ( rval_cpu , rval_gpu , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> def run_conv_nnet2 ( use_gpu ) : <EOL> if use_gpu : <EOL> shared_fn = tcn . shared_constructor <EOL> else : <EOL> shared_fn = shared <EOL> n_batch = <NUM_LIT> <EOL> shape_img = ( n_batch , <NUM_LIT:1> , <NUM_LIT:32> , <NUM_LIT:32> ) <EOL> n_kern = <NUM_LIT:20> <EOL> shape_kern = ( n_kern , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> n_kern1 = <NUM_LIT:10> <EOL> shape_kern1 = ( n_kern1 , n_kern , <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> n_train = <NUM_LIT:30> <EOL> if config . mode == '<STR_LIT>' : <EOL> n_train = <NUM_LIT:1> <EOL> logical_hid_shape = tcn . blas . GpuConv . logical_output_shape_2d ( tuple ( <EOL> shape_img [ <NUM_LIT:2> : ] ) , tuple ( shape_kern [ <NUM_LIT:2> : ] ) , '<STR_LIT>' ) <EOL> logical_hid_shape1 = tcn . blas . GpuConv . logical_output_shape_2d ( <EOL> ( logical_hid_shape [ <NUM_LIT:0> ] // <NUM_LIT:2> , logical_hid_shape [ <NUM_LIT:1> ] // <NUM_LIT:2> ) , <EOL> tuple ( shape_kern1 [ <NUM_LIT:2> : ] ) , '<STR_LIT>' ) <EOL> n_hid = n_kern1 * logical_hid_shape1 [ <NUM_LIT:0> ] * logical_hid_shape1 [ <NUM_LIT:1> ] <EOL> n_out = <NUM_LIT:10> <EOL> w0 = shared_fn ( <NUM_LIT> * ( my_rand ( * shape_kern ) - <NUM_LIT:0.5> ) , '<STR_LIT>' ) <EOL> b0 = shared_fn ( my_zeros ( ( n_kern , ) ) , '<STR_LIT>' ) <EOL> w1 = shared_fn ( <NUM_LIT> * ( my_rand ( * shape_kern1 ) - <NUM_LIT:0.5> ) , '<STR_LIT>' ) <EOL> b1 = shared_fn ( my_zeros ( ( n_kern1 , ) ) , '<STR_LIT>' ) <EOL> v = shared_fn ( my_zeros ( ( n_hid , n_out ) ) , '<STR_LIT:c>' ) <EOL> c = shared_fn ( my_zeros ( n_out ) , '<STR_LIT:c>' ) <EOL> x = tensor . Tensor ( dtype = '<STR_LIT>' , broadcastable = ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) ( '<STR_LIT:x>' ) <EOL> y = tensor . fmatrix ( '<STR_LIT:y>' ) <EOL> lr = tensor . fscalar ( '<STR_LIT>' ) <EOL> conv_op = conv . ConvOp ( shape_img [ <NUM_LIT:2> : ] , shape_kern [ <NUM_LIT:2> : ] , n_kern , n_batch , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> conv_op1 = conv . ConvOp ( ( n_kern , logical_hid_shape [ <NUM_LIT:0> ] // <NUM_LIT:2> , <EOL> logical_hid_shape [ <NUM_LIT:1> ] // <NUM_LIT:2> ) , shape_kern1 [ <NUM_LIT:2> : ] , n_kern1 , n_batch , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> hid = tensor . tanh ( conv_op ( x , w0 ) + b0 . dimshuffle ( ( <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' ) ) ) <EOL> hid1 = tensor . tanh ( conv_op1 ( hid [ : , : , : : <NUM_LIT:2> , : : <NUM_LIT:2> ] , w1 ) + b1 . dimshuffle ( ( <EOL> <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' ) ) ) <EOL> hid_flat = hid1 . reshape ( ( n_batch , n_hid ) ) <EOL> out = tensor . tanh ( tensor . dot ( hid_flat , v ) + c ) <EOL> loss = tensor . sum ( <NUM_LIT:0.5> * ( out - y ) ** <NUM_LIT:2> * lr ) <EOL> params = [ w0 , b0 , w1 , b1 , v , c ] <EOL> gparams = tensor . grad ( loss , params ) <EOL> mode = get_mode ( use_gpu ) <EOL> train = pfunc ( [ x , y , lr ] , [ loss ] , mode = mode , updates = [ ( p , p - g ) for p , <EOL> g in zip ( params , gparams ) ] ) <EOL> xval = my_rand ( * shape_img ) <EOL> yval = my_rand ( n_batch , n_out ) <EOL> lr = theano . _asarray ( <NUM_LIT> , dtype = '<STR_LIT>' ) <EOL> for i in xrange ( n_train ) : <EOL> rval = train ( xval , yval , lr ) <EOL> print_mode ( mode ) <EOL> return rval <EOL> def test_conv_nnet2 ( ) : <EOL> utt . seed_rng ( ) <EOL> rval_gpu = run_conv_nnet2 ( True ) <EOL> if True : <EOL> utt . seed_rng ( ) <EOL> rval_cpu = run_conv_nnet2 ( False ) <EOL> utt . assert_allclose ( rval_cpu , rval_gpu , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> def build_conv_nnet2_classif ( use_gpu , isize , ksize , n_batch , <EOL> downsample_ops = True , verbose = <NUM_LIT:0> , version = - <NUM_LIT:1> , <EOL> check_isfinite = True ) : <EOL> if use_gpu : <EOL> shared_fn = tcn . shared_constructor <EOL> else : <EOL> shared_fn = shared <EOL> isize1 = isize <EOL> isize2 = isize <EOL> if isinstance ( isize , ( tuple , ) ) : <EOL> isize1 = isize [ <NUM_LIT:0> ] <EOL> isize2 = isize [ <NUM_LIT:1> ] <EOL> shape_img = ( n_batch , <NUM_LIT:1> , isize1 , isize2 ) <EOL> n_kern = <NUM_LIT:20> <EOL> shape_kern = ( n_kern , <NUM_LIT:1> , ksize , ksize ) <EOL> n_kern1 = <NUM_LIT:30> <EOL> shape_kern1 = ( n_kern1 , n_kern , ksize , ksize ) <EOL> logical_hid_shape = tcn . blas . GpuConv . logical_output_shape_2d ( <EOL> ( isize1 , isize2 ) , ( ksize , ksize ) , '<STR_LIT>' ) <EOL> logical_hid_shape1 = tcn . blas . GpuConv . logical_output_shape_2d ( <EOL> ( logical_hid_shape [ <NUM_LIT:0> ] // <NUM_LIT:2> , logical_hid_shape [ <NUM_LIT:1> ] // <NUM_LIT:2> ) , <EOL> ( ksize , ksize ) , '<STR_LIT>' ) <EOL> n_hid = n_kern1 * logical_hid_shape1 [ <NUM_LIT:0> ] * logical_hid_shape1 [ <NUM_LIT:1> ] <EOL> n_out = <NUM_LIT:10> <EOL> w0 = shared_fn ( <NUM_LIT> * ( my_rand ( * shape_kern ) - <NUM_LIT:0.5> ) , '<STR_LIT>' ) <EOL> b0 = shared_fn ( my_zeros ( ( n_kern , ) ) , '<STR_LIT>' ) <EOL> w1 = shared_fn ( <NUM_LIT> * ( my_rand ( * shape_kern1 ) - <NUM_LIT:0.5> ) , '<STR_LIT>' ) <EOL> b1 = shared_fn ( my_zeros ( ( n_kern1 , ) ) , '<STR_LIT>' ) <EOL> v = shared_fn ( <NUM_LIT> * my_randn ( n_hid , n_out ) , '<STR_LIT:v>' ) <EOL> c = shared_fn ( my_zeros ( n_out ) , '<STR_LIT:c>' ) <EOL> x = tensor . Tensor ( dtype = '<STR_LIT>' , broadcastable = ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) ( '<STR_LIT:x>' ) <EOL> y = tensor . fmatrix ( '<STR_LIT:y>' ) <EOL> lr = tensor . fscalar ( '<STR_LIT>' ) <EOL> conv_op = conv . ConvOp ( shape_img [ <NUM_LIT:2> : ] , shape_kern [ <NUM_LIT:2> : ] , n_kern , <EOL> n_batch , <NUM_LIT:1> , <NUM_LIT:1> , verbose = verbose , version = version ) <EOL> conv_op1 = conv . ConvOp ( <EOL> ( n_kern , logical_hid_shape [ <NUM_LIT:0> ] // <NUM_LIT:2> , logical_hid_shape [ <NUM_LIT:1> ] // <NUM_LIT:2> ) , <EOL> shape_kern1 [ <NUM_LIT:2> : ] , n_kern1 , n_batch , <NUM_LIT:1> , <NUM_LIT:1> , verbose = verbose , version = version ) <EOL> ds_op = pool . Pool ( ( <NUM_LIT:2> , <NUM_LIT:2> ) , ignore_border = False ) <EOL> if downsample_ops : <EOL> hid = tensor . tanh ( ds_op ( conv_op ( x , w0 ) + b0 . dimshuffle ( ( <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' ) ) ) ) <EOL> else : <EOL> hid = tensor . tanh ( ( conv_op ( x , w0 ) + b0 . dimshuffle ( ( <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' ) <EOL> ) ) [ : , : , : : <NUM_LIT:2> , : : <NUM_LIT:2> ] ) <EOL> hid1 = tensor . tanh ( conv_op1 ( hid , w1 ) + b1 . dimshuffle ( ( <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' ) ) ) <EOL> hid_flat = hid1 . reshape ( ( n_batch , n_hid ) ) <EOL> out = tensor . nnet . softmax ( tensor . dot ( hid_flat , v ) + c ) <EOL> loss = tensor . sum ( tensor . nnet . crossentropy_categorical_1hot ( out , <EOL> tensor . argmax ( y , axis = <NUM_LIT:1> ) ) * lr ) <EOL> params = [ w0 , b0 , w1 , b1 , v , c ] <EOL> gparams = tensor . grad ( loss , params ) <EOL> mode = get_mode ( use_gpu , check_isfinite ) <EOL> train = pfunc ( [ x , y , lr ] , [ loss ] , mode = mode , updates = [ ( p , p - g ) for p , <EOL> g in zip ( params , gparams ) ] ) <EOL> if verbose : <EOL> theano . printing . debugprint ( train ) <EOL> if use_gpu : <EOL> topo = train . maker . fgraph . toposort ( ) <EOL> conv_ops = ( tcn . blas . GpuConv , <EOL> tcn . dnn . GpuDnnConv , <EOL> tcn . dnn . GpuDnnConvGradI , <EOL> tcn . dnn . GpuDnnConvGradW , <EOL> tcn . blas . BaseGpuCorrMM ) <EOL> assert len ( [ n for n in topo if isinstance ( n . op , conv_ops ) ] ) > <NUM_LIT:0> <EOL> shape_target = ( n_batch , n_out ) <EOL> return train , params , shape_img , shape_target , mode <EOL> def run_conv_nnet2_classif ( use_gpu , seed , isize , ksize , bsize , <EOL> n_train = <NUM_LIT:10> , <EOL> check_isfinite = True , <EOL> pickle = False , <EOL> verbose = <NUM_LIT:0> , <EOL> version = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> utt . seed_rng ( seed ) <EOL> train , params , x_shape , y_shape , mode = build_conv_nnet2_classif ( <EOL> use_gpu = use_gpu , <EOL> isize = isize , <EOL> ksize = ksize , <EOL> n_batch = bsize , <EOL> verbose = verbose , <EOL> version = version , <EOL> check_isfinite = check_isfinite ) <EOL> if use_gpu : <EOL> device = '<STR_LIT>' <EOL> else : <EOL> device = '<STR_LIT>' <EOL> xval = my_rand ( * x_shape ) <EOL> yval = my_rand ( * y_shape ) <EOL> lr = theano . _asarray ( <NUM_LIT> , dtype = '<STR_LIT>' ) <EOL> rvals = my_zeros ( n_train ) <EOL> t0 = time . time ( ) <EOL> for i in xrange ( n_train ) : <EOL> rvals [ i ] = train ( xval , yval , lr ) [ <NUM_LIT:0> ] <EOL> t1 = time . time ( ) <EOL> print_mode ( mode ) <EOL> if pickle and isinstance ( mode , theano . compile . ProfileMode ) : <EOL> import pickle <EOL> print ( "<STR_LIT>" % device ) <EOL> print ( pickle . dumps ( mode ) ) <EOL> print ( "<STR_LIT>" % device ) <EOL> def cmp_run_conv_nnet2_classif ( seed , isize , ksize , bsize , <EOL> ignore_error = False , <EOL> n_train = <NUM_LIT:10> , <EOL> gpu_only = False , <EOL> cpu_only = False , <EOL> float_atol = <NUM_LIT> , <EOL> check_isfinite = True , <EOL> pickle = False , <EOL> verbose = <NUM_LIT:0> , <EOL> version = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if config . mode == '<STR_LIT>' : <EOL> n_train = <NUM_LIT:1> <EOL> orig_float32_atol = theano . tensor . basic . float32_atol <EOL> try : <EOL> if float_atol : <EOL> theano . tensor . basic . float32_atol = float_atol <EOL> if gpu_only and cpu_only : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif cpu_only : <EOL> use_gpu = False <EOL> compare = False <EOL> elif gpu_only : <EOL> use_gpu = True <EOL> compare = False <EOL> else : <EOL> compare = True <EOL> if not compare : <EOL> return run_conv_nnet2_classif ( use_gpu = use_gpu , <EOL> seed = seed , isize = isize , ksize = ksize , bsize = bsize , <EOL> n_train = n_train , <EOL> check_isfinite = check_isfinite , <EOL> pickle = pickle , <EOL> verbose = verbose , <EOL> version = version ) <EOL> utt . seed_rng ( seed ) <EOL> train_cpu , params_cpu , x_shape , y_shape , mode_cpu = build_conv_nnet2_classif ( <EOL> use_gpu = False , <EOL> isize = isize , <EOL> ksize = ksize , <EOL> n_batch = bsize , <EOL> verbose = verbose , <EOL> version = version , <EOL> check_isfinite = check_isfinite ) <EOL> utt . seed_rng ( seed ) <EOL> train_gpu , params_gpu , x_shape_gpu , y_shape_gpu , mode_gpu = build_conv_nnet2_classif ( <EOL> use_gpu = True , <EOL> isize = isize , <EOL> ksize = ksize , <EOL> n_batch = bsize , <EOL> verbose = verbose , <EOL> version = version , <EOL> check_isfinite = check_isfinite ) <EOL> assert x_shape == x_shape_gpu <EOL> assert y_shape == y_shape_gpu <EOL> xval = my_rand ( * x_shape ) <EOL> yval = my_rand ( * y_shape ) <EOL> lr = theano . _asarray ( <NUM_LIT> , dtype = '<STR_LIT>' ) <EOL> time_cpu = <NUM_LIT:0> <EOL> time_gpu = <NUM_LIT:0> <EOL> for i in range ( n_train ) : <EOL> t0 = time . time ( ) <EOL> rval_cpu = train_cpu ( xval , yval , lr ) [ <NUM_LIT:0> ] <EOL> t1 = time . time ( ) <EOL> time_cpu += ( t1 - t0 ) <EOL> t0 = time . time ( ) <EOL> rval_gpu = train_gpu ( xval , yval , lr ) [ <NUM_LIT:0> ] <EOL> t1 = time . time ( ) <EOL> time_gpu += ( t1 - t0 ) <EOL> if ( verbose or not <EOL> numpy . allclose ( rval_cpu , rval_gpu , rtol = <NUM_LIT> , atol = float_atol ) ) : <EOL> print ( "<STR_LIT>" , i + <NUM_LIT:1> ) <EOL> print ( "<STR_LIT>" , rval_cpu ) <EOL> print ( "<STR_LIT>" , rval_gpu ) <EOL> print ( "<STR_LIT>" , numpy . absolute ( rval_gpu - rval_cpu ) ) <EOL> print ( "<STR_LIT>" , numpy . absolute ( ( <EOL> rval_gpu - rval_cpu ) / rval_gpu ) ) <EOL> if not ignore_error : <EOL> utt . assert_allclose ( rval_cpu , rval_gpu , <EOL> rtol = <NUM_LIT> , atol = float_atol ) <EOL> if i < n_train - <NUM_LIT:1> : <EOL> for cpu_p , gpu_p in zip ( params_cpu , params_gpu ) : <EOL> cpu_p . set_value ( gpu_p . get_value ( borrow = False ) , borrow = True ) <EOL> finally : <EOL> theano . tensor . basic . float32_atol = orig_float32_atol <EOL> if pickle : <EOL> if isinstance ( cpu_mode , theano . compile . ProfileMode ) : <EOL> import pickle <EOL> print ( "<STR_LIT>" ) <EOL> print ( pickle . dumps ( cpu_mode ) ) <EOL> print ( "<STR_LIT>" ) <EOL> if isinstance ( gpu_mode , theano . compile . ProfileMode ) : <EOL> import pickle <EOL> print ( "<STR_LIT>" ) <EOL> print ( pickle . dumps ( gpu_mode ) ) <EOL> print ( "<STR_LIT>" ) <EOL> gpu_only = False <EOL> cpu_only = False <EOL> ignore_error = False <EOL> verbose = <NUM_LIT:0> <EOL> version = - <NUM_LIT:1> <EOL> seed = utt . fetch_seed ( ) <EOL> def test_lenet_28 ( ) : <EOL> cmp_run_conv_nnet2_classif ( seed , <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT> , n_train = <NUM_LIT:10> , <EOL> ignore_error = ignore_error , gpu_only = gpu_only , <EOL> cpu_only = cpu_only , verbose = verbose , version = version ) <EOL> def test_lenet_32 ( ) : <EOL> cmp_run_conv_nnet2_classif ( seed , <NUM_LIT:32> , <NUM_LIT:5> , <NUM_LIT> , n_train = <NUM_LIT:8> , <EOL> ignore_error = ignore_error , gpu_only = gpu_only , <EOL> verbose = verbose , version = version ) <EOL> def test_lenet_32_long ( ) : <EOL> cmp_run_conv_nnet2_classif ( seed , <NUM_LIT:32> , <NUM_LIT:5> , <NUM_LIT:30> , n_train = <NUM_LIT:50> , <EOL> ignore_error = ignore_error , gpu_only = gpu_only , <EOL> cpu_only = cpu_only , verbose = verbose , version = version ) <EOL> def test_lenet_64 ( ) : <EOL> cmp_run_conv_nnet2_classif ( seed , <NUM_LIT:64> , <NUM_LIT:7> , <NUM_LIT:10> , n_train = <NUM_LIT:10> , <EOL> ignore_error = ignore_error , gpu_only = gpu_only , <EOL> cpu_only = cpu_only , verbose = verbose , <EOL> check_isfinite = True , version = version ) <EOL> def test_lenet_108 ( ) : <EOL> cmp_run_conv_nnet2_classif ( seed , <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:5> , n_train = <NUM_LIT:4> , <EOL> ignore_error = ignore_error , gpu_only = gpu_only , <EOL> cpu_only = cpu_only , verbose = verbose , <EOL> check_isfinite = True , version = version ) <EOL> def test_lenet_256 ( ) : <EOL> cmp_run_conv_nnet2_classif ( seed , <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:2> , n_train = <NUM_LIT:5> , <EOL> ignore_error = ignore_error , gpu_only = gpu_only , <EOL> cpu_only = cpu_only , verbose = verbose , <EOL> check_isfinite = True , version = version , float_atol = <NUM_LIT> ) <EOL> def tes_lenet_hd ( ) : <EOL> cmp_run_conv_nnet2_classif ( seed , ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT:9> , <NUM_LIT:2> , n_train = <NUM_LIT:3> , <EOL> ignore_error = ignore_error , gpu_only = gpu_only , <EOL> cpu_only = cpu_only , verbose = verbose , <EOL> check_isfinite = True , version = version ) <EOL> def tes_lenet_full_hd ( ) : <EOL> cmp_run_conv_nnet2_classif ( seed , ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT:9> , <NUM_LIT:2> , n_train = <NUM_LIT:3> , <EOL> ignore_error = ignore_error , gpu_only = gpu_only , <EOL> cpu_only = cpu_only , verbose = verbose , <EOL> check_isfinite = True , version = version ) </s>
