<s> """<STR_LIT>""" <EOL> import os . path <EOL> import numpy as np <EOL> import theano <EOL> import theano . tensor as tensor <EOL> from theano import config <EOL> from pylearn2 . models . autoencoder import Autoencoder , HigherOrderContractiveAutoencoder , DeepComposedAutoencoder , UntiedAutoencoder , StackedDenoisingAutoencoder <EOL> from pylearn2 . corruption import BinomialCorruptor <EOL> from pylearn2 . config import yaml_parse <EOL> from theano . tensor . basic import _allclose <EOL> yaml_dir_path = os . path . join ( <EOL> os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) ) ) , '<STR_LIT>' ) <EOL> def test_autoencoder_properly_initialized ( ) : <EOL> ae = Autoencoder ( <NUM_LIT:1> , <NUM_LIT:1> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert hasattr ( ae , '<STR_LIT>' ) , "<STR_LIT>" <EOL> assert hasattr ( ae , '<STR_LIT>' ) , "<STR_LIT>" <EOL> def test_autoencoder_logistic_linear_tied ( ) : <EOL> data = np . random . randn ( <NUM_LIT:10> , <NUM_LIT:5> ) . astype ( config . floatX ) <EOL> ae = Autoencoder ( <NUM_LIT:5> , <NUM_LIT:7> , act_enc = '<STR_LIT>' , act_dec = '<STR_LIT>' , <EOL> tied_weights = True ) <EOL> w = ae . weights . get_value ( ) <EOL> ae . hidbias . set_value ( np . random . randn ( <NUM_LIT:7> ) . astype ( config . floatX ) ) <EOL> hb = ae . hidbias . get_value ( ) <EOL> ae . visbias . set_value ( np . random . randn ( <NUM_LIT:5> ) . astype ( config . floatX ) ) <EOL> vb = ae . visbias . get_value ( ) <EOL> d = tensor . matrix ( ) <EOL> result = np . dot ( <NUM_LIT:1.> / ( <NUM_LIT:1> + np . exp ( - hb - np . dot ( data , w ) ) ) , w . T ) + vb <EOL> ff = theano . function ( [ d ] , ae . reconstruct ( d ) ) <EOL> assert _allclose ( ff ( data ) , result ) <EOL> def test_autoencoder_tanh_cos_untied ( ) : <EOL> data = np . random . randn ( <NUM_LIT:10> , <NUM_LIT:5> ) . astype ( config . floatX ) <EOL> ae = Autoencoder ( <NUM_LIT:5> , <NUM_LIT:7> , act_enc = '<STR_LIT>' , act_dec = '<STR_LIT>' , <EOL> tied_weights = False ) <EOL> w = ae . weights . get_value ( ) <EOL> w_prime = ae . w_prime . get_value ( ) <EOL> ae . hidbias . set_value ( np . random . randn ( <NUM_LIT:7> ) . astype ( config . floatX ) ) <EOL> hb = ae . hidbias . get_value ( ) <EOL> ae . visbias . set_value ( np . random . randn ( <NUM_LIT:5> ) . astype ( config . floatX ) ) <EOL> vb = ae . visbias . get_value ( ) <EOL> d = tensor . matrix ( ) <EOL> result = np . cos ( np . dot ( np . tanh ( hb + np . dot ( data , w ) ) , w_prime ) + vb ) <EOL> ff = theano . function ( [ d ] , ae . reconstruct ( d ) ) <EOL> assert _allclose ( ff ( data ) , result ) <EOL> def test_high_order_autoencoder_init ( ) : <EOL> """<STR_LIT>""" <EOL> corruptor = BinomialCorruptor ( corruption_level = <NUM_LIT:0.5> ) <EOL> model = HigherOrderContractiveAutoencoder ( <EOL> corruptor = corruptor , <EOL> num_corruptions = <NUM_LIT:2> , <EOL> nvis = <NUM_LIT:5> , <EOL> nhid = <NUM_LIT:7> , <EOL> act_enc = '<STR_LIT>' , <EOL> act_dec = '<STR_LIT>' ) <EOL> X = tensor . matrix ( ) <EOL> data = np . random . randn ( <NUM_LIT:10> , <NUM_LIT:5> ) . astype ( config . floatX ) <EOL> ff = theano . function ( [ X ] , model . higher_order_penalty ( X ) ) <EOL> assert type ( ff ( data ) ) == np . ndarray <EOL> def test_cae_basic ( ) : <EOL> """<STR_LIT>""" <EOL> with open ( os . path . join ( yaml_dir_path , '<STR_LIT>' ) ) as f : <EOL> yaml_string = f . read ( ) <EOL> train = yaml_parse . load ( yaml_string ) <EOL> train . main_loop ( ) <EOL> def test_hcae_basic ( ) : <EOL> """<STR_LIT>""" <EOL> with open ( os . path . join ( yaml_dir_path , '<STR_LIT>' ) ) as f : <EOL> yaml_string = f . read ( ) <EOL> train = yaml_parse . load ( yaml_string ) <EOL> train . main_loop ( ) <EOL> def test_untied_ae ( ) : <EOL> """<STR_LIT>""" <EOL> ae = Autoencoder ( <NUM_LIT:5> , <NUM_LIT:7> , act_enc = '<STR_LIT>' , act_dec = '<STR_LIT>' , <EOL> tied_weights = True ) <EOL> model = UntiedAutoencoder ( ae ) <EOL> model . _ensure_extensions ( ) <EOL> def test_dcae ( ) : <EOL> """<STR_LIT>""" <EOL> ae = Autoencoder ( <NUM_LIT:5> , <NUM_LIT:7> , act_enc = '<STR_LIT>' , act_dec = '<STR_LIT>' , <EOL> tied_weights = True ) <EOL> model = DeepComposedAutoencoder ( [ ae ] ) <EOL> model . _ensure_extensions ( ) <EOL> data = np . random . randn ( <NUM_LIT:10> , <NUM_LIT:5> ) . astype ( config . floatX ) <EOL> model . perform ( data ) <EOL> def test_sdae ( ) : <EOL> """<STR_LIT>""" <EOL> data = np . random . randn ( <NUM_LIT:10> , <NUM_LIT:5> ) . astype ( config . floatX ) * <NUM_LIT:100> <EOL> ae = Autoencoder ( <NUM_LIT:5> , <NUM_LIT:7> , act_enc = '<STR_LIT>' , act_dec = '<STR_LIT>' , <EOL> tied_weights = False ) <EOL> corruptor = BinomialCorruptor ( corruption_level = <NUM_LIT:0.5> ) <EOL> model = StackedDenoisingAutoencoder ( [ ae ] , corruptor ) <EOL> model . _ensure_extensions ( ) <EOL> w = ae . weights . get_value ( ) <EOL> w_prime = ae . w_prime . get_value ( ) <EOL> ae . hidbias . set_value ( np . random . randn ( <NUM_LIT:7> ) . astype ( config . floatX ) ) <EOL> hb = ae . hidbias . get_value ( ) <EOL> ae . visbias . set_value ( np . random . randn ( <NUM_LIT:5> ) . astype ( config . floatX ) ) <EOL> vb = ae . visbias . get_value ( ) <EOL> d = tensor . matrix ( ) <EOL> result = np . cos ( np . dot ( np . tanh ( hb + np . dot ( data , w ) ) , w_prime ) + vb ) <EOL> ff = theano . function ( [ d ] , model . reconstruct ( d ) ) <EOL> assert not _allclose ( ff ( data ) , result ) </s>
