<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 . signal import downsample <EOL> from theano . tensor . nnet import conv <EOL> from theano . sandbox . cuda . basic_ops import gpu_contiguous <EOL> from pylearn2 . sandbox . cuda_convnet . filter_acts import FilterActs <EOL> from pylearn2 . sandbox . cuda_convnet . pool import MaxPool <EOL> class ConvLayer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , numpy_rng = None , input = None , is_input_layer = False , <EOL> input_shape = ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> ) , filter_shape = ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:5> ) , <EOL> poolsize = ( <NUM_LIT:1> , <NUM_LIT:1> ) , activation = T . tanh , <EOL> flatten = False , border_mode = '<STR_LIT>' , <EOL> non_maximum_erasing = False , W = None , b = None ) : <EOL> assert input_shape [ <NUM_LIT:1> ] == filter_shape [ <NUM_LIT:1> ] <EOL> if is_input_layer : <EOL> self . input = input . reshape ( input_shape ) . dimshuffle ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:0> ) <EOL> else : <EOL> self . input = input <EOL> input_shape = ( input_shape [ <NUM_LIT:1> ] , input_shape [ <NUM_LIT:2> ] , input_shape [ <NUM_LIT:3> ] , input_shape [ <NUM_LIT:0> ] ) <EOL> filter_shape = ( filter_shape [ <NUM_LIT:1> ] , filter_shape [ <NUM_LIT:2> ] , filter_shape [ <NUM_LIT:3> ] , filter_shape [ <NUM_LIT:0> ] ) <EOL> self . input_shape = input_shape <EOL> self . filter_shape = filter_shape <EOL> self . poolsize = poolsize <EOL> self . activation = activation <EOL> self . flatten = flatten <EOL> fan_in = numpy . prod ( filter_shape [ : <NUM_LIT:3> ] ) <EOL> fan_out = ( filter_shape [ <NUM_LIT:3> ] * numpy . prod ( filter_shape [ <NUM_LIT:1> : <NUM_LIT:3> ] ) / <EOL> numpy . prod ( poolsize ) ) <EOL> if W is None : <EOL> W_bound = numpy . sqrt ( <NUM_LIT> / ( fan_in + fan_out ) ) <EOL> initial_W = numpy . asarray ( numpy_rng . uniform ( <EOL> low = - W_bound , high = W_bound , <EOL> size = filter_shape ) , <EOL> dtype = theano . config . floatX ) <EOL> if activation == T . nnet . sigmoid : <EOL> initial_W *= <NUM_LIT:4> <EOL> W = theano . shared ( value = initial_W , name = '<STR_LIT>' ) <EOL> self . W = W <EOL> if b is None : <EOL> b_values = numpy . zeros ( ( filter_shape [ <NUM_LIT:3> ] , ) , dtype = theano . config . floatX ) <EOL> b = theano . shared ( value = b_values , name = '<STR_LIT:b>' ) <EOL> self . b = b <EOL> self . delta_W = theano . shared ( value = numpy . zeros ( filter_shape , <EOL> dtype = theano . config . floatX ) , name = '<STR_LIT>' ) <EOL> self . delta_b = theano . shared ( value = numpy . zeros_like ( self . b . get_value ( borrow = True ) , <EOL> dtype = theano . config . floatX ) , name = '<STR_LIT>' ) <EOL> conv_op = FilterActs ( ) <EOL> contiguous_input = gpu_contiguous ( self . input ) <EOL> contiguous_filters = gpu_contiguous ( self . W ) <EOL> conv_out = conv_op ( contiguous_input , contiguous_filters ) <EOL> y_out = activation ( conv_out + self . b . dimshuffle ( <NUM_LIT:0> , '<STR_LIT:x>' , '<STR_LIT:x>' , '<STR_LIT:x>' ) ) <EOL> pool_op = MaxPool ( ds = poolsize [ <NUM_LIT:0> ] , stride = poolsize [ <NUM_LIT:0> ] ) <EOL> pooled_out = pool_op ( y_out ) <EOL> if non_maximum_erasing : <EOL> ds = tuple ( poolsize ) <EOL> po = pooled_out . repeat ( ds [ <NUM_LIT:0> ] , axis = <NUM_LIT:2> ) . repeat ( ds [ <NUM_LIT:1> ] , axis = <NUM_LIT:3> ) <EOL> self . output = T . eq ( y_out , po ) * y_out <EOL> else : <EOL> self . output = pooled_out <EOL> if flatten : <EOL> self . output = self . output . dimshuffle ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> self . output = self . output . flatten ( <NUM_LIT:2> ) <EOL> self . params = [ self . W , self . b ] <EOL> self . delta_params = [ self . delta_W , self . delta_b ] </s>
