<s> import cPickle <EOL> import gzip <EOL> import os <EOL> import sys <EOL> import time <EOL> import collections <EOL> import numpy <EOL> import json <EOL> import theano <EOL> import theano . tensor as T <EOL> from theano . tensor . signal import downsample <EOL> from theano . tensor . nnet import conv <EOL> from layers . logistic_sgd import LogisticRegression <EOL> from layers . mlp import HiddenLayer <EOL> from layers . conv import ConvLayer , ConvLayerForward <EOL> from dnn import DNN <EOL> from cnn import CNN <EOL> class CNN_SAT ( object ) : <EOL> def __init__ ( self , numpy_rng , theano_rng = None , cfg_si = None , cfg_adapt = None , testing = False ) : <EOL> self . x = T . matrix ( '<STR_LIT:x>' ) <EOL> self . y = T . ivector ( '<STR_LIT:y>' ) <EOL> cnn_input_shape = cfg_si . conv_layer_configs [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> self . feat_dim = cnn_input_shape [ - <NUM_LIT:1> ] * cnn_input_shape [ - <NUM_LIT:2> ] * cnn_input_shape [ - <NUM_LIT:3> ] <EOL> self . ivec_dim = cfg_adapt . n_ins <EOL> self . iv = self . x [ : , self . feat_dim : self . feat_dim + self . ivec_dim ] <EOL> self . feat = self . x [ : , <NUM_LIT:0> : self . feat_dim ] <EOL> self . params = [ ] <EOL> self . delta_params = [ ] <EOL> dnn_adapt = DNN ( numpy_rng = numpy_rng , theano_rng = theano_rng , cfg = cfg_adapt , input = self . iv ) <EOL> self . dnn_adapt = dnn_adapt <EOL> linear_func = lambda x : x <EOL> adapt_output_layer = HiddenLayer ( rng = numpy_rng , <EOL> input = dnn_adapt . layers [ - <NUM_LIT:1> ] . output , <EOL> n_in = cfg_adapt . hidden_layers_sizes [ - <NUM_LIT:1> ] , <EOL> n_out = self . feat_dim , <EOL> activation = linear_func ) <EOL> dnn_adapt . layers . append ( adapt_output_layer ) <EOL> dnn_adapt . params . extend ( adapt_output_layer . params ) <EOL> dnn_adapt . delta_params . extend ( adapt_output_layer . delta_params ) <EOL> cnn_si = CNN ( numpy_rng = numpy_rng , theano_rng = theano_rng , cfg = cfg_si , input = self . feat + dnn_adapt . layers [ - <NUM_LIT:1> ] . output ) <EOL> self . cnn_si = cnn_si <EOL> self . finetune_cost = cnn_si . fc_dnn . logLayer . negative_log_likelihood ( self . y ) <EOL> self . errors = cnn_si . fc_dnn . logLayer . errors ( self . y ) <EOL> def build_finetune_functions ( self , train_shared_xy , valid_shared_xy , batch_size ) : <EOL> ( train_set_x , train_set_y ) = train_shared_xy <EOL> ( valid_set_x , valid_set_y ) = valid_shared_xy <EOL> index = T . lscalar ( '<STR_LIT:index>' ) <EOL> learning_rate = T . fscalar ( '<STR_LIT>' ) <EOL> momentum = T . fscalar ( '<STR_LIT>' ) <EOL> gparams = T . grad ( self . finetune_cost , self . params ) <EOL> updates = collections . OrderedDict ( ) <EOL> for dparam , gparam in zip ( self . delta_params , gparams ) : <EOL> updates [ dparam ] = momentum * dparam - gparam * learning_rate <EOL> for dparam , param in zip ( self . delta_params , self . params ) : <EOL> updates [ param ] = param + updates [ dparam ] <EOL> train_fn = theano . function ( inputs = [ index , theano . Param ( learning_rate , default = <NUM_LIT> ) , <EOL> theano . Param ( momentum , default = <NUM_LIT:0.5> ) ] , <EOL> outputs = self . errors , <EOL> updates = updates , <EOL> givens = { <EOL> self . x : train_set_x [ index * batch_size : <EOL> ( index + <NUM_LIT:1> ) * batch_size ] , <EOL> self . y : train_set_y [ index * batch_size : <EOL> ( index + <NUM_LIT:1> ) * batch_size ] } ) <EOL> valid_fn = theano . function ( inputs = [ index ] , <EOL> outputs = self . errors , <EOL> givens = { <EOL> self . x : valid_set_x [ index * batch_size : <EOL> ( index + <NUM_LIT:1> ) * batch_size ] , <EOL> self . y : valid_set_y [ index * batch_size : <EOL> ( index + <NUM_LIT:1> ) * batch_size ] } ) <EOL> return train_fn , valid_fn </s>
