Pool Layer of a convolutional network
| 40 | |
| 41 | |
| 42 | class LeNetConvPoolLayer(object): |
| 43 | """Pool Layer of a convolutional network """ |
| 44 | |
| 45 | def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)): |
| 46 | """ |
| 47 | Allocate a LeNetConvPoolLayer with shared variable internal parameters. |
| 48 | |
| 49 | :type rng: numpy.random.RandomState |
| 50 | :param rng: a random number generator used to initialize weights |
| 51 | |
| 52 | :type input: theano.tensor.dtensor4 |
| 53 | :param input: symbolic image tensor, of shape image_shape |
| 54 | |
| 55 | :type filter_shape: tuple or list of length 4 |
| 56 | :param filter_shape: (number of filters, num input feature maps, |
| 57 | filter height, filter width) |
| 58 | |
| 59 | :type image_shape: tuple or list of length 4 |
| 60 | :param image_shape: (batch size, num input feature maps, |
| 61 | image height, image width) |
| 62 | |
| 63 | :type poolsize: tuple or list of length 2 |
| 64 | :param poolsize: the downsampling (pooling) factor (#rows, #cols) |
| 65 | """ |
| 66 | |
| 67 | assert image_shape[1] == filter_shape[1] |
| 68 | self.input = input |
| 69 | |
| 70 | # there are "num input feature maps * filter height * filter width" |
| 71 | # inputs to each hidden unit |
| 72 | fan_in = numpy.prod(filter_shape[1:]) |
| 73 | # each unit in the lower layer receives a gradient from: |
| 74 | # "num output feature maps * filter height * filter width" / |
| 75 | # pooling size |
| 76 | fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) // |
| 77 | numpy.prod(poolsize)) |
| 78 | # initialize weights with random weights |
| 79 | W_bound = numpy.sqrt(6. / (fan_in + fan_out)) |
| 80 | self.W = theano.shared( |
| 81 | numpy.asarray( |
| 82 | rng.uniform(low=-W_bound, high=W_bound, size=filter_shape), |
| 83 | dtype=theano.config.floatX |
| 84 | ), |
| 85 | borrow=True |
| 86 | ) |
| 87 | |
| 88 | # the bias is a 1D tensor -- one bias per output feature map |
| 89 | b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX) |
| 90 | self.b = theano.shared(value=b_values, borrow=True) |
| 91 | |
| 92 | # convolve input feature maps with filters |
| 93 | conv_out = conv2d( |
| 94 | input=input, |
| 95 | filters=self.W, |
| 96 | filter_shape=filter_shape, |
| 97 | input_shape=image_shape |
| 98 | ) |
| 99 |