<s> import cPickle <EOL> import gzip <EOL> import os <EOL> import sys <EOL> import time <EOL> import numpy <EOL> import theano <EOL> import theano . tensor as T <EOL> from theano . tensor . shared_randomstreams import RandomStreams <EOL> from layers . logistic_sgd import LogisticRegression <EOL> from layers . mlp import HiddenLayer <EOL> from layers . da import dA , dA_maxout <EOL> class SdA ( object ) : <EOL> def __init__ ( self , numpy_rng , theano_rng = None , cfg = None , dnn = None ) : <EOL> """<STR_LIT>""" <EOL> self . cfg = cfg <EOL> self . hidden_layers_sizes = cfg . hidden_layers_sizes <EOL> self . n_ins = cfg . n_ins <EOL> self . hidden_layers_number = len ( self . hidden_layers_sizes ) <EOL> self . dA_layers = [ ] <EOL> if not theano_rng : <EOL> theano_rng = RandomStreams ( numpy_rng . randint ( <NUM_LIT:2> ** <NUM_LIT:30> ) ) <EOL> self . x = dnn . x <EOL> for i in xrange ( self . hidden_layers_number ) : <EOL> if i == <NUM_LIT:0> : <EOL> input_size = self . n_ins <EOL> layer_input = self . x <EOL> else : <EOL> input_size = self . hidden_layers_sizes [ i - <NUM_LIT:1> ] <EOL> layer_input = dnn . layers [ i - <NUM_LIT:1> ] . output <EOL> if i == <NUM_LIT:0> : <EOL> reconstruct_activation = cfg . firstlayer_reconstruct_activation <EOL> else : <EOL> reconstruct_activation = cfg . hidden_activation <EOL> dA_layer = dA ( numpy_rng = numpy_rng , <EOL> theano_rng = theano_rng , <EOL> input = layer_input , <EOL> n_visible = input_size , <EOL> n_hidden = self . hidden_layers_sizes [ i ] , <EOL> W = dnn . layers [ i ] . W , <EOL> bhid = dnn . layers [ i ] . b , <EOL> sparsity = cfg . sparsity , <EOL> sparsity_weight = cfg . sparsity_weight , <EOL> hidden_activation = cfg . hidden_activation , <EOL> reconstruct_activation = reconstruct_activation ) <EOL> self . dA_layers . append ( dA_layer ) <EOL> def pretraining_functions ( self , train_set_x , batch_size ) : <EOL> index = T . lscalar ( '<STR_LIT:index>' ) <EOL> corruption_level = T . scalar ( '<STR_LIT>' ) <EOL> learning_rate = T . scalar ( '<STR_LIT>' ) <EOL> momentum = T . scalar ( '<STR_LIT>' ) <EOL> n_batches = train_set_x . get_value ( borrow = True ) . shape [ <NUM_LIT:0> ] / batch_size <EOL> batch_begin = index * batch_size <EOL> batch_end = batch_begin + batch_size <EOL> pretrain_fns = [ ] <EOL> for dA in self . dA_layers : <EOL> cost , updates = dA . get_cost_updates ( corruption_level , learning_rate , momentum ) <EOL> fn = theano . function ( inputs = [ index , <EOL> theano . Param ( corruption_level , default = <NUM_LIT> ) , <EOL> theano . Param ( learning_rate , default = <NUM_LIT:0.1> ) , <EOL> theano . Param ( momentum , default = <NUM_LIT:0.5> ) ] , <EOL> outputs = cost , <EOL> updates = updates , <EOL> givens = { self . x : train_set_x [ batch_begin : batch_end ] } ) <EOL> pretrain_fns . append ( fn ) <EOL> return pretrain_fns <EOL> class SdA_maxout ( object ) : <EOL> def __init__ ( self , numpy_rng , theano_rng = None , n_ins = <NUM_LIT> , <EOL> hidden_layers_sizes = [ <NUM_LIT> , <NUM_LIT> ] , n_outs = <NUM_LIT:10> , <EOL> corruption_levels = [ <NUM_LIT:0.1> , <NUM_LIT:0.1> ] , pool_size = <NUM_LIT:3> , <EOL> sparsity = None , sparsity_weight = None , <EOL> first_reconstruct_activation = T . tanh ) : <EOL> self . sigmoid_layers = [ ] <EOL> self . dA_layers = [ ] <EOL> self . params = [ ] <EOL> self . n_layers = len ( hidden_layers_sizes ) <EOL> assert self . n_layers > <NUM_LIT:0> <EOL> if not theano_rng : <EOL> theano_rng = RandomStreams ( numpy_rng . randint ( <NUM_LIT:2> ** <NUM_LIT:30> ) ) <EOL> self . x = T . matrix ( '<STR_LIT:x>' ) <EOL> self . y = T . ivector ( '<STR_LIT:y>' ) <EOL> for i in xrange ( self . n_layers ) : <EOL> if i == <NUM_LIT:0> : <EOL> input_size = n_ins <EOL> else : <EOL> input_size = hidden_layers_sizes [ i - <NUM_LIT:1> ] <EOL> if i == <NUM_LIT:0> : <EOL> layer_input = self . x <EOL> else : <EOL> layer_input = self . sigmoid_layers [ - <NUM_LIT:1> ] . output <EOL> sigmoid_layer = HiddenLayer ( rng = numpy_rng , <EOL> input = layer_input , <EOL> n_in = input_size , <EOL> n_out = hidden_layers_sizes [ i ] * pool_size , <EOL> activation = ( lambda x : <NUM_LIT:1.0> * x ) , <EOL> do_maxout = True , pool_size = pool_size ) <EOL> self . sigmoid_layers . append ( sigmoid_layer ) <EOL> self . params . extend ( sigmoid_layer . params ) <EOL> if i == <NUM_LIT:0> : <EOL> reconstruct_activation = first_reconstruct_activation <EOL> else : <EOL> reconstruct_activation = ( lambda x : <NUM_LIT:1.0> * x ) <EOL> dA_layer = dA_maxout ( numpy_rng = numpy_rng , <EOL> theano_rng = theano_rng , <EOL> input = layer_input , <EOL> n_visible = input_size , <EOL> n_hidden = hidden_layers_sizes [ i ] * pool_size , <EOL> W = sigmoid_layer . W , <EOL> bhid = sigmoid_layer . b , <EOL> sparsity = sparsity , <EOL> sparsity_weight = sparsity_weight , <EOL> pool_size = pool_size , <EOL> reconstruct_activation = reconstruct_activation ) <EOL> self . dA_layers . append ( dA_layer ) <EOL> self . logLayer = LogisticRegression ( <EOL> input = self . sigmoid_layers [ - <NUM_LIT:1> ] . output , <EOL> n_in = hidden_layers_sizes [ - <NUM_LIT:1> ] , n_out = n_outs ) <EOL> self . sigmoid_layers . append ( self . logLayer ) <EOL> self . params . extend ( self . logLayer . params ) <EOL> def pretraining_functions ( self , train_set_x , batch_size ) : <EOL> index = T . lscalar ( '<STR_LIT:index>' ) <EOL> corruption_level = T . scalar ( '<STR_LIT>' ) <EOL> learning_rate = T . scalar ( '<STR_LIT>' ) <EOL> n_batches = train_set_x . get_value ( borrow = True ) . shape [ <NUM_LIT:0> ] / batch_size <EOL> batch_begin = index * batch_size <EOL> batch_end = batch_begin + batch_size <EOL> pretrain_fns = [ ] <EOL> for dA in self . dA_layers : <EOL> cost , updates = dA . get_cost_updates ( corruption_level , <EOL> learning_rate ) <EOL> fn = theano . function ( inputs = [ index , <EOL> theano . Param ( corruption_level , default = <NUM_LIT> ) , <EOL> theano . Param ( learning_rate , default = <NUM_LIT:0.1> ) ] , <EOL> outputs = cost , <EOL> updates = updates , <EOL> givens = { self . x : train_set_x [ batch_begin : <EOL> batch_end ] } ) <EOL> pretrain_fns . append ( fn ) <EOL> return pretrain_fns </s>
