Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import theano
- import theano.tensor as T
- from theano.tensor.signal import pool
- from theano.tensor.nnet import conv2d, relu
- from utils import eprint
- class LeNetConvPoolLayer(object):
- """Pool Layer of a convolutional network """
- def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):
- """
- Allocate a LeNetConvPoolLayer with shared variable internal parameters.
- :type rng: numpy.random.RandomState
- :param rng: a random number generator used to initialize weights
- :type input: theano.tensor.dtensor4
- :param input: symbolic image tensor, of shape image_shape
- :type filter_shape: tuple or list of length 4
- :param filter_shape: (number of filters, num input feature maps,
- filter height, filter width)
- :type image_shape: tuple or list of length 4
- :param image_shape: (batch size, num input feature maps,
- image height, image width)
- :type poolsize: tuple or list of length 2
- :param poolsize: the downsampling (pooling) factor (#rows, #cols)
- """
- assert image_shape[1] == filter_shape[1]
- self.input = input
- fan_in = np.prod(filter_shape[1:])
- fan_out = (filter_shape[0] * np.prod(filter_shape[2:]) //
- np.prod(poolsize))
- W_bound = np.sqrt(6. / (fan_in + fan_out))
- self.W = theano.shared(
- np.asarray(
- np.random.uniform(low=-W_bound, high=W_bound, size=filter_shape),
- dtype=theano.config.floatX # @UndefinedVariable
- ),
- borrow=True
- )
- b_values = np.zeros((filter_shape[0],), dtype=theano.config.floatX) # @UndefinedVariable
- self.b = theano.shared(value=b_values, borrow=True)
- conv_out = conv2d(
- input=self.input,
- filters=self.W,
- filter_shape=filter_shape,
- input_shape=image_shape
- )
- # pool each feature map individually, using maxpooling
- pooled_out = pool.pool_2d(
- input=conv_out,
- ds=poolsize,
- ignore_border=True
- )
- self.output = relu(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
- self.params = [self.W, self.b]
- self.input = input
- self.dim0, self.dim1 = self.compute_dims(image_shape[2:],
- filter_shape[2:], poolsize)
- return
- def compute_dims(self, input_shape, filter_shape, pool_size=None,
- padding='valid'):
- if padding == 'valid':
- dim0 = input_shape[0] - filter_shape[0] + 1
- dim1 = input_shape[1] - filter_shape[1] + 1
- if pool_size is not None:
- if dim0%pool_size[0] != 0. or dim1%pool_size[1] != 0.:
- eprint('Conv2D output dimensions not compatible with pooling size!')
- eprint('(%d, %d) -> (%d, %d)'
- %(dim0,dim1,pool_size[0], pool_size[1]))
- fin_dim0 = dim0/pool_size[0]
- fin_dim1 = dim1/pool_size[1]
- else:
- fin_dim0 = dim0
- fin_dim1 = dim1
- else:
- fin_dim0 = input_shape[0]
- fin_dim1 = input_shape[1]
- return int(fin_dim0), int(fin_dim1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement