<s> from __future__ import absolute_import , print_function , division <EOL> from nose . plugins . skip import SkipTest <EOL> import numpy <EOL> import unittest <EOL> import theano <EOL> import theano . tensor as T <EOL> import theano . tests . unittest_tools as utt <EOL> import theano . sandbox . cuda as cuda <EOL> if not cuda . cuda_available : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> if theano . config . mode == '<STR_LIT>' : <EOL> mode_with_gpu = theano . compile . mode . get_mode ( '<STR_LIT>' ) . including ( '<STR_LIT>' ) <EOL> mode_without_gpu = theano . compile . mode . get_mode ( '<STR_LIT>' ) <EOL> else : <EOL> mode_with_gpu = theano . compile . mode . get_default_mode ( ) . including ( '<STR_LIT>' ) <EOL> mode_without_gpu = theano . compile . mode . get_default_mode ( ) . excluding ( '<STR_LIT>' ) <EOL> def test_GpuCrossentropySoftmaxArgmax1HotWithBias ( ) : <EOL> """<STR_LIT>""" <EOL> n_in = <NUM_LIT:1000> <EOL> batch_size = <NUM_LIT> <EOL> n_out = <NUM_LIT> <EOL> if not isinstance ( mode_with_gpu , theano . compile . DebugMode ) : <EOL> n_in = <NUM_LIT> <EOL> n_out = <NUM_LIT> <EOL> y = T . lvector ( '<STR_LIT:y>' ) <EOL> b = T . fvector ( '<STR_LIT:b>' ) <EOL> dot_result = T . fmatrix ( '<STR_LIT>' ) <EOL> utt . seed_rng ( ) <EOL> xx = numpy . asarray ( numpy . random . rand ( batch_size , n_in ) , <EOL> dtype = numpy . float32 ) <EOL> yy = numpy . ones ( ( batch_size , ) , dtype = '<STR_LIT>' ) <EOL> b_values = numpy . zeros ( ( n_out , ) , dtype = '<STR_LIT>' ) <EOL> W_values = numpy . asarray ( numpy . random . rand ( n_in , n_out ) , dtype = '<STR_LIT>' ) <EOL> dot_value = numpy . asarray ( numpy . dot ( xx , W_values ) , dtype = '<STR_LIT>' ) <EOL> del W_values <EOL> p_y_given_x = T . nnet . softmax ( dot_result + b ) <EOL> y_pred = T . argmax ( p_y_given_x , axis = - <NUM_LIT:1> ) <EOL> loss = - T . mean ( T . log ( p_y_given_x ) [ T . arange ( y . shape [ <NUM_LIT:0> ] ) , y ] ) <EOL> dW = T . grad ( loss , dot_result ) <EOL> classify = theano . function ( inputs = [ y , b , dot_result ] , <EOL> outputs = [ loss , y_pred , dW ] , <EOL> mode = mode_without_gpu ) <EOL> classify_gpu = theano . function ( inputs = [ y , b , dot_result ] , <EOL> outputs = [ loss , y_pred , dW ] , <EOL> mode = mode_with_gpu ) <EOL> assert any ( [ isinstance ( node . op , <EOL> T . nnet . CrossentropySoftmaxArgmax1HotWithBias ) <EOL> for node in classify . maker . fgraph . toposort ( ) ] ) <EOL> assert any ( [ isinstance ( node . op , <EOL> cuda . nnet . GpuCrossentropySoftmaxArgmax1HotWithBias ) <EOL> for node in classify_gpu . maker . fgraph . toposort ( ) ] ) <EOL> out = classify ( yy , b_values , dot_value ) <EOL> gout = classify_gpu ( yy , b_values , dot_value ) <EOL> assert len ( out ) == len ( gout ) == <NUM_LIT:3> <EOL> assert numpy . allclose ( out [ <NUM_LIT:0> ] , gout [ <NUM_LIT:0> ] ) <EOL> assert numpy . allclose ( out [ <NUM_LIT:2> ] , gout [ <NUM_LIT:2> ] , atol = <NUM_LIT> ) , numpy . absolute ( <EOL> gout - out ) . max ( ) <EOL> assert numpy . allclose ( out [ <NUM_LIT:1> ] , gout [ <NUM_LIT:1> ] ) , [ ( id , out [ <NUM_LIT:1> ] [ id ] , gout [ <NUM_LIT:1> ] [ id ] , val ) <EOL> for id , val in enumerate ( out [ <NUM_LIT:1> ] - <EOL> gout [ <NUM_LIT:1> ] ) <EOL> if val != <NUM_LIT:0> ] <EOL> def test_GpuCrossentropySoftmax1HotWithBiasDx ( ) : <EOL> """<STR_LIT>""" <EOL> batch_size = <NUM_LIT> <EOL> n_out = <NUM_LIT> <EOL> if not isinstance ( mode_with_gpu , theano . compile . DebugMode ) : <EOL> n_out = <NUM_LIT> <EOL> utt . seed_rng ( ) <EOL> softmax_output_value = numpy . random . rand ( batch_size , <EOL> n_out ) . astype ( '<STR_LIT>' ) <EOL> dnll_value = numpy . asarray ( numpy . random . rand ( batch_size ) , dtype = '<STR_LIT>' ) <EOL> y_idx_value = numpy . random . randint ( low = <NUM_LIT:0> , high = <NUM_LIT:5> , size = batch_size ) <EOL> softmax_output = T . fmatrix ( ) <EOL> softmax_output /= softmax_output . sum ( axis = <NUM_LIT:1> ) . reshape ( <EOL> softmax_output . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> op = theano . tensor . nnet . crossentropy_softmax_1hot_with_bias_dx ( <EOL> dnll_value , <EOL> softmax_output , <EOL> y_idx_value ) <EOL> cpu_f = theano . function ( [ softmax_output ] , op , mode = mode_without_gpu ) <EOL> gpu_f = theano . function ( [ softmax_output ] , op , mode = mode_with_gpu ) <EOL> assert any ( [ isinstance ( node . op , T . nnet . CrossentropySoftmax1HotWithBiasDx ) <EOL> for node in cpu_f . maker . fgraph . toposort ( ) ] ) <EOL> assert any ( [ isinstance ( node . op , <EOL> cuda . nnet . GpuCrossentropySoftmax1HotWithBiasDx ) <EOL> for node in gpu_f . maker . fgraph . toposort ( ) ] ) <EOL> cpu_out = cpu_f ( softmax_output_value ) <EOL> gpu_out = gpu_f ( softmax_output_value ) <EOL> rtol = <NUM_LIT> <EOL> atol = <NUM_LIT> <EOL> if not numpy . allclose ( cpu_out , gpu_out , rtol = rtol , atol = atol ) : <EOL> abs_err , rel_err = T . numeric_grad . abs_rel_err ( cpu_out , gpu_out ) <EOL> scaled_err = numpy . minimum ( abs_err / atol , rel_err / rtol ) <EOL> max_i = scaled_err . argmax ( ) <EOL> print ( '<STR_LIT>' , max_i , max_i / batch_size , end = '<STR_LIT:U+0020>' ) <EOL> print ( max_i % batch_size , max_i / n_out , max_i & n_out ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' , scaled_err . flatten ( ) [ max_i ] ) <EOL> print ( '<STR_LIT>' , abs_err . flatten ( ) [ max_i ] ) <EOL> print ( '<STR_LIT>' , rel_err . flatten ( ) [ max_i ] ) <EOL> print ( '<STR_LIT>' , cpu_out . flatten ( ) [ max_i ] ) <EOL> print ( '<STR_LIT>' , gpu_out . flatten ( ) [ max_i ] ) <EOL> print ( '<STR_LIT>' , softmax_output_value . flatten ( ) [ max_i ] ) <EOL> print ( '<STR_LIT>' , dnll_value [ max_i / n_out ] ) <EOL> print ( '<STR_LIT>' , y_idx_value [ max_i / n_out ] ) <EOL> assert False , "<STR_LIT>" % ( <EOL> rtol , atol ) <EOL> def test_softmax_with_bias ( ) : <EOL> """<STR_LIT>""" <EOL> x = T . fmatrix ( '<STR_LIT:x>' ) <EOL> z = T . nnet . softmax_with_bias ( x , T . arange ( x . shape [ <NUM_LIT:1> ] * <NUM_LIT:2> , <EOL> dtype = '<STR_LIT>' ) [ : : <NUM_LIT:2> ] ) <EOL> f = theano . function ( [ x ] , z , mode = mode_without_gpu ) <EOL> f_gpu = theano . function ( [ x ] , z , mode = mode_with_gpu ) <EOL> assert f . maker . fgraph . toposort ( ) [ - <NUM_LIT:1> ] . op == T . nnet . softmax_with_bias <EOL> assert isinstance ( f_gpu . maker . fgraph . toposort ( ) [ - <NUM_LIT:2> ] . op , <EOL> cuda . nnet . GpuSoftmaxWithBias ) <EOL> def cmp ( n , m ) : <EOL> data = numpy . arange ( n * m , dtype = '<STR_LIT>' ) . reshape ( n , m ) <EOL> out = f ( data ) <EOL> gout = f_gpu ( data ) <EOL> assert numpy . allclose ( out , gout ) , numpy . absolute ( out - gout ) <EOL> cmp ( <NUM_LIT:2> , <NUM_LIT:5> ) <EOL> cmp ( <NUM_LIT:2> << <NUM_LIT:15> , <NUM_LIT:5> ) <EOL> cmp ( <NUM_LIT> , <NUM_LIT> ) <EOL> cmp ( <NUM_LIT:0> , <NUM_LIT:10> ) <EOL> cmp ( <NUM_LIT> , <NUM_LIT> ) <EOL> cmp ( <NUM_LIT:4> , <NUM_LIT:1000> ) <EOL> cmp ( <NUM_LIT:4> , <NUM_LIT> ) <EOL> cmp ( <NUM_LIT:4> , <NUM_LIT> ) <EOL> cmp ( <NUM_LIT:4> , <NUM_LIT> ) <EOL> cmp ( <NUM_LIT:4> , <NUM_LIT> ) <EOL> cmp ( <NUM_LIT:2> , <NUM_LIT> ) <EOL> cmp ( <NUM_LIT> , <NUM_LIT:16> * <NUM_LIT> ) <EOL> cmp ( <NUM_LIT> , <NUM_LIT:64> * <NUM_LIT> ) <EOL> class test_SoftMax ( unittest . TestCase ) : <EOL> gpu_op = cuda . nnet . GpuSoftmax <EOL> mode = mode_with_gpu . excluding ( "<STR_LIT>" ) <EOL> do_big = True <EOL> do_0 = True <EOL> topo_idx = - <NUM_LIT:2> <EOL> def _test_softmax ( <EOL> self , <EOL> x , <EOL> x_gpu , <EOL> f_z , <EOL> f_gpu_z , <EOL> cmp , <EOL> check_types <EOL> ) : <EOL> """<STR_LIT>""" <EOL> f_z_out = f_z ( x ) <EOL> f_gpu_z_out = f_gpu_z ( x_gpu ) <EOL> f = theano . function ( [ x ] , f_z_out , mode = mode_without_gpu ) <EOL> f_gpu = theano . function ( [ x_gpu ] , f_gpu_z_out , mode = self . mode ) <EOL> check_types ( f , f_gpu ) <EOL> cmp ( <NUM_LIT:1> , <NUM_LIT:5> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:2> , <NUM_LIT:5> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:10> , <NUM_LIT:5> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:100> , <NUM_LIT:5> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:1000> , <NUM_LIT:5> , f , f_gpu ) <EOL> cmp ( <NUM_LIT> , <NUM_LIT:5> , f , f_gpu ) <EOL> cmp ( <NUM_LIT> , <NUM_LIT> , f , f_gpu ) <EOL> cmp ( <NUM_LIT> , <NUM_LIT> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:4> , <NUM_LIT:1000> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:4> , <NUM_LIT> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:4> , <NUM_LIT> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:4> , <NUM_LIT> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:4> , <NUM_LIT> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:2> , <NUM_LIT> , f , f_gpu ) <EOL> cmp ( <NUM_LIT> , <NUM_LIT:16> * <NUM_LIT> , f , f_gpu ) <EOL> cmp ( <NUM_LIT> , <NUM_LIT:64> * <NUM_LIT> , f , f_gpu ) <EOL> cmp ( ( <NUM_LIT:2> << <NUM_LIT:15> ) - <NUM_LIT:1> , <NUM_LIT:5> , f , f_gpu ) <EOL> cmp ( <NUM_LIT:5> , <NUM_LIT:2> << <NUM_LIT:15> , f , f_gpu ) <EOL> return f , f_gpu <EOL> def _cmp ( self , n , m , f , f_gpu ) : <EOL> data = numpy . arange ( n * m , dtype = '<STR_LIT>' ) . reshape ( n , m ) <EOL> out = f ( data ) <EOL> gout = f_gpu ( data ) <EOL> utt . assert_allclose ( out , gout ) <EOL> def _check_types ( self , graph , graph_gpu , f_type , f_gpu_type ) : <EOL> assert isinstance ( graph . maker . fgraph . toposort ( ) [ - <NUM_LIT:1> ] . op , f_type ) <EOL> assert isinstance ( <EOL> graph_gpu . maker . fgraph . toposort ( ) [ self . topo_idx ] . op , <EOL> f_gpu_type <EOL> ) <EOL> def test_softmax ( self ) : <EOL> x = T . fmatrix ( '<STR_LIT:x>' ) <EOL> z = T . nnet . softmax_op <EOL> def check_types ( graph , graph_gpu ) : <EOL> self . _check_types ( <EOL> graph , <EOL> graph_gpu , <EOL> type ( z ) , <EOL> self . gpu_op <EOL> ) <EOL> f , f_gpu = self . _test_softmax ( <EOL> x , <EOL> x , <EOL> z , <EOL> z , <EOL> self . _cmp , <EOL> check_types <EOL> ) <EOL> if self . do_big : <EOL> self . _cmp ( <NUM_LIT:2> << <NUM_LIT:15> , <NUM_LIT:5> , f , f_gpu ) <EOL> if self . do_0 : <EOL> self . _cmp ( <NUM_LIT:0> , <NUM_LIT:10> , f , f_gpu ) </s>
