<s> import tensorflow as tf <EOL> import numpy as np <EOL> from models . autoencoder_models import stacked_denoising_autoencoder <EOL> from utils import datasets <EOL> flags = tf . app . flags <EOL> FLAGS = flags . FLAGS <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , - <NUM_LIT:1> , '<STR_LIT>' ) <EOL> flags . DEFINE_boolean ( '<STR_LIT>' , True , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:30> , '<STR_LIT>' ) <EOL> flags . DEFINE_float ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT:relu>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_float ( '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:20> , '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT:none>' , '<STR_LIT>' ) <EOL> flags . DEFINE_float ( '<STR_LIT>' , <NUM_LIT:0.0> , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> layers = [ int ( _ ) for _ in FLAGS . layers . split ( '<STR_LIT:U+002C>' ) if _ ] <EOL> xavier_init = [ int ( _ ) for _ in FLAGS . xavier_init . split ( '<STR_LIT:U+002C>' ) if _ ] <EOL> enc_act_func = [ _ for _ in FLAGS . enc_act_func . split ( '<STR_LIT:U+002C>' ) if _ ] <EOL> dec_act_func = [ _ for _ in FLAGS . dec_act_func . split ( '<STR_LIT:U+002C>' ) if _ ] <EOL> opt = [ _ for _ in FLAGS . opt . split ( '<STR_LIT:U+002C>' ) if _ ] <EOL> loss_func = [ _ for _ in FLAGS . loss_func . split ( '<STR_LIT:U+002C>' ) if _ ] <EOL> learning_rate = [ float ( _ ) for _ in FLAGS . learning_rate . split ( '<STR_LIT:U+002C>' ) if _ ] <EOL> momentum = [ float ( _ ) for _ in FLAGS . momentum . split ( '<STR_LIT:U+002C>' ) if _ ] <EOL> num_epochs = [ int ( _ ) for _ in FLAGS . num_epochs . split ( '<STR_LIT:U+002C>' ) if _ ] <EOL> batch_size = [ int ( _ ) for _ in FLAGS . batch_size . split ( '<STR_LIT:U+002C>' ) if _ ] <EOL> dae_params = { '<STR_LIT>' : layers , '<STR_LIT>' : xavier_init , '<STR_LIT>' : enc_act_func , <EOL> '<STR_LIT>' : dec_act_func , '<STR_LIT>' : loss_func , '<STR_LIT>' : learning_rate , <EOL> '<STR_LIT>' : opt , <EOL> '<STR_LIT>' : momentum , '<STR_LIT>' : num_epochs , '<STR_LIT>' : batch_size } <EOL> for p in dae_params : <EOL> if len ( dae_params [ p ] ) != len ( layers ) : <EOL> dae_params [ p ] = [ dae_params [ p ] [ <NUM_LIT:0> ] for _ in layers ] <EOL> assert <NUM_LIT:0.> <= FLAGS . corr_frac <= <NUM_LIT:1.> <EOL> assert FLAGS . corr_type in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:none>' ] <EOL> assert FLAGS . dataset in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert len ( layers ) > <NUM_LIT:0> <EOL> assert all ( [ af in [ '<STR_LIT>' , '<STR_LIT>' ] for af in enc_act_func ] ) <EOL> assert all ( [ af in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:none>' ] for af in dec_act_func ] ) <EOL> assert all ( [ lf in [ '<STR_LIT>' , '<STR_LIT>' ] for lf in loss_func ] ) <EOL> assert FLAGS . finetune_opt in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if FLAGS . dataset == '<STR_LIT>' : <EOL> trX , trY , vlX , vlY , teX , teY = datasets . load_mnist_dataset ( mode = '<STR_LIT>' ) <EOL> elif FLAGS . dataset == '<STR_LIT>' : <EOL> trX , trY , teX , teY = datasets . load_cifar10_dataset ( FLAGS . cifar_dir , mode = '<STR_LIT>' ) <EOL> vlX = teX [ : <NUM_LIT> ] <EOL> vlY = teY [ : <NUM_LIT> ] <EOL> elif FLAGS . dataset == '<STR_LIT>' : <EOL> def load_from_np ( dataset_path ) : <EOL> if dataset_path != '<STR_LIT>' : <EOL> return np . load ( dataset_path ) <EOL> else : <EOL> return None <EOL> trX , trY = load_from_np ( FLAGS . train_dataset ) , load_from_np ( FLAGS . train_labels ) <EOL> vlX , vlY = load_from_np ( FLAGS . valid_dataset ) , load_from_np ( FLAGS . valid_labels ) <EOL> teX , teY = load_from_np ( FLAGS . test_dataset ) , load_from_np ( FLAGS . test_labels ) <EOL> else : <EOL> trX = None <EOL> trY = None <EOL> vlX = None <EOL> vlY = None <EOL> teX = None <EOL> teY = None <EOL> sdae = None <EOL> sdae = stacked_denoising_autoencoder . StackedDenoisingAutoencoder ( <EOL> do_pretrain = FLAGS . do_pretrain , <EOL> layers = dae_params [ '<STR_LIT>' ] , seed = FLAGS . seed , finetune_loss_func = FLAGS . finetune_loss_func , <EOL> finetune_learning_rate = FLAGS . finetune_learning_rate , finetune_num_epochs = FLAGS . finetune_num_epochs , <EOL> finetune_opt = FLAGS . finetune_opt , finetune_batch_size = FLAGS . finetune_batch_size , dropout = FLAGS . dropout , <EOL> enc_act_func = dae_params [ '<STR_LIT>' ] , dec_act_func = dae_params [ '<STR_LIT>' ] , <EOL> xavier_init = dae_params [ '<STR_LIT>' ] , corr_type = FLAGS . corr_type , corr_frac = FLAGS . corr_frac , <EOL> dataset = FLAGS . dataset , loss_func = dae_params [ '<STR_LIT>' ] , main_dir = FLAGS . main_dir , opt = dae_params [ '<STR_LIT>' ] , <EOL> learning_rate = dae_params [ '<STR_LIT>' ] , momentum = dae_params [ '<STR_LIT>' ] , verbose = FLAGS . verbose , <EOL> num_epochs = dae_params [ '<STR_LIT>' ] , batch_size = dae_params [ '<STR_LIT>' ] , <EOL> finetune_act_func = FLAGS . finetune_act_func ) <EOL> if FLAGS . do_pretrain : <EOL> encoded_X , encoded_vX = sdae . pretrain ( trX , vlX ) <EOL> sdae . build_supervised_model ( trX . shape [ <NUM_LIT:1> ] , trY . shape [ <NUM_LIT:1> ] ) <EOL> sdae . fit ( trX , trY , vlX , vlY , mode = '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' . format ( sdae . compute_accuracy ( teX , teY ) ) ) <EOL> if FLAGS . save_predictions : <EOL> print ( '<STR_LIT>' ) <EOL> np . save ( FLAGS . save_predictions , sdae . predict ( teX ) ) <EOL> if FLAGS . save_layers_output : <EOL> print ( '<STR_LIT>' ) <EOL> out = sdae . get_layers_output ( teX ) <EOL> for i , o in enumerate ( out ) : <EOL> np . save ( FLAGS . save_layers_output + '<STR_LIT>' + str ( i + <NUM_LIT:1> ) , o ) </s>
