<s> from load_data import load_color <EOL> import numpy as np <EOL> import theano <EOL> import theano . tensor as tensor <EOL> from theano . tensor . signal . downsample import max_pool_2d <EOL> def _relu ( X ) : <EOL> return X * ( X > <NUM_LIT> ) <EOL> def conv_layer ( input_variable , filter_shape , pool_shape , stride , random_state ) : <EOL> np_filters = <NUM_LIT> * ( random_state . rand ( <EOL> * filter_shape ) . astype ( theano . config . floatX ) - <NUM_LIT:0.5> ) <EOL> filters = theano . shared ( np_filters ) <EOL> np_biases = np . zeros ( filter_shape [ <NUM_LIT:0> ] ) . astype ( theano . config . floatX ) <EOL> biases = theano . shared ( np_biases ) <EOL> params = [ filters , biases ] <EOL> conv = tensor . nnet . conv2d ( input_variable , filters , subsample = stride ) <EOL> conv += biases . dimshuffle ( '<STR_LIT:x>' , <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' ) <EOL> out = _relu ( conv ) <EOL> pooled = max_pool_2d ( out , pool_shape , ignore_border = True ) <EOL> return pooled , params <EOL> def fc_layer ( input_variable , layer_shape , random_state ) : <EOL> np_W = <NUM_LIT> * ( random_state . rand ( <EOL> * layer_shape ) . astype ( theano . config . floatX ) - <NUM_LIT:0.5> ) <EOL> W = theano . shared ( np_W ) <EOL> np_b = np . zeros ( layer_shape [ <NUM_LIT:1> ] ) . astype ( theano . config . floatX ) <EOL> b = theano . shared ( np_b ) <EOL> params = [ W , b ] <EOL> out = _relu ( tensor . dot ( input_variable , W ) + b ) <EOL> return out , params <EOL> def softmax_layer ( input_variable , layer_shape , random_state ) : <EOL> np_W = <NUM_LIT> * ( random_state . rand ( <EOL> * layer_shape ) . astype ( theano . config . floatX ) - <NUM_LIT:0.5> ) <EOL> W = theano . shared ( np_W ) <EOL> np_b = np . zeros ( layer_shape [ <NUM_LIT:1> ] ) . astype ( theano . config . floatX ) <EOL> b = theano . shared ( np_b ) <EOL> out = tensor . dot ( input_variable , W ) + b <EOL> params = [ W , b ] <EOL> e = tensor . exp ( out - out . max ( axis = <NUM_LIT:1> , keepdims = True ) ) <EOL> softmax = e / e . sum ( axis = <NUM_LIT:1> , keepdims = True ) <EOL> return softmax , params <EOL> def softmax_cost ( y_hat_sym , y_sym ) : <EOL> return - tensor . mean ( <EOL> tensor . log ( y_hat_sym ) [ tensor . arange ( y_sym . shape [ <NUM_LIT:0> ] ) , y_sym ] ) <EOL> def minibatch_indices ( X , minibatch_size , lb = None , ub = None ) : <EOL> if lb is None : <EOL> lb = <NUM_LIT:0> <EOL> if ub is None : <EOL> ub = len ( X ) <EOL> minibatch_indices = np . arange ( lb , ub , minibatch_size ) <EOL> minibatch_indices = np . asarray ( list ( minibatch_indices ) + [ ub ] ) <EOL> start_indices = minibatch_indices [ : - <NUM_LIT:1> ] <EOL> end_indices = minibatch_indices [ <NUM_LIT:1> : ] <EOL> return zip ( start_indices , end_indices ) <EOL> X_t , y_t = load_color ( ) <EOL> shp = X_t . shape <EOL> X_train = X_t [ : <NUM_LIT> ] . reshape ( <NUM_LIT> , - <NUM_LIT:1> ) . astype ( '<STR_LIT>' ) <EOL> mean = X_train . mean ( axis = <NUM_LIT:0> , keepdims = True ) <EOL> std = X_train . std ( axis = <NUM_LIT:0> , keepdims = True ) <EOL> X_t = ( X_t [ : ] . reshape ( len ( X_t ) , - <NUM_LIT:1> ) - mean ) / std <EOL> X_t = X_t . reshape ( * shp ) <EOL> X = tensor . tensor4 ( '<STR_LIT:X>' ) <EOL> y = tensor . ivector ( '<STR_LIT:y>' ) <EOL> params = [ ] <EOL> random_state = np . random . RandomState ( <NUM_LIT> ) <EOL> filter_shape = ( <NUM_LIT:32> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> stride = ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> out , l_params = conv_layer ( X , filter_shape , pool_shape , stride , random_state ) <EOL> params += l_params <EOL> filter_shape = ( <NUM_LIT:64> , filter_shape [ <NUM_LIT:0> ] , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> stride = ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> out , l_params = conv_layer ( out , filter_shape , pool_shape , stride , <EOL> random_state ) <EOL> params += l_params <EOL> filter_shape = ( <NUM_LIT> , filter_shape [ <NUM_LIT:0> ] , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> pool_shape = ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> stride = ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> out , l_params = conv_layer ( out , filter_shape , pool_shape , stride , <EOL> random_state ) <EOL> params += l_params <EOL> shp = out . shape <EOL> out = out . reshape ( ( shp [ <NUM_LIT:0> ] , shp [ <NUM_LIT:1> ] * shp [ <NUM_LIT:2> ] * shp [ <NUM_LIT:3> ] ) ) <EOL> shape = ( <NUM_LIT> , <NUM_LIT> ) <EOL> out , l_params = fc_layer ( out , shape , random_state ) <EOL> params += l_params <EOL> shape = ( shape [ <NUM_LIT:1> ] , <NUM_LIT:64> ) <EOL> out , l_params = fc_layer ( out , shape , random_state ) <EOL> params += l_params <EOL> shape = ( shape [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> out , l_params = softmax_layer ( out , shape , random_state ) <EOL> params += l_params <EOL> cost = softmax_cost ( out , y ) <EOL> grads = tensor . grad ( cost , params ) <EOL> minibatch_size = <NUM_LIT:10> <EOL> learning_rate = <NUM_LIT> / minibatch_size <EOL> updates = [ ( param_i , param_i - learning_rate * grad_i ) <EOL> for param_i , grad_i in zip ( params , grads ) ] <EOL> train_function = theano . function ( [ X , y ] , cost , updates = updates ) <EOL> predict_function = theano . function ( [ X ] , out ) <EOL> epochs = <NUM_LIT:100> <EOL> for n in range ( epochs ) : <EOL> loss = [ ] <EOL> for i , j in minibatch_indices ( X_t , minibatch_size , lb = <NUM_LIT:0> , ub = <NUM_LIT> ) : <EOL> X_nt = X_t [ i : j ] <EOL> flip_idx = np . where ( random_state . rand ( len ( X_nt ) ) > <NUM_LIT:0.5> ) [ <NUM_LIT:0> ] <EOL> X_nt [ flip_idx ] = X_nt [ flip_idx ] [ : , : , : , : : - <NUM_LIT:1> ] <EOL> l = train_function ( X_nt , y_t [ i : j ] ) <EOL> loss . append ( l ) <EOL> loss = np . mean ( loss ) <EOL> train_y_pred = [ ] <EOL> for i , j in minibatch_indices ( X_t , minibatch_size , lb = <NUM_LIT:0> , ub = <NUM_LIT> ) : <EOL> train_y_hat = predict_function ( X_t [ i : j ] ) <EOL> y_p = np . argmax ( train_y_hat , axis = <NUM_LIT:1> ) <EOL> train_y_pred . extend ( list ( y_p ) ) <EOL> valid_y_pred = [ ] <EOL> for i , j in minibatch_indices ( X_t , minibatch_size , lb = <NUM_LIT> , ub = <NUM_LIT> ) : <EOL> valid_y_hat = predict_function ( X_t [ i : j ] ) <EOL> y_p = np . argmax ( valid_y_hat , axis = <NUM_LIT:1> ) <EOL> valid_y_pred . extend ( list ( y_p ) ) <EOL> train_y_pred = np . array ( train_y_pred ) <EOL> valid_y_pred = np . array ( valid_y_pred ) <EOL> print ( "<STR_LIT>" % n ) <EOL> print ( "<STR_LIT>" % np . mean ( ( y_t [ <NUM_LIT:0> : <NUM_LIT> ] . flatten ( ) == <EOL> train_y_pred . flatten ( ) ) . astype ( <EOL> "<STR_LIT>" ) ) ) <EOL> print ( "<STR_LIT>" % np . mean ( ( y_t [ <NUM_LIT> : <NUM_LIT> ] . flatten ( ) == <EOL> valid_y_pred . flatten ( ) ) . astype ( <EOL> "<STR_LIT>" ) ) ) <EOL> print ( "<STR_LIT>" % loss ) </s>
