def test_make_sparse_random_conv2D(self): """ Make random sparse filters, count whether the number of non-zero elements is sensible """ axes = ('c', 0, 1, 'b') input_space = Conv2DSpace((3, 3), 16, axes=axes) output_space = Conv2DSpace((3, 3), 16, axes=axes) num_nonzero = 2 kernel_shape = (2, 2) conv2d = make_sparse_random_conv2D(num_nonzero, input_space, output_space, kernel_shape) f = theano.function([self.image_tensor], conv2d.lmul(self.image_tensor)) assert f(self.image).shape == (16, 2, 2, 1) assert conv2d.output_axes == axes assert numpy.count_nonzero(conv2d._filters.get_value()) >= 32
def set_input_space(self, space): self.input_space = space if not isinstance(space, Conv2DSpace): raise BadInputSpaceError("ConvRectifiedLinear.set_input_space " "expected a Conv2DSpace, got " + str(space) + " of type " + str(type(space))) rng = self.mlp.rng if self.border_mode == 'valid': output_shape = [(self.input_space.shape[0]-self.kernel_shape[0]) / self.kernel_stride[0] + 1, (self.input_space.shape[1]-self.kernel_shape[1]) / self.kernel_stride[1] + 1] elif self.border_mode == 'full': output_shape = [(self.input_space.shape[0]+self.kernel_shape[0]) / self.kernel_stride[0] - 1, (self.input_space.shape[1]+self.kernel_shape[1]) / self.kernel_stride[1] - 1] self.detector_space = Conv2DSpace(shape=output_shape, num_channels=self.output_channels, axes=('c', 0, 1, 'b')) if self.irange is not None: assert self.sparse_init is None self.transformer = conv2d.make_random_conv2D( irange=self.irange, input_channels=self.input_space.num_channels, input_axes=self.input_space.axes, output_axes=self.detector_space.axes, output_channels=self.detector_space.num_channels, kernel_shape=self.kernel_shape, kernel_stride=self.kernel_stride, rng=rng) elif self.sparse_init is not None: self.transformer = conv2d.make_sparse_random_conv2D( num_nonzero=self.sparse_init, input_space=self.input_space, output_space=self.detector_space, kernel_shape=self.kernel_shape, batch_size=self.mlp.batch_size, subsample=self.kernel_stride, border_mode=self.border_mode, rng=rng) W, = self.transformer.get_params() W.name = 'W' self.b = sharedX(self.detector_space.get_origin() + self.init_bias) self.b.name = 'b' print 'Input shape: ', self.input_space.shape print 'Detector space: ', self.detector_space.shape assert self.pool_type in ['max', 'mean'] dummy_batch_size = self.mlp.batch_size if dummy_batch_size is None: dummy_batch_size = 2 dummy_detector = sharedX( self.detector_space.get_origin_batch(dummy_batch_size)) if self.pool_type == 'max': dummy_p = max_pool_c01b(c01b=dummy_detector, pool_shape=self.pool_shape, pool_stride=self.pool_stride, image_shape=self.detector_space.shape) elif self.pool_type == 'mean': dummy_p = mean_pool(bc01=dummy_detector, pool_shape=self.pool_shape, pool_stride=self.pool_stride, image_shape=self.detector_space.shape) dummy_p = dummy_p.eval() self.output_space = Conv2DSpace(shape=[dummy_p.shape[2], dummy_p.shape[3]], num_channels=self.output_channels, axes=('c', 0, 1, 'b')) print 'Output space: ', self.output_space.shape
def set_input_space(self, space): self.input_space = space if not isinstance(space, Conv2DSpace): raise BadInputSpaceError("ConvRectifiedLinear.set_input_space " "expected a Conv2DSpace, got " + str(space) + " of type " + str(type(space))) rng = self.mlp.rng if self.border_mode == 'valid': output_shape = [ (self.input_space.shape[0] - self.kernel_shape[0]) / self.kernel_stride[0] + 1, (self.input_space.shape[1] - self.kernel_shape[1]) / self.kernel_stride[1] + 1 ] elif self.border_mode == 'full': output_shape = [ (self.input_space.shape[0] + self.kernel_shape[0]) / self.kernel_stride[0] - 1, (self.input_space.shape[1] + self.kernel_shape[1]) / self.kernel_stride[1] - 1 ] self.detector_space = Conv2DSpace(shape=output_shape, num_channels=self.output_channels, axes=('c', 0, 1, 'b')) if self.irange is not None: assert self.sparse_init is None self.transformer = conv2d.make_random_conv2D( irange=self.irange, input_channels=self.input_space.num_channels, input_axes=self.input_space.axes, output_axes=self.detector_space.axes, output_channels=self.detector_space.num_channels, kernel_shape=self.kernel_shape, kernel_stride=self.kernel_stride, rng=rng) elif self.sparse_init is not None: self.transformer = conv2d.make_sparse_random_conv2D( num_nonzero=self.sparse_init, input_space=self.input_space, output_space=self.detector_space, kernel_shape=self.kernel_shape, batch_size=self.mlp.batch_size, subsample=self.kernel_stride, border_mode=self.border_mode, rng=rng) W, = self.transformer.get_params() W.name = 'W' self.b = sharedX(self.detector_space.get_origin() + self.init_bias) self.b.name = 'b' print 'Input shape: ', self.input_space.shape print 'Detector space: ', self.detector_space.shape assert self.pool_type in ['max', 'mean'] dummy_batch_size = self.mlp.batch_size if dummy_batch_size is None: dummy_batch_size = 2 dummy_detector = sharedX( self.detector_space.get_origin_batch(dummy_batch_size)) if self.pool_type == 'max': dummy_p = max_pool_c01b(c01b=dummy_detector, pool_shape=self.pool_shape, pool_stride=self.pool_stride, image_shape=self.detector_space.shape) elif self.pool_type == 'mean': dummy_p = mean_pool(bc01=dummy_detector, pool_shape=self.pool_shape, pool_stride=self.pool_stride, image_shape=self.detector_space.shape) dummy_p = dummy_p.eval() self.output_space = Conv2DSpace( shape=[dummy_p.shape[2], dummy_p.shape[3]], num_channels=self.output_channels, axes=('c', 0, 1, 'b')) print 'Output space: ', self.output_space.shape