<s> """<STR_LIT>""" <EOL> __authors__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> import numpy as np <EOL> import theano <EOL> from theano . gof . op import get_debug_values <EOL> from theano import tensor as T <EOL> from pylearn2 . models . mlp import MLP , Sigmoid <EOL> from pylearn2 . expr . nnet import arg_of_sigmoid <EOL> from pylearn2 . expr . nnet import pseudoinverse_softmax_numpy <EOL> from pylearn2 . expr . nnet import softmax_numpy <EOL> from pylearn2 . expr . nnet import softmax_ratio <EOL> from pylearn2 . expr . nnet import compute_recall <EOL> from pylearn2 . expr . nnet import kl <EOL> from pylearn2 . expr . nnet import elemwise_kl <EOL> from pylearn2 . utils import sharedX <EOL> def test_softmax_ratio ( ) : <EOL> n = <NUM_LIT:3> <EOL> m = <NUM_LIT:4> <EOL> rng = np . random . RandomState ( [ <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT> ] ) <EOL> Z_numer = sharedX ( rng . randn ( m , n ) ) <EOL> Z_denom = sharedX ( rng . randn ( m , n ) ) <EOL> numer = T . nnet . softmax ( Z_numer ) <EOL> denom = T . nnet . softmax ( Z_denom ) <EOL> naive = numer / denom <EOL> stable = softmax_ratio ( numer , denom ) <EOL> naive = naive . eval ( ) <EOL> stable = stable . eval ( ) <EOL> assert np . allclose ( naive , stable ) <EOL> def test_pseudoinverse_softmax_numpy ( ) : <EOL> rng = np . random . RandomState ( [ <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT> ] ) <EOL> p = np . abs ( rng . randn ( <NUM_LIT:5> ) ) <EOL> p /= p . sum ( ) <EOL> z = pseudoinverse_softmax_numpy ( p ) <EOL> zbroad = z . reshape ( <NUM_LIT:1> , z . size ) <EOL> p2 = softmax_numpy ( zbroad ) <EOL> p2 = p2 [ <NUM_LIT:0> , : ] <EOL> assert np . allclose ( p , p2 ) <EOL> def test_compute_recall ( ) : <EOL> """<STR_LIT>""" <EOL> tp_pyval = <NUM_LIT:4> <EOL> ys_pyval = np . asarray ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> tp = sharedX ( tp_pyval , name = "<STR_LIT>" ) <EOL> ys = sharedX ( ys_pyval , name = "<STR_LIT>" ) <EOL> recall_py = tp_pyval / ys_pyval . sum ( ) <EOL> recall = compute_recall ( ys , tp ) <EOL> assert np . allclose ( recall . eval ( ) , <EOL> recall_py ) <EOL> def test_kl ( ) : <EOL> """<STR_LIT>""" <EOL> init_mode = theano . config . compute_test_value <EOL> theano . config . compute_test_value = '<STR_LIT>' <EOL> try : <EOL> mlp = MLP ( layers = [ Sigmoid ( dim = <NUM_LIT:10> , layer_name = '<STR_LIT:Y>' , irange = <NUM_LIT:0.1> ) ] , <EOL> nvis = <NUM_LIT:10> ) <EOL> X = mlp . get_input_space ( ) . make_theano_batch ( ) <EOL> Y = mlp . get_output_space ( ) . make_theano_batch ( ) <EOL> X . tag . test_value = np . random . random ( <EOL> get_debug_values ( X ) [ <NUM_LIT:0> ] . shape ) . astype ( theano . config . floatX ) <EOL> Y_hat = mlp . fprop ( X ) <EOL> ave = kl ( Y , Y_hat , <NUM_LIT:1> ) <EOL> Y . tag . test_value [ <NUM_LIT:2> ] [ <NUM_LIT:3> ] = <NUM_LIT> <EOL> np . testing . assert_raises ( ValueError , kl , Y , Y_hat , <NUM_LIT:1> ) <EOL> Y . tag . test_value [ <NUM_LIT:2> ] [ <NUM_LIT:3> ] = - <NUM_LIT:0.1> <EOL> np . testing . assert_raises ( ValueError , kl , Y , Y_hat , <NUM_LIT:1> ) <EOL> finally : <EOL> theano . config . compute_test_value = init_mode <EOL> def test_elemwise_kl ( ) : <EOL> """<STR_LIT>""" <EOL> init_mode = theano . config . compute_test_value <EOL> theano . config . compute_test_value = '<STR_LIT>' <EOL> try : <EOL> mlp = MLP ( layers = [ Sigmoid ( dim = <NUM_LIT:10> , layer_name = '<STR_LIT:Y>' , irange = <NUM_LIT:0.1> ) ] , <EOL> nvis = <NUM_LIT:10> ) <EOL> X = mlp . get_input_space ( ) . make_theano_batch ( ) <EOL> Y = mlp . get_output_space ( ) . make_theano_batch ( ) <EOL> X . tag . test_value = np . random . random ( <EOL> get_debug_values ( X ) [ <NUM_LIT:0> ] . shape ) . astype ( theano . config . floatX ) <EOL> Y_hat = mlp . fprop ( X ) <EOL> ave = elemwise_kl ( Y , Y_hat ) <EOL> Y . tag . test_value [ <NUM_LIT:2> ] [ <NUM_LIT:3> ] = <NUM_LIT> <EOL> np . testing . assert_raises ( ValueError , elemwise_kl , Y , Y_hat ) <EOL> Y . tag . test_value [ <NUM_LIT:2> ] [ <NUM_LIT:3> ] = - <NUM_LIT:0.1> <EOL> np . testing . assert_raises ( ValueError , elemwise_kl , Y , Y_hat ) <EOL> finally : <EOL> theano . config . compute_test_value = init_mode <EOL> def test_arg_of_sigmoid_good ( ) : <EOL> """<STR_LIT>""" <EOL> X = T . matrix ( ) <EOL> Y = T . nnet . sigmoid ( X ) <EOL> Z = arg_of_sigmoid ( Y ) <EOL> assert X is Z <EOL> def test_arg_of_sigmoid_bad ( ) : <EOL> """<STR_LIT>""" <EOL> X = T . matrix ( ) <EOL> Y = T . nnet . softmax ( X ) <EOL> try : <EOL> Z = arg_of_sigmoid ( Y ) <EOL> except TypeError : <EOL> return <EOL> assert False </s>
