<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import matplotlib <EOL> import matplotlib . pyplot as plt <EOL> import deeppy as dp <EOL> import deeppy . expr as expr <EOL> dataset = dp . dataset . MNIST ( ) <EOL> x_train , y_train , x_test , y_test = dataset . arrays ( dp_dtypes = True , flat = True ) <EOL> n_classes = dataset . n_classes <EOL> img_shape = dataset . img_shape <EOL> scaler = dp . UniformScaler ( ) <EOL> x_train = scaler . fit_transform ( x_train ) <EOL> x_test = scaler . transform ( x_test ) <EOL> scaler = dp . UniformScaler ( ) <EOL> x_train = scaler . fit_transform ( x_train ) <EOL> x_test = scaler . transform ( x_test ) <EOL> x_train = np . reshape ( x_train , ( x_train . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) ) <EOL> x_test = np . reshape ( x_test , ( x_test . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) ) <EOL> def affine ( n_out ) : <EOL> return expr . nnet . Affine ( n_out = n_out , weights = dp . AutoFiller ( gain = <NUM_LIT> ) ) <EOL> n_in = x_train . shape [ <NUM_LIT:1> ] <EOL> n_discriminator = <NUM_LIT> <EOL> n_hidden = <NUM_LIT:64> <EOL> n_generator = <NUM_LIT> <EOL> generator = expr . Sequential ( [ <EOL> affine ( n_generator ) , <EOL> expr . nnet . BatchNormalization ( ) , <EOL> expr . nnet . ReLU ( ) , <EOL> affine ( n_generator ) , <EOL> expr . nnet . BatchNormalization ( ) , <EOL> expr . nnet . ReLU ( ) , <EOL> affine ( n_in ) , <EOL> expr . nnet . Sigmoid ( ) , <EOL> ] ) <EOL> discriminator = expr . Sequential ( [ <EOL> expr . nnet . Dropout ( <NUM_LIT:0.5> ) , <EOL> affine ( n_discriminator ) , <EOL> expr . nnet . ReLU ( ) , <EOL> expr . nnet . Dropout ( <NUM_LIT:0.5> ) , <EOL> affine ( n_discriminator ) , <EOL> expr . nnet . ReLU ( ) , <EOL> affine ( <NUM_LIT:1> ) , <EOL> expr . nnet . Sigmoid ( ) , <EOL> ] ) <EOL> model = dp . model . AdversarialNet ( generator , discriminator , n_hidden = <NUM_LIT> ) <EOL> batch_size = <NUM_LIT:64> <EOL> train_feed = dp . Feed ( x_train , batch_size = batch_size ) <EOL> n_examples = <NUM_LIT:100> <EOL> samples = np . random . normal ( size = ( n_examples , model . n_hidden ) ) . astype ( dp . float_ ) <EOL> plot_epochs = [ <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT> ] <EOL> plot_imgs = [ ( x_train [ : n_examples ] , '<STR_LIT>' ) ] <EOL> n_epochs = <NUM_LIT:15> <EOL> margin = <NUM_LIT> <EOL> equilibrium = <NUM_LIT> <EOL> learn_rate = <NUM_LIT> <EOL> learn_rule_g = dp . RMSProp ( learn_rate = learn_rate ) <EOL> learn_rule_d = dp . RMSProp ( learn_rate = learn_rate ) <EOL> model . setup ( * train_feed . shapes ) <EOL> g_params , d_params = model . params <EOL> learn_rule_g . learn_rate /= batch_size <EOL> learn_rule_d . learn_rate /= batch_size * <NUM_LIT:2> <EOL> g_states = [ learn_rule_g . init_state ( p ) for p in g_params ] <EOL> d_states = [ learn_rule_d . init_state ( p ) for p in d_params ] <EOL> for epoch in range ( n_epochs ) : <EOL> batch_costs = [ ] <EOL> for x , in train_feed . batches ( ) : <EOL> real_cost , gen_cost = model . update ( x ) <EOL> batch_costs . append ( ( real_cost , gen_cost ) ) <EOL> update_g = True <EOL> update_d = True <EOL> if real_cost < equilibrium - margin or gen_cost < equilibrium - margin : <EOL> update_d = False <EOL> if real_cost > equilibrium + margin or gen_cost > equilibrium + margin : <EOL> update_g = False <EOL> if not ( update_g or update_d ) : <EOL> update_g = True <EOL> update_d = True <EOL> if update_g : <EOL> for param , state in zip ( g_params , g_states ) : <EOL> learn_rule_g . step ( param , state ) <EOL> if update_d : <EOL> for param , state in zip ( d_params , d_states ) : <EOL> learn_rule_d . step ( param , state ) <EOL> real_cost = np . mean ( [ cost [ <NUM_LIT:0> ] for cost in batch_costs ] ) <EOL> gen_cost = np . mean ( [ cost [ <NUM_LIT:1> ] for cost in batch_costs ] ) <EOL> print ( '<STR_LIT>' % ( epoch , real_cost , <EOL> gen_cost ) ) <EOL> if epoch in plot_epochs : <EOL> samples_img = model . generate ( samples ) <EOL> plot_imgs . append ( ( samples_img , '<STR_LIT>' % ( epoch + <NUM_LIT:1> ) ) ) <EOL> model . setup ( train_feed . x_shape ) <EOL> model . phase = '<STR_LIT:train>' <EOL> fig = plt . figure ( ) <EOL> fig_gs = matplotlib . gridspec . GridSpec ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> for i , ( imgs , title ) in enumerate ( plot_imgs ) : <EOL> imgs = np . reshape ( imgs , ( - <NUM_LIT:1> , ) + img_shape ) <EOL> imgs = dp . misc . to_b01c ( imgs ) <EOL> img_tile = dp . misc . img_tile ( dp . misc . img_stretch ( imgs ) ) <EOL> ax = plt . subplot ( fig_gs [ i // <NUM_LIT:2> , i % <NUM_LIT:2> ] ) <EOL> ax . imshow ( img_tile , interpolation = '<STR_LIT>' , cmap = '<STR_LIT>' ) <EOL> ax . set_title ( title ) <EOL> ax . axis ( '<STR_LIT>' ) <EOL> plt . tight_layout ( ) </s>
