<s> from __future__ import print_function <EOL> from pylearn2 . models . s3c import S3C <EOL> from pylearn2 . models . s3c import E_Step_Scan <EOL> from pylearn2 . models . s3c import Grad_M_Step <EOL> from pylearn2 . models . s3c import E_Step <EOL> from pylearn2 . utils import contains_nan <EOL> from theano import function <EOL> import numpy as np <EOL> from theano . compat . six . moves import xrange <EOL> import theano . tensor as T <EOL> from theano import config <EOL> def broadcast ( mat , shape_0 ) : <EOL> rval = mat <EOL> if mat . shape [ <NUM_LIT:0> ] != shape_0 : <EOL> assert mat . shape [ <NUM_LIT:0> ] == <NUM_LIT:1> <EOL> rval = np . zeros ( ( shape_0 , mat . shape [ <NUM_LIT:1> ] ) , dtype = mat . dtype ) <EOL> for i in xrange ( shape_0 ) : <EOL> rval [ i , : ] = mat [ <NUM_LIT:0> , : ] <EOL> return rval <EOL> class Test_S3C_Inference : <EOL> def setUp ( self ) : <EOL> self . prev_floatX = config . floatX <EOL> config . floatX = '<STR_LIT>' <EOL> def tearDown ( self ) : <EOL> config . floatX = self . prev_floatX <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . prev_floatX = config . floatX <EOL> config . floatX = '<STR_LIT>' <EOL> try : <EOL> self . tol = <NUM_LIT> <EOL> X = np . random . RandomState ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) . randn ( <NUM_LIT:1000> , <NUM_LIT:5> ) <EOL> X -= X . mean ( ) <EOL> X /= X . std ( ) <EOL> m , D = X . shape <EOL> N = <NUM_LIT:5> <EOL> self . model = S3C ( nvis = D , <EOL> nhid = N , <EOL> irange = <NUM_LIT> , <EOL> init_bias_hid = <NUM_LIT:0.> , <EOL> init_B = <NUM_LIT> , <EOL> min_B = <NUM_LIT> , <EOL> max_B = <NUM_LIT> , <EOL> init_alpha = <NUM_LIT:1.> , min_alpha = <NUM_LIT> , max_alpha = <NUM_LIT> , <EOL> init_mu = <NUM_LIT:1.> , e_step = None , <EOL> m_step = Grad_M_Step ( ) , <EOL> min_bias_hid = - <NUM_LIT> , max_bias_hid = <NUM_LIT> , <EOL> ) <EOL> self . model . make_pseudoparams ( ) <EOL> self . h_new_coeff_schedule = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.> ] <EOL> self . e_step = E_Step_Scan ( h_new_coeff_schedule = self . h_new_coeff_schedule ) <EOL> self . e_step . register_model ( self . model ) <EOL> self . X = X <EOL> self . N = N <EOL> self . m = m <EOL> finally : <EOL> config . floatX = self . prev_floatX <EOL> def test_match_unrolled ( self ) : <EOL> """<STR_LIT>""" <EOL> unrolled_e_step = E_Step ( h_new_coeff_schedule = self . h_new_coeff_schedule ) <EOL> unrolled_e_step . register_model ( self . model ) <EOL> V = T . matrix ( ) <EOL> scan_result = self . e_step . infer ( V ) <EOL> unrolled_result = unrolled_e_step . infer ( V ) <EOL> outputs = [ ] <EOL> for key in scan_result : <EOL> outputs . append ( scan_result [ key ] ) <EOL> outputs . append ( unrolled_result [ key ] ) <EOL> f = function ( [ V ] , outputs ) <EOL> outputs = f ( self . X ) <EOL> assert len ( outputs ) % <NUM_LIT:2> == <NUM_LIT:0> <EOL> for i in xrange ( <NUM_LIT:0> , len ( outputs ) , <NUM_LIT:2> ) : <EOL> assert np . allclose ( outputs [ i ] , outputs [ i + <NUM_LIT:1> ] ) <EOL> def test_grad_s ( self ) : <EOL> "<STR_LIT>" <EOL> model = self . model <EOL> e_step = self . e_step <EOL> X = self . X <EOL> assert X . shape [ <NUM_LIT:0> ] == self . m <EOL> model . test_batch_size = X . shape [ <NUM_LIT:0> ] <EOL> init_H = e_step . init_H_hat ( V = X ) <EOL> init_Mu1 = e_step . init_S_hat ( V = X ) <EOL> prev_setting = config . compute_test_value <EOL> config . compute_test_value = '<STR_LIT>' <EOL> H , Mu1 = function ( [ ] , outputs = [ init_H , init_Mu1 ] ) ( ) <EOL> config . compute_test_value = prev_setting <EOL> H = broadcast ( H , self . m ) <EOL> Mu1 = broadcast ( Mu1 , self . m ) <EOL> H = np . cast [ config . floatX ] ( self . model . rng . uniform ( <NUM_LIT:0.> , <NUM_LIT:1.> , H . shape ) ) <EOL> Mu1 = np . cast [ config . floatX ] ( self . model . rng . uniform ( - <NUM_LIT> , <NUM_LIT> , Mu1 . shape ) ) <EOL> H_var = T . matrix ( name = '<STR_LIT>' ) <EOL> H_var . tag . test_value = H <EOL> Mu1_var = T . matrix ( name = '<STR_LIT>' ) <EOL> Mu1_var . tag . test_value = Mu1 <EOL> idx = T . iscalar ( ) <EOL> idx . tag . test_value = <NUM_LIT:0> <EOL> S = e_step . infer_S_hat ( V = X , H_hat = H_var , S_hat = Mu1_var ) <EOL> s_idx = S [ : , idx ] <EOL> s_i_func = function ( [ H_var , Mu1_var , idx ] , s_idx ) <EOL> sigma0 = <NUM_LIT:1.> / model . alpha <EOL> Sigma1 = e_step . infer_var_s1_hat ( ) <EOL> mu0 = T . zeros_like ( model . mu ) <EOL> trunc_kl = - model . entropy_hs ( H_hat = H_var , var_s0_hat = sigma0 , var_s1_hat = Sigma1 ) + model . expected_energy_vhs ( V = X , H_hat = H_var , S_hat = Mu1_var , var_s0_hat = sigma0 , var_s1_hat = Sigma1 ) <EOL> grad_Mu1 = T . grad ( trunc_kl . sum ( ) , Mu1_var ) <EOL> grad_Mu1_idx = grad_Mu1 [ : , idx ] <EOL> grad_func = function ( [ H_var , Mu1_var , idx ] , grad_Mu1_idx ) <EOL> for i in xrange ( self . N ) : <EOL> Mu1 [ : , i ] = s_i_func ( H , Mu1 , i ) <EOL> g = grad_func ( H , Mu1 , i ) <EOL> assert not contains_nan ( g ) <EOL> g_abs_max = np . abs ( g ) . max ( ) <EOL> if g_abs_max > self . tol : <EOL> raise Exception ( '<STR_LIT>' + str ( g_abs_max ) + '<STR_LIT>' + str ( i ) ) <EOL> def test_value_s ( self ) : <EOL> "<STR_LIT>" <EOL> model = self . model <EOL> e_step = self . e_step <EOL> X = self . X <EOL> assert X . shape [ <NUM_LIT:0> ] == self . m <EOL> init_H = e_step . init_H_hat ( V = X ) <EOL> init_Mu1 = e_step . init_S_hat ( V = X ) <EOL> prev_setting = config . compute_test_value <EOL> config . compute_test_value = '<STR_LIT>' <EOL> H , Mu1 = function ( [ ] , outputs = [ init_H , init_Mu1 ] ) ( ) <EOL> config . compute_test_value = prev_setting <EOL> H = broadcast ( H , self . m ) <EOL> Mu1 = broadcast ( Mu1 , self . m ) <EOL> H = np . cast [ config . floatX ] ( self . model . rng . uniform ( <NUM_LIT:0.> , <NUM_LIT:1.> , H . shape ) ) <EOL> Mu1 = np . cast [ config . floatX ] ( self . model . rng . uniform ( - <NUM_LIT> , <NUM_LIT> , Mu1 . shape ) ) <EOL> H_var = T . matrix ( name = '<STR_LIT>' ) <EOL> H_var . tag . test_value = H <EOL> Mu1_var = T . matrix ( name = '<STR_LIT>' ) <EOL> Mu1_var . tag . test_value = Mu1 <EOL> idx = T . iscalar ( ) <EOL> idx . tag . test_value = <NUM_LIT:0> <EOL> S = e_step . infer_S_hat ( V = X , H_hat = H_var , S_hat = Mu1_var ) <EOL> s_idx = S [ : , idx ] <EOL> s_i_func = function ( [ H_var , Mu1_var , idx ] , s_idx ) <EOL> sigma0 = <NUM_LIT:1.> / model . alpha <EOL> Sigma1 = e_step . infer_var_s1_hat ( ) <EOL> mu0 = T . zeros_like ( model . mu ) <EOL> trunc_kl = - model . entropy_hs ( H_hat = H_var , var_s0_hat = sigma0 , var_s1_hat = Sigma1 ) + model . expected_energy_vhs ( V = X , H_hat = H_var , S_hat = Mu1_var , var_s0_hat = sigma0 , var_s1_hat = Sigma1 ) <EOL> trunc_kl_func = function ( [ H_var , Mu1_var ] , trunc_kl ) <EOL> for i in xrange ( self . N ) : <EOL> prev_kl = trunc_kl_func ( H , Mu1 ) <EOL> Mu1 [ : , i ] = s_i_func ( H , Mu1 , i ) <EOL> new_kl = trunc_kl_func ( H , Mu1 ) <EOL> increase = new_kl - prev_kl <EOL> mx = increase . max ( ) <EOL> if mx > <NUM_LIT> : <EOL> raise Exception ( '<STR_LIT>' + str ( mx ) + '<STR_LIT>' + str ( i ) ) <EOL> def test_grad_h ( self ) : <EOL> "<STR_LIT>" <EOL> model = self . model <EOL> e_step = self . e_step <EOL> X = self . X <EOL> assert X . shape [ <NUM_LIT:0> ] == self . m <EOL> init_H = e_step . init_H_hat ( V = X ) <EOL> init_Mu1 = e_step . init_S_hat ( V = X ) <EOL> prev_setting = config . compute_test_value <EOL> config . compute_test_value = '<STR_LIT>' <EOL> H , Mu1 = function ( [ ] , outputs = [ init_H , init_Mu1 ] ) ( ) <EOL> config . compute_test_value = prev_setting <EOL> H = broadcast ( H , self . m ) <EOL> Mu1 = broadcast ( Mu1 , self . m ) <EOL> H = np . cast [ config . floatX ] ( self . model . rng . uniform ( <NUM_LIT:0.> , <NUM_LIT:1.> , H . shape ) ) <EOL> Mu1 = np . cast [ config . floatX ] ( self . model . rng . uniform ( - <NUM_LIT> , <NUM_LIT> , Mu1 . shape ) ) <EOL> H_var = T . matrix ( name = '<STR_LIT>' ) <EOL> H_var . tag . test_value = H <EOL> Mu1_var = T . matrix ( name = '<STR_LIT>' ) <EOL> Mu1_var . tag . test_value = Mu1 <EOL> idx = T . iscalar ( ) <EOL> idx . tag . test_value = <NUM_LIT:0> <EOL> new_H = e_step . infer_H_hat ( V = X , H_hat = H_var , S_hat = Mu1_var ) <EOL> h_idx = new_H [ : , idx ] <EOL> updates_func = function ( [ H_var , Mu1_var , idx ] , h_idx ) <EOL> sigma0 = <NUM_LIT:1.> / model . alpha <EOL> Sigma1 = e_step . infer_var_s1_hat ( ) <EOL> mu0 = T . zeros_like ( model . mu ) <EOL> trunc_kl = - model . entropy_hs ( H_hat = H_var , var_s0_hat = sigma0 , var_s1_hat = Sigma1 ) + model . expected_energy_vhs ( V = X , H_hat = H_var , S_hat = Mu1_var , var_s0_hat = sigma0 , <EOL> var_s1_hat = Sigma1 ) <EOL> grad_H = T . grad ( trunc_kl . sum ( ) , H_var ) <EOL> assert len ( grad_H . type . broadcastable ) == <NUM_LIT:2> <EOL> grad_func = function ( [ H_var , Mu1_var ] , grad_H ) <EOL> failed = False <EOL> for i in xrange ( self . N ) : <EOL> rval = updates_func ( H , Mu1 , i ) <EOL> H [ : , i ] = rval <EOL> g = grad_func ( H , Mu1 ) [ : , i ] <EOL> assert not contains_nan ( g ) <EOL> g_abs_max = np . abs ( g ) . max ( ) <EOL> if g_abs_max > self . tol : <EOL> failed = True <EOL> print ( '<STR_LIT>' , i ) <EOL> failing_h = H [ np . abs ( g ) > self . tol , i ] <EOL> high_mask = failing_h > <NUM_LIT> <EOL> low_mask = failing_h < <NUM_LIT> <EOL> mask = high_mask * low_mask <EOL> print ( '<STR_LIT>' , mask . shape [ <NUM_LIT:0> ] , '<STR_LIT>' , g_abs_max ) <EOL> if mask . sum ( ) > <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' ) <EOL> print ( failing_h [ mask . astype ( bool ) ] ) <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> + str ( g_abs_max ) + '<STR_LIT>' + str ( i ) ) <EOL> def test_value_h ( self ) : <EOL> "<STR_LIT>" <EOL> model = self . model <EOL> e_step = self . e_step <EOL> X = self . X <EOL> assert X . shape [ <NUM_LIT:0> ] == self . m <EOL> init_H = e_step . init_H_hat ( V = X ) <EOL> init_Mu1 = e_step . init_S_hat ( V = X ) <EOL> prev_setting = config . compute_test_value <EOL> config . compute_test_value = '<STR_LIT>' <EOL> H , Mu1 = function ( [ ] , outputs = [ init_H , init_Mu1 ] ) ( ) <EOL> config . compute_test_value = prev_setting <EOL> H = broadcast ( H , self . m ) <EOL> Mu1 = broadcast ( Mu1 , self . m ) <EOL> H = np . cast [ config . floatX ] ( self . model . rng . uniform ( <NUM_LIT:0.> , <NUM_LIT:1.> , H . shape ) ) <EOL> Mu1 = np . cast [ config . floatX ] ( self . model . rng . uniform ( - <NUM_LIT> , <NUM_LIT> , Mu1 . shape ) ) <EOL> H_var = T . matrix ( name = '<STR_LIT>' ) <EOL> H_var . tag . test_value = H <EOL> Mu1_var = T . matrix ( name = '<STR_LIT>' ) <EOL> Mu1_var . tag . test_value = Mu1 <EOL> idx = T . iscalar ( ) <EOL> idx . tag . test_value = <NUM_LIT:0> <EOL> newH = e_step . infer_H_hat ( V = X , H_hat = H_var , S_hat = Mu1_var ) <EOL> h_idx = newH [ : , idx ] <EOL> h_i_func = function ( [ H_var , Mu1_var , idx ] , h_idx ) <EOL> sigma0 = <NUM_LIT:1.> / model . alpha <EOL> Sigma1 = e_step . infer_var_s1_hat ( ) <EOL> mu0 = T . zeros_like ( model . mu ) <EOL> trunc_kl = - model . entropy_hs ( H_hat = H_var , var_s0_hat = sigma0 , var_s1_hat = Sigma1 ) + model . expected_energy_vhs ( V = X , H_hat = H_var , S_hat = Mu1_var , var_s0_hat = sigma0 , var_s1_hat = Sigma1 ) <EOL> trunc_kl_func = function ( [ H_var , Mu1_var ] , trunc_kl ) <EOL> for i in xrange ( self . N ) : <EOL> prev_kl = trunc_kl_func ( H , Mu1 ) <EOL> H [ : , i ] = h_i_func ( H , Mu1 , i ) <EOL> new_kl = trunc_kl_func ( H , Mu1 ) <EOL> increase = new_kl - prev_kl <EOL> print ( '<STR_LIT>' , i , '<STR_LIT>' , ( increase > self . tol ) . sum ( ) ) <EOL> mx = increase . max ( ) <EOL> if mx > <NUM_LIT> : <EOL> print ( '<STR_LIT>' ) <EOL> print ( increase [ increase > self . tol ] ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( H [ increase > self . tol , : ] ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( Mu1 [ increase > self . tol , : ] ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( X [ increase > self . tol , : ] ) <EOL> raise Exception ( '<STR_LIT>' + str ( mx ) + '<STR_LIT>' + str ( i ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> obj = Test_S3C_Inference ( ) <EOL> obj . test_value_h ( ) </s>
