<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import os <EOL> import sys <EOL> import timeit <EOL> import numpy <EOL> import theano <EOL> import theano . tensor as T <EOL> from theano . tensor . signal import downsample <EOL> from theano . tensor . nnet import conv2d <EOL> from logistic_sgd import LogisticRegression , load_data <EOL> from mlp import HiddenLayer <EOL> class LeNetConvPoolLayer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rng , input , filter_shape , image_shape , poolsize = ( <NUM_LIT:2> , <NUM_LIT:2> ) ) : <EOL> """<STR_LIT>""" <EOL> assert image_shape [ <NUM_LIT:1> ] == filter_shape [ <NUM_LIT:1> ] <EOL> self . input = input <EOL> fan_in = numpy . prod ( filter_shape [ <NUM_LIT:1> : ] ) <EOL> fan_out = ( filter_shape [ <NUM_LIT:0> ] * numpy . prod ( filter_shape [ <NUM_LIT:2> : ] ) // <EOL> numpy . prod ( poolsize ) ) <EOL> W_bound = numpy . sqrt ( <NUM_LIT> / ( fan_in + fan_out ) ) <EOL> self . W = theano . shared ( <EOL> numpy . asarray ( <EOL> rng . uniform ( low = - W_bound , high = W_bound , size = filter_shape ) , <EOL> dtype = theano . config . floatX <EOL> ) , <EOL> borrow = True <EOL> ) <EOL> b_values = numpy . zeros ( ( filter_shape [ <NUM_LIT:0> ] , ) , dtype = theano . config . floatX ) <EOL> self . b = theano . shared ( value = b_values , borrow = True ) <EOL> conv_out = conv2d ( <EOL> input = input , <EOL> filters = self . W , <EOL> filter_shape = filter_shape , <EOL> input_shape = image_shape <EOL> ) <EOL> pooled_out = downsample . max_pool_2d ( <EOL> input = conv_out , <EOL> ds = poolsize , <EOL> ignore_border = True <EOL> ) <EOL> self . output = T . tanh ( pooled_out + self . b . dimshuffle ( '<STR_LIT:x>' , <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' ) ) <EOL> self . params = [ self . W , self . b ] <EOL> self . input = input <EOL> def evaluate_lenet5 ( learning_rate = <NUM_LIT:0.1> , n_epochs = <NUM_LIT:200> , <EOL> dataset = '<STR_LIT>' , <EOL> nkerns = [ <NUM_LIT:20> , <NUM_LIT:50> ] , batch_size = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> datasets = load_data ( dataset ) <EOL> train_set_x , train_set_y = datasets [ <NUM_LIT:0> ] <EOL> valid_set_x , valid_set_y = datasets [ <NUM_LIT:1> ] <EOL> test_set_x , test_set_y = datasets [ <NUM_LIT:2> ] <EOL> n_train_batches = train_set_x . get_value ( borrow = True ) . shape [ <NUM_LIT:0> ] <EOL> n_valid_batches = valid_set_x . get_value ( borrow = True ) . shape [ <NUM_LIT:0> ] <EOL> n_test_batches = test_set_x . get_value ( borrow = True ) . shape [ <NUM_LIT:0> ] <EOL> n_train_batches //= batch_size <EOL> n_valid_batches //= batch_size <EOL> n_test_batches //= batch_size <EOL> index = T . lscalar ( ) <EOL> x = T . matrix ( '<STR_LIT:x>' ) <EOL> y = T . ivector ( '<STR_LIT:y>' ) <EOL> print ( '<STR_LIT>' ) <EOL> layer0_input = x . reshape ( ( batch_size , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> layer0 = LeNetConvPoolLayer ( <EOL> rng , <EOL> input = layer0_input , <EOL> image_shape = ( batch_size , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> ) , <EOL> filter_shape = ( nkerns [ <NUM_LIT:0> ] , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:5> ) , <EOL> poolsize = ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> ) <EOL> layer1 = LeNetConvPoolLayer ( <EOL> rng , <EOL> input = layer0 . output , <EOL> image_shape = ( batch_size , nkerns [ <NUM_LIT:0> ] , <NUM_LIT:12> , <NUM_LIT:12> ) , <EOL> filter_shape = ( nkerns [ <NUM_LIT:1> ] , nkerns [ <NUM_LIT:0> ] , <NUM_LIT:5> , <NUM_LIT:5> ) , <EOL> poolsize = ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> ) <EOL> layer2_input = layer1 . output . flatten ( <NUM_LIT:2> ) <EOL> layer2 = HiddenLayer ( <EOL> rng , <EOL> input = layer2_input , <EOL> n_in = nkerns [ <NUM_LIT:1> ] * <NUM_LIT:4> * <NUM_LIT:4> , <EOL> n_out = <NUM_LIT> , <EOL> activation = T . tanh <EOL> ) <EOL> layer3 = LogisticRegression ( input = layer2 . output , n_in = <NUM_LIT> , n_out = <NUM_LIT:10> ) <EOL> cost = layer3 . negative_log_likelihood ( y ) <EOL> test_model = theano . function ( <EOL> [ index ] , <EOL> layer3 . errors ( y ) , <EOL> givens = { <EOL> x : test_set_x [ index * batch_size : ( index + <NUM_LIT:1> ) * batch_size ] , <EOL> y : test_set_y [ index * batch_size : ( index + <NUM_LIT:1> ) * batch_size ] <EOL> } <EOL> ) <EOL> validate_model = theano . function ( <EOL> [ index ] , <EOL> layer3 . errors ( y ) , <EOL> givens = { <EOL> x : valid_set_x [ index * batch_size : ( index + <NUM_LIT:1> ) * batch_size ] , <EOL> y : valid_set_y [ index * batch_size : ( index + <NUM_LIT:1> ) * batch_size ] <EOL> } <EOL> ) <EOL> params = layer3 . params + layer2 . params + layer1 . params + layer0 . params <EOL> grads = T . grad ( cost , params ) <EOL> updates = [ <EOL> ( param_i , param_i - learning_rate * grad_i ) <EOL> for param_i , grad_i in zip ( params , grads ) <EOL> ] <EOL> train_model = theano . function ( <EOL> [ index ] , <EOL> cost , <EOL> updates = updates , <EOL> givens = { <EOL> x : train_set_x [ index * batch_size : ( index + <NUM_LIT:1> ) * batch_size ] , <EOL> y : train_set_y [ index * batch_size : ( index + <NUM_LIT:1> ) * batch_size ] <EOL> } <EOL> ) <EOL> print ( '<STR_LIT>' ) <EOL> patience = <NUM_LIT> <EOL> patience_increase = <NUM_LIT:2> <EOL> improvement_threshold = <NUM_LIT> <EOL> validation_frequency = min ( n_train_batches , patience // <NUM_LIT:2> ) <EOL> best_validation_loss = numpy . inf <EOL> best_iter = <NUM_LIT:0> <EOL> test_score = <NUM_LIT:0.> <EOL> start_time = timeit . default_timer ( ) <EOL> epoch = <NUM_LIT:0> <EOL> done_looping = False <EOL> while ( epoch < n_epochs ) and ( not done_looping ) : <EOL> epoch = epoch + <NUM_LIT:1> <EOL> for minibatch_index in range ( n_train_batches ) : <EOL> iter = ( epoch - <NUM_LIT:1> ) * n_train_batches + minibatch_index <EOL> if iter % <NUM_LIT:100> == <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' , iter ) <EOL> cost_ij = train_model ( minibatch_index ) <EOL> if ( iter + <NUM_LIT:1> ) % validation_frequency == <NUM_LIT:0> : <EOL> validation_losses = [ validate_model ( i ) for i <EOL> in range ( n_valid_batches ) ] <EOL> this_validation_loss = numpy . mean ( validation_losses ) <EOL> print ( '<STR_LIT>' % <EOL> ( epoch , minibatch_index + <NUM_LIT:1> , n_train_batches , <EOL> this_validation_loss * <NUM_LIT> ) ) <EOL> if this_validation_loss < best_validation_loss : <EOL> if this_validation_loss < best_validation_loss * improvement_threshold : <EOL> patience = max ( patience , iter * patience_increase ) <EOL> best_validation_loss = this_validation_loss <EOL> best_iter = iter <EOL> test_losses = [ <EOL> test_model ( i ) <EOL> for i in range ( n_test_batches ) <EOL> ] <EOL> test_score = numpy . mean ( test_losses ) <EOL> print ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % <EOL> ( epoch , minibatch_index + <NUM_LIT:1> , n_train_batches , <EOL> test_score * <NUM_LIT> ) ) <EOL> if patience <= iter : <EOL> done_looping = True <EOL> break <EOL> end_time = timeit . default_timer ( ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( best_validation_loss * <NUM_LIT> , best_iter + <NUM_LIT:1> , test_score * <NUM_LIT> ) ) <EOL> print ( ( '<STR_LIT>' + <EOL> os . path . split ( __file__ ) [ <NUM_LIT:1> ] + <EOL> '<STR_LIT>' % ( ( end_time - start_time ) / <NUM_LIT> ) ) , file = sys . stderr ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> evaluate_lenet5 ( ) <EOL> def experiment ( state , channel ) : <EOL> evaluate_lenet5 ( state . learning_rate , dataset = state . dataset ) </s>
