<s> import tensorflow . python . platform <EOL> import numpy as np <EOL> import tensorflow as tf <EOL> NUM_LABELS = <NUM_LIT:2> <EOL> BATCH_SIZE = <NUM_LIT:100> <EOL> tf . app . flags . DEFINE_string ( '<STR_LIT:train>' , None , <EOL> '<STR_LIT>' ) <EOL> tf . app . flags . DEFINE_string ( '<STR_LIT:test>' , None , <EOL> '<STR_LIT>' ) <EOL> tf . app . flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> '<STR_LIT>' ) <EOL> tf . app . flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:1> , <EOL> '<STR_LIT>' ) <EOL> tf . app . flags . DEFINE_boolean ( '<STR_LIT>' , False , '<STR_LIT>' ) <EOL> FLAGS = tf . app . flags . FLAGS <EOL> def extract_data ( filename ) : <EOL> labels = [ ] <EOL> fvecs = [ ] <EOL> for line in file ( filename ) : <EOL> row = line . split ( "<STR_LIT:U+002C>" ) <EOL> labels . append ( int ( row [ <NUM_LIT:0> ] ) ) <EOL> fvecs . append ( [ float ( x ) for x in row [ <NUM_LIT:1> : ] ] ) <EOL> fvecs_np = np . matrix ( fvecs ) . astype ( np . float32 ) <EOL> labels_np = np . array ( labels ) . astype ( dtype = np . uint8 ) <EOL> labels_onehot = ( np . arange ( NUM_LABELS ) == labels_np [ : , None ] ) . astype ( np . float32 ) <EOL> return fvecs_np , labels_onehot <EOL> def init_weights ( shape , init_method = '<STR_LIT>' , xavier_params = ( None , None ) ) : <EOL> if init_method == '<STR_LIT>' : <EOL> return tf . Variable ( tf . zeros ( shape , dtype = tf . float32 ) ) <EOL> elif init_method == '<STR_LIT>' : <EOL> return tf . Variable ( tf . random_normal ( shape , stddev = <NUM_LIT> , dtype = tf . float32 ) ) <EOL> else : <EOL> ( fan_in , fan_out ) = xavier_params <EOL> low = - <NUM_LIT:4> * np . sqrt ( <NUM_LIT> / ( fan_in + fan_out ) ) <EOL> high = <NUM_LIT:4> * np . sqrt ( <NUM_LIT> / ( fan_in + fan_out ) ) <EOL> return tf . Variable ( tf . random_uniform ( shape , minval = low , maxval = high , dtype = tf . float32 ) ) <EOL> def main ( argv = None ) : <EOL> verbose = FLAGS . verbose <EOL> train_data_filename = FLAGS . train <EOL> test_data_filename = FLAGS . test <EOL> train_data , train_labels = extract_data ( train_data_filename ) <EOL> test_data , test_labels = extract_data ( test_data_filename ) <EOL> train_size , num_features = train_data . shape <EOL> num_epochs = FLAGS . num_epochs <EOL> num_hidden = FLAGS . num_hidden <EOL> x = tf . placeholder ( "<STR_LIT:float>" , shape = [ None , num_features ] ) <EOL> y_ = tf . placeholder ( "<STR_LIT:float>" , shape = [ None , NUM_LABELS ] ) <EOL> test_data_node = tf . constant ( test_data ) <EOL> w_hidden = init_weights ( <EOL> [ num_features , num_hidden ] , <EOL> '<STR_LIT>' , <EOL> xavier_params = ( num_features , num_hidden ) ) <EOL> b_hidden = init_weights ( [ <NUM_LIT:1> , num_hidden ] , '<STR_LIT>' ) <EOL> hidden = tf . nn . tanh ( tf . matmul ( x , w_hidden ) + b_hidden ) <EOL> w_out = init_weights ( <EOL> [ num_hidden , NUM_LABELS ] , <EOL> '<STR_LIT>' , <EOL> xavier_params = ( num_hidden , NUM_LABELS ) ) <EOL> b_out = init_weights ( [ <NUM_LIT:1> , NUM_LABELS ] , '<STR_LIT>' ) <EOL> y = tf . nn . softmax ( tf . matmul ( hidden , w_out ) + b_out ) <EOL> cross_entropy = - tf . reduce_sum ( y_ * tf . log ( y ) ) <EOL> train_step = tf . train . GradientDescentOptimizer ( <NUM_LIT> ) . minimize ( cross_entropy ) <EOL> correct_prediction = tf . equal ( tf . argmax ( y , <NUM_LIT:1> ) , tf . argmax ( y_ , <NUM_LIT:1> ) ) <EOL> accuracy = tf . reduce_mean ( tf . cast ( correct_prediction , "<STR_LIT:float>" ) ) <EOL> with tf . Session ( ) as s : <EOL> tf . initialize_all_variables ( ) . run ( ) <EOL> if verbose : <EOL> print '<STR_LIT>' <EOL> print <EOL> print '<STR_LIT>' <EOL> for step in xrange ( num_epochs * train_size // BATCH_SIZE ) : <EOL> if verbose : <EOL> print step , <EOL> offset = ( step * BATCH_SIZE ) % train_size <EOL> batch_data = train_data [ offset : ( offset + BATCH_SIZE ) , : ] <EOL> batch_labels = train_labels [ offset : ( offset + BATCH_SIZE ) ] <EOL> train_step . run ( feed_dict = { x : batch_data , y_ : batch_labels } ) <EOL> if verbose and offset >= train_size - BATCH_SIZE : <EOL> print <EOL> print "<STR_LIT>" , accuracy . eval ( feed_dict = { x : test_data , y_ : test_labels } ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> tf . app . run ( ) </s>
