<s> import theano <EOL> import theano . tensor as T <EOL> from theano . sandbox . rng_mrg import MRG_RandomStreams <EOL> from theano . tensor . nnet . conv import conv2d <EOL> from theano . tensor . signal . downsample import max_pool_2d <EOL> from theano . tensor . shared_randomstreams import RandomStreams <EOL> import numpy as np <EOL> from toolbox import * <EOL> from modelbase import * <EOL> import itertools <EOL> class FFN_vat ( ModelSLBase ) : <EOL> """<STR_LIT>""" <EOL> def save ( self ) : <EOL> if not os . path . exists ( '<STR_LIT>' ) : <EOL> os . makedirs ( '<STR_LIT>' ) <EOL> self . params . save ( self . filename ) <EOL> self . shared_vars . save ( self . filename + '<STR_LIT>' ) <EOL> def __init__ ( self , data , hp ) : <EOL> super ( FFN_vat , self ) . __init__ ( self . __class__ . __name__ , data , hp ) <EOL> self . epsilon = <NUM_LIT> <EOL> self . params = Parameters ( ) <EOL> self . shared_vars = Parameters ( ) <EOL> n_x = self . data [ '<STR_LIT>' ] <EOL> n_y = self . data [ '<STR_LIT>' ] <EOL> dropout_rate = <NUM_LIT> <EOL> n_h1 = <NUM_LIT> <EOL> n_h2 = <NUM_LIT:1000> <EOL> n_h3 = <NUM_LIT> <EOL> scale = hp . init_scale <EOL> if hp . load_model and os . path . isfile ( self . filename ) : <EOL> self . params . load ( self . filename ) <EOL> self . shared_vars . load ( self . filename + '<STR_LIT>' ) <EOL> else : <EOL> with self . params : <EOL> w_h = shared_normal ( ( n_x , n_h1 ) , scale = scale ) <EOL> b_h = shared_normal ( ( n_h1 , ) , scale = <NUM_LIT:0> ) <EOL> w_h2 = shared_normal ( ( n_h1 , n_h2 ) , scale = scale ) <EOL> b_h2 = shared_normal ( ( n_h2 , ) , scale = <NUM_LIT:0> ) <EOL> w_h3 = shared_normal ( ( n_h2 , n_h3 ) , scale = scale ) <EOL> b_h3 = shared_normal ( ( n_h3 , ) , scale = <NUM_LIT:0> ) <EOL> w_o = shared_normal ( ( n_h3 , n_y ) , scale = scale ) <EOL> def model ( X , params , shared_vars , p_drop_hidden ) : <EOL> h = dropout ( rectify ( T . dot ( X , params . w_h ) + params . b_h ) , p_drop_hidden ) <EOL> h2 = dropout ( rectify ( T . dot ( h , params . w_h2 ) + params . b_h2 ) , p_drop_hidden ) <EOL> h3 = dropout ( rectify ( T . dot ( h2 , params . w_h3 ) + params . b_h3 ) , p_drop_hidden ) <EOL> py_x = softmax ( T . dot ( h3 , params . w_o ) ) <EOL> return py_x <EOL> add_updates = [ ] <EOL> adv_est_noise = <NUM_LIT> <EOL> sl_noise = <NUM_LIT> <EOL> adv_noise = <NUM_LIT> <EOL> adv_cost_coeff = <NUM_LIT:1.0> <EOL> clean_py_x = model ( self . X , self . params , self . shared_vars , dropout_rate ) <EOL> adv_X = normalize ( gaussian ( self . X . shape , <NUM_LIT:1.0> ) ) <EOL> adv_py_x = model ( self . X + adv_X * adv_est_noise , self . params , self . shared_vars , dropout_rate ) <EOL> cost_adv = T . sum ( T . nnet . categorical_crossentropy ( adv_py_x , clean_py_x ) ) <EOL> adv_X = T . grad ( cost = cost_adv , wrt = self . X ) <EOL> adv_X = theano . gradient . disconnected_grad ( adv_X ) <EOL> adv_X = normalize ( adv_X ) <EOL> adv_py_x = model ( self . X + adv_X * adv_noise , self . params , self . shared_vars , dropout_rate ) <EOL> noise_py_x = model ( self . X + gaussian ( self . X . shape , sl_noise ) , self . params , self . shared_vars , dropout_rate ) <EOL> noise_py_x_hat = theano . gradient . disconnected_grad ( noise_py_x ) <EOL> sl_cost = T . sum ( T . nnet . categorical_crossentropy ( noise_py_x , self . Y ) ) <EOL> adv_cost = T . sum ( T . nnet . categorical_crossentropy ( adv_py_x , noise_py_x_hat ) ) <EOL> cost = sl_cost + adv_cost * adv_cost_coeff <EOL> pyx = model ( self . X , self . params , self . shared_vars , <NUM_LIT:0.> ) <EOL> map_pyx = T . argmax ( pyx , axis = <NUM_LIT:1> ) <EOL> error_map_pyx = T . sum ( T . neq ( map_pyx , T . argmax ( self . Y , axis = <NUM_LIT:1> ) ) ) <EOL> self . compile ( cost , error_map_pyx , add_updates , [ adv_X ] ) </s>
