#NEWLINE# @ no_debug_mode #NEWLINE# def test_ais( ) : #NEWLINE# #INDENT# "\n    Test ais computation by comparing the output of estimate_likelihood to\n    Russ's code's output for the same parameters.\n    " #NEWLINE# try : #NEWLINE# #INDENT# trainset = MNIST ( which_set = 'train' ) #NEWLINE# testset = MNIST ( which_set = 'test' ) #NEWLINE# #UNINDENT# except NoDataPathError : #NEWLINE# #INDENT# raise SkipTest ( 'PYLEARN2_DATA_PATH environment variable not defined' ) #NEWLINE# #UNINDENT# nvis = 784 #NEWLINE# nhid = 20 #NEWLINE# numpy . random . seed ( 98734 ) #NEWLINE# w_hid = ( 10 * numpy . cast [ theano . config . floatX ] ( numpy . random . randn ( nvis , nhid ) ) ) #NEWLINE# b_vis = ( 10 * numpy . cast [ theano . config . floatX ] ( numpy . random . randn ( nvis ) ) ) #NEWLINE# b_hid = ( 10 * numpy . cast [ theano . config . floatX ] ( numpy . random . randn ( nhid ) ) ) #NEWLINE# visible_layer = BinaryVector ( nvis ) #NEWLINE# hidden_layer = BinaryVectorMaxPool ( detector_layer_dim = nhid , pool_size = 1 , layer_name = 'h' , irange = 0.1 ) #NEWLINE# rbm = DBM ( 100 , visible_layer , [ hidden_layer ] , 1 ) #NEWLINE# rbm . visible_layer . set_biases ( b_vis ) #NEWLINE# rbm . hidden_layers [ 0 ] . set_weights ( w_hid ) #NEWLINE# rbm . hidden_layers [ 0 ] . set_biases ( b_hid ) #NEWLINE# rbm . nvis = nvis #NEWLINE# rbm . nhid = nhid #NEWLINE# v_sample = T . matrix ( 'v_sample' ) #NEWLINE# h_sample = T . matrix ( 'h_sample' ) #NEWLINE# W = theano . shared ( rbm . hidden_layers [ 0 ] . get_weights ( ) ) #NEWLINE# hbias = theano . shared ( rbm . hidden_layers [ 0 ] . get_biases ( ) ) #NEWLINE# vbias = theano . shared ( rbm . visible_layer . get_biases ( ) ) #NEWLINE# wx_b = ( T . dot ( v_sample , W ) + hbias ) #NEWLINE# vbias_term = T . dot ( v_sample , vbias ) #NEWLINE# hidden_term = T . sum ( T . log ( ( 1 + T . exp ( wx_b ) ) ) , axis = 1 ) #NEWLINE# free_energy_v = ( ( - hidden_term ) - vbias_term ) #NEWLINE# free_energy_v_fn = theano . function ( inputs = [ v_sample ] , outputs = free_energy_v ) #NEWLINE# wh_c = ( T . dot ( h_sample , W . T ) + vbias ) #NEWLINE# hbias_term = T . dot ( h_sample , hbias ) #NEWLINE# visible_term = T . sum ( T . log ( ( 1 + T . exp ( wh_c ) ) ) , axis = 1 ) #NEWLINE# free_energy_h = ( ( - visible_term ) - hbias_term ) #NEWLINE# free_energy_h_fn = theano . function ( inputs = [ h_sample ] , outputs = free_energy_h ) #NEWLINE# real_logz = rbm_tools . compute_log_z ( rbm , free_energy_h_fn ) #NEWLINE# real_ais_train_ll = ( - rbm_tools . compute_nll ( rbm , trainset . get_design_matrix ( ) , real_logz , free_energy_v_fn ) ) #NEWLINE# real_ais_test_ll = ( - rbm_tools . compute_nll ( rbm , testset . get_design_matrix ( ) , real_logz , free_energy_v_fn ) ) #NEWLINE# ( train_ll , test_ll , logz ) = dbm_metrics . estimate_likelihood ( [ W ] , [ vbias , hbias ] , trainset , testset , pos_mf_steps = 100 ) #NEWLINE# assert ( ( real_logz - logz ) < 2.0 ) #NEWLINE# assert ( ( real_ais_train_ll - train_ll ) < 2.0 ) #NEWLINE# assert ( ( real_ais_test_ll - test_ll ) < 2.0 )