desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'.. todo:: WRITEME'
def lmul(self, x):
reshaped = x.reshape((self.input_groups, (x.shape[0] / self.input_groups), x.shape[1], x.shape[2], x.shape[3])) out = LocalDot.rmul(self, reshaped) return out.reshape(((out.shape[0] * out.shape[1]), out.shape[2], out.shape[3], out.shape[4]))
'.. todo:: WRITEME'
def get_params(self):
return [self._filters]
'Set up a test image and filter to re-use'
def setUp(self):
skip_if_no_gpu() self.image = numpy.random.rand(16, 3, 3, 1).astype(theano.config.floatX) self.image_tensor = tensor.tensor4() self.filters_values = numpy.ones((2, 2, 16, 2, 2, 1, 16), dtype=theano.config.floatX) self.filters = sharedX(self.filters_values, name='filters') self.local = Local(self...
'Check whether the local receptive field has stored the correct filters'
def test_get_params(self):
assert (self.local.get_params() == [self.filters])
'Make sure the shape of the output is correct'
def test_lmul(self):
f = theano.function([self.image_tensor], self.local.lmul(self.image_tensor)) assert (f(self.image).shape == (16, 2, 2, 1))
'Create random local receptive fields and check whether they can be applied and give a sensible output shape'
def test_make_random_local(self):
local = make_random_local(1, 16, ('c', 0, 1, 'b'), 1, (3, 3), 16, ('c', 0, 1, 'b'), (2, 2)) f = theano.function([self.image_tensor], local.lmul(self.image_tensor)) assert (f(self.image).shape == (16, 2, 2, 1))
'Set up a test image and filter to re-use.'
def setUp(self):
skip_if_no_gpu() if (not dnn_available()): raise SkipTest('Skipping tests cause cudnn is not available') self.orig_floatX = theano.config.floatX theano.config.floatX = 'float32' self.image = np.random.rand(1, 1, 3, 3).astype(theano.config.floatX) self.image_tensor = ten...
'After test clean up.'
def tearDown(self):
theano.config.floatX = self.orig_floatX
'Check correct errors are raised when bad input is given.'
def test_value_errors(self):
with self.assertRaises(AssertionError): Cudnn2D(filters=self.filters, batch_size=(-1), input_space=self.input_space)
'Check whether the cudnn has stored the correct filters.'
def test_get_params(self):
self.assertEqual(self.cudnn2d.get_params(), [self.filters])
'Check whether the cudnn has stored the correct filters.'
def test_get_weights_topo(self):
self.assertTrue(np.all((self.cudnn2d.get_weights_topo(borrow=True) == np.transpose(self.filters.get_value(borrow=True), (0, 2, 3, 1)))))
'Use conv2D to check whether the convolution worked correctly.'
def test_lmul(self):
conv2d = Conv2D(self.filters, self.batch_size, self.input_space, output_axes=('b', 'c', 0, 1)) f_co = theano.function([self.image_tensor], conv2d.lmul(self.image_tensor)) f_cu = theano.function([self.image_tensor], self.cudnn2d.lmul(self.image_tensor)) self.assertTrue(np.allclose(f_co(self.image), f_cu(...
'Make sure that setting the batch size actually changes the property.'
def test_set_batch_size(self):
img_shape = self.cudnn2d._img_shape self.cudnn2d.set_batch_size((self.batch_size + 10)) np.testing.assert_equal(self.cudnn2d._img_shape[0], (self.batch_size + 10)) np.testing.assert_equal(self.cudnn2d._img_shape[1:], img_shape[1:])
'Test different output axes. Use different output axes and see whether the output is what we expect.'
def test_axes(self):
default_axes = ('b', 'c', 0, 1) axes = (0, 'b', 1, 'c') another_axes = (0, 1, 'c', 'b') map_to_default = tuple((axes.index(axis) for axis in default_axes)) map_to_another_axes = tuple((default_axes.index(axis) for axis in another_axes)) input_space = Conv2DSpace((3, 3), num_channels=1, axes=anot...
'Go from 2 to 3 channels and see whether the shape is correct.'
def test_channels(self):
input_space = Conv2DSpace((3, 3), num_channels=3) filters_values = np.ones((2, 3, 2, 2), dtype=theano.config.floatX) filters = sharedX(filters_values) image = np.random.rand(1, 3, 3, 3).astype(theano.config.floatX) cudnn2d = Cudnn2D(filters, 1, input_space) f = theano.function([self.image_tensor...
'Test a random convolution. Create a random convolution and check whether the shape, axes and input space are all what we expect.'
def test_make_random_conv2D(self):
output_space = Conv2DSpace((2, 2), 1) cudnn2d = make_random_conv2D(1, self.input_space, output_space, (2, 2), 1) f = theano.function([self.image_tensor], cudnn2d.lmul(self.image_tensor)) assert (f(self.image).shape == (1, 2, 2, 1)) assert (cudnn2d._input_space == self.input_space) assert (cudnn2...
'Set up a test image and filter to re-use'
def setUp(self):
self.orig_floatX = theano.config.floatX theano.config.floatX = 'float32' theano.sandbox.cuda.use('gpu') self.image = numpy.random.rand(16, 3, 3, 1).astype(theano.config.floatX) self.image_tensor = tensor.tensor4() self.filters_values = numpy.random.rand(16, 2, 2, 32).astype(theano.config.floatX)...
'Emulate c01b convolution with scipy'
def scipy_conv_c01b(self, images, filters):
assert (images.ndim == 4) assert (filters.ndim == 4) (in_chans, rows, cols, bs) = images.shape (in_chans_, rows_, cols_, out_chans) = filters.shape assert (in_chans_ == in_chans) out_bc01 = [[sum((scipy.ndimage.filters.convolve(images[c, :, :, b], filters[c, ::(-1), ::(-1), i]) for c in xrange(i...
'Check whether the conv2d has stored the correct filters'
def test_get_params(self):
assert (self.conv2d.get_params() == [self.filters])
'Use SciPy\'s ndimage to check whether the convolution worked correctly'
def test_lmul(self):
f = theano.function([self.image_tensor], self.conv2d.lmul(self.image_tensor)) if scipy_available: self.assertTrue(numpy.allclose(f(self.image), self.scipy_conv_c01b(self.image, self.filters_values)[:, :2, :2, :]))
'Check whether this function outputs the right shape'
def test_lmul_T(self):
conv2d = self.conv2d.lmul(self.image_tensor) f = theano.function([self.image_tensor], self.conv2d.lmul_T(conv2d)) assert (f(self.image).shape == self.image.shape)
'Use custom output axes and check whether it worked'
def test_axes(self):
default_axes = ('c', 0, 1, 'b') axes = (0, 'b', 1, 'c') mapping = tuple((axes.index(axis) for axis in default_axes)) conv2d = Conv2D(self.filters, output_axes=axes) f_axes = theano.function([self.image_tensor], conv2d.lmul(self.image_tensor)) f = theano.function([self.image_tensor], self.conv2d....
'Go from 32 to 16 channels and see whether that works without error'
def test_channels(self):
filters_values = numpy.ones((32, 2, 2, 16), dtype=theano.config.floatX) filters = sharedX(filters_values) image = numpy.random.rand(32, 3, 3, 1).astype(theano.config.floatX) conv2d = Conv2D(filters) f = theano.function([self.image_tensor], conv2d.lmul(self.image_tensor)) assert (f(image).shape =...
'Make random filters'
def test_make_random_conv2D(self):
default_axes = ('c', 0, 1, 'b') conv2d = make_random_conv2D(1, 16, default_axes, default_axes, 16, (2, 2)) f = theano.function([self.image_tensor], conv2d.lmul(self.image_tensor)) assert (f(self.image).shape == (16, 2, 2, 1)) assert (conv2d.output_axes == default_axes)
'Make random sparse filters, count whether the number of non-zero elements is sensible'
def test_make_sparse_random_conv2D(self):
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], c...
'Very basic test to see whether a detector layer can be set up without error. Not checking much for the actual output.'
def test_setup_detector_layer_c01b(self):
axes = ('c', 0, 1, 'b') layer = MaxoutConvC01B(16, 2, (2, 2), (2, 2), (1, 1), 'maxout', irange=1.0) input_space = Conv2DSpace((3, 3), 16, axes=axes) MLP(layers=[layer], input_space=input_space) layer.set_input_space(input_space) assert isinstance(layer.input_space, Conv2DSpace) input = thean...
'Set up a test image and filter to re-use'
def setUp(self):
self.image = numpy.random.rand(1, 3, 3, 1).astype(theano.config.floatX) self.image_tensor = tensor.tensor4() self.input_space = Conv2DSpace((3, 3), 1) self.filters_values = numpy.ones((1, 1, 2, 2), dtype=theano.config.floatX) self.filters = sharedX(self.filters_values, name='filters') self.conv2...
'Check correct errors are raised when bad input is given'
def test_value_errors(self):
bad_filters = sharedX(numpy.zeros((1, 3, 2))) self.assertRaises(ValueError, Conv2D, bad_filters, 1, self.input_space) self.assertRaises(AssertionError, Conv2D, self.filters, 0, self.input_space)
'Check whether the conv2d has stored the correct filters'
def test_get_params(self):
assert (self.conv2d.get_params() == [self.filters])
'Use SciPy\'s ndimage to check whether the convolution worked correctly'
def test_lmul(self):
f = theano.function([self.image_tensor], self.conv2d.lmul(self.image_tensor)) if scipy_available: numpy.allclose(f(self.image).reshape((2, 2)), scipy.ndimage.filters.convolve(self.image.reshape((3, 3)), self.filters_values.reshape((2, 2)))[:2, :2])
'Check whether this function outputs the right shape'
def test_lmul_T(self):
conv2d = self.conv2d.lmul(self.image_tensor) f = theano.function([self.image_tensor], self.conv2d.lmul_T(conv2d)) assert (f(self.image).shape == self.image.shape)
'Check whether this function outputs the same values as when taking the square manually'
def test_lmul_sq_T(self):
conv2d_sq = Conv2D(sharedX(numpy.square(self.filters_values)), 1, self.input_space).lmul(self.image_tensor) conv2d = self.conv2d.lmul(self.image_tensor) f = theano.function([self.image_tensor], self.conv2d.lmul_T(conv2d_sq)) f2 = theano.function([self.image_tensor], self.conv2d.lmul_sq_T(conv2d)) nu...
'Make sure that setting the batch size actually changes the property'
def test_set_batch_size(self):
cur_img_shape = self.conv2d._img_shape cur_batch_size = self.conv2d._img_shape[0] self.conv2d.set_batch_size((cur_batch_size + 10)) assert (self.conv2d._img_shape[0] == (cur_batch_size + 10)) assert (self.conv2d._img_shape[1:] == cur_img_shape[1:])
'Use different output axes and see whether the output is what we expect'
def test_axes(self):
default_axes = ('b', 0, 1, 'c') axes = (0, 'b', 1, 'c') mapping = tuple((axes.index(axis) for axis in default_axes)) input_space = Conv2DSpace((3, 3), num_channels=1, axes=axes) conv2d = Conv2D(self.filters, 1, input_space, output_axes=axes) f_axes = theano.function([self.image_tensor], conv2d.l...
'Go from 2 to 3 channels and see whether the shape is correct'
def test_channels(self):
input_space = Conv2DSpace((3, 3), num_channels=3) filters_values = numpy.ones((2, 3, 2, 2), dtype=theano.config.floatX) filters = sharedX(filters_values) image = numpy.random.rand(1, 3, 3, 3).astype(theano.config.floatX) conv2d = Conv2D(filters, 1, input_space) f = theano.function([self.image_te...
'Create a random convolution and check whether the shape, axes and input space are all what we expect'
def test_make_random_conv2D(self):
output_space = Conv2DSpace((2, 2), 1) conv2d = make_random_conv2D(1, self.input_space, output_space, (2, 2), 1) f = theano.function([self.image_tensor], conv2d.lmul(self.image_tensor)) assert (f(self.image).shape == (1, 2, 2, 1)) assert (conv2d.input_space == self.input_space) assert (conv2d.out...
'.. todo:: WRITEME'
@functools.wraps(P2LT.get_params) def get_params(self):
return [self._filters]
'.. todo:: WRITEME'
@functools.wraps(P2LT.get_weights_topo) def get_weights_topo(self, borrow):
return np.transpose(self._filters.get_value(borrow=borrow), (0, 2, 3, 1))
'.. todo:: WRITEME properly dot(x, A) This method overrides the original Conv2D lmul to make it work with arbitrary axis orders'
def lmul(self, x):
assert (x.ndim == 4) axes = self.input_space.axes assert (len(axes) == 4) op_axes = ('b', 'c', 0, 1) if (tuple(axes) != op_axes): x = x.dimshuffle(axes.index('b'), axes.index('c'), axes.index(0), axes.index(1)) rval = conv2d(x, self._filters, image_shape=self._img_shape, filter_shape=sel...
'.. todo:: WRITEME properly Override the original Conv2D lmul_T to make it work with pylearn format of topological data using dimshuffles'
def lmul_T(self, x):
assert (x.dtype == self._filters.dtype) op_axes = ('b', 'c', 0, 1) axes = self.output_axes if (tuple(axes) != op_axes): x = x.dimshuffle(axes.index('b'), axes.index('c'), axes.index(0), axes.index(1)) dummy_v = T.tensor4() dummy_v.name = 'dummy_v' if (theano.config.compute_test_value...
'.. todo:: WRITEME properly Kind of a stupid hacky method used to support convolutional score matching. Ought to find a way to make _filters symbolic rather than shared.'
def lmul_sq_T(self, x):
assert (x.dtype == self._filters.dtype) op_axes = ('b', 'c', 0, 1) axes = self.output_axes if (tuple(axes) != op_axes): x = x.dimshuffle(axes.index('b'), axes.index('c'), axes.index(0), axes.index(1)) dummy_v = T.tensor4() sqfilt = T.square(self._filters) z_hs = conv2d(dummy_v, sqfil...
'.. todo:: WRITEME'
def set_batch_size(self, batch_size):
self._img_shape = tuple(([batch_size] + list(self._img_shape[1:])))
'Return self._filters.'
@functools.wraps(P2LT.get_params) def get_params(self):
return [self._filters]
'Parameters borrow : TODO TODO'
@functools.wraps(P2LT.get_weights_topo) def get_weights_topo(self, borrow):
return np.transpose(self._filters.get_value(borrow=borrow), (0, 2, 3, 1))
'.. todo:: WRITEME properly dot(x, A) This method overrides the original Conv2D lmul to make it work with arbitrary axis orders Parameters x : TODO TODO'
def lmul(self, x):
assert (x.ndim == 4) axes = self._input_space.axes assert (len(axes) == 4) op_axes = ('b', 'c', 0, 1) if (tuple(axes) != op_axes): x = x.dimshuffle(*[axes.index(ax) for ax in op_axes]) img = gpu_contiguous(x) kerns = gpu_contiguous(self._filters) shape = GpuDnnConv.get_out_shape(...
'.. todo:: WRITEME Parameters batch_size : TODO TODO'
def set_batch_size(self, batch_size):
self._img_shape = tuple(([batch_size] + list(self._img_shape[1:])))
'.. todo:: WRITEME'
@functools.wraps(LinearTransform.get_params) def get_params(self):
return [self._filters]
'.. todo:: WRITEME'
@functools.wraps(LinearTransform.get_weights_topo) def get_weights_topo(self, borrow=False):
(inp, rows, cols, outp) = range(4) raw = self._filters.get_value(borrow=borrow) return np.transpose(raw, (outp, rows, cols, inp))
'.. todo:: WRITEME properly dot(x, A) aka, do convolution with input image x'
def lmul(self, x):
check_cuda((str(type(self)) + '.lmul')) cpu = ('Cuda' not in str(type(x))) if cpu: x = gpu_from_host(x) assert (x.ndim == 4) x_axes = self.input_axes assert (len(x_axes) == 4) op_axes = ('c', 0, 1, 'b') if (tuple(x_axes) != op_axes): x = x.dimshuffle(*[x_axes.index(axis) ...
'.. todo:: WRITEME'
def lmul_T(self, x):
check_cuda((str(type(self)) + '.lmul_T')) assert (x.dtype == self._filters.dtype) op_axes = ('c', 0, 1, 'b') axes = self.output_axes if (tuple(axes) != op_axes): x = x.dimshuffle(*[axes.index(ax) for ax in op_axes]) x = gpu_contiguous(x) rval = ImageActs(pad=self.pad, partial_sum=sel...
'.. todo:: WRITEME'
def lmul_sq_T(self, x):
raise NotImplementedError('This method is not yet modified since copy-pasting from pylearn2.linear.conv2d') ' Kind of a stupid hacky method used to support convolutional score\n matching. Ought to find a wa...
'.. todo:: WRITEME'
def set_batch_size(self, batch_size):
pass
'Return a list of parameters that govern the linear transformation'
def get_params(self):
raise NotImplementedError()
'Return a batch of filters, formatted topologically. This only really makes sense if you are working with a topological space, such as for a convolution operator. If your transformation is defined on a VectorSpace then some other class like a ViewConverter will need to transform your vector into a topological space; yo...
def get_weights_topo(self):
raise NotImplementedError()
'Some transformers such as Conv2D have a fixed batch size. Use this method to change the batch size. Parameters batch_size : int The size of the batch'
def set_batch_size(self, batch_size):
pass
'Sets the initial values of the matrix'
def __init__(self, W):
self._W = W
'.. todo:: WRITEME'
@functools.wraps(LinearTransform.get_params) def get_params(self):
return [self._W]
'.. todo:: WRITEME Parameters x : ndarray, 1d or 2d The input data'
def lmul(self, x):
return T.dot(x, self._W)
'.. todo:: WRITEME Parameters x : ndarray, 1d or 2d The input data'
def lmul_T(self, x):
return T.dot(x, self._W.T)
'Reads a given configuration file, and updates the internal installed packages list. :param from_location: a path (string) to a directory containing an installed.lst file'
def read_installed_packages_list(self, from_location):
try: installed_list_file = open((from_location + '/installed.lst')) except IOError: pass else: for line in installed_list_file: l = line.rstrip().split(' ') if l: self.installed_packages_list[l[0]] = this_package = self.package_info(from_loc...
'Looks up a dataset name and return its location, or None if it\'s unknown. :param dataset_name: a canonical dataset name, \'mnist\' for e.g. :returns: A path, if dataset is found, or None otherwise.'
def resolve_dataset(self, dataset_name):
if (dataset_name in self.installed_packages_list): return os.path.join(self.installed_packages_list[dataset_name].where, self.installed_packages_list[dataset_name].name) else: return None
'Scans possible locations to load installed.lst files. It first scans the root install, the user install, then the paths, from left to right, specified by the PYLEARN2_DATA_PATH environment variable.'
def __init__(self):
paths = ['/etc/pylearn/', (os.environ['HOME'] + '/.local/share/pylearn/')] try: paths += re.split(':|;', os.environ['PYLEARN2_DATA_PATH']) except Exception: pass for path in paths: self.read_installed_packages_list(path)
'which_set: A string specifying which portion of the dataset to load. Valid values are \'train\' or \'public_test\' base_path: The directory containing the .csv files from kaggle.com. This directory should be writable; if the .csv files haven\'t already been converted to npy, this class will convert them to save memory...
def __init__(self, which_set, base_path='${PYLEARN2_DATA_PATH}/icml_2013_black_box', start=None, stop=None, preprocessor=None, fit_preprocessor=False, fit_test_preprocessor=False):
self.test_args = locals() self.test_args['which_set'] = 'public_test' self.test_args['fit_preprocessor'] = fit_test_preprocessor del self.test_args['start'] del self.test_args['stop'] del self.test_args['self'] files = {'train': 'train.csv', 'public_test': 'test.csv'} sizes = {'train': 1...
'which_set: A string specifying which portion of the dataset to load. Valid values are \'train\' or \'public_test\' base_path: The directory containing the .csv files from kaggle.com. This directory should be writable; if the .csv files haven\'t already been converted to npy, this class will convert them to save memory...
def __init__(self, which_set, base_path='${PYLEARN2_DATA_PATH}/icml_2013_emotions', start=None, stop=None, preprocessor=None, fit_preprocessor=False, axes=('b', 0, 1, 'c'), fit_test_preprocessor=False):
self.test_args = locals() self.test_args['which_set'] = 'public_test' self.test_args['fit_preprocessor'] = fit_test_preprocessor del self.test_args['start'] del self.test_args['stop'] del self.test_args['self'] del self.test_args['__class__'] files = {'train': 'train.csv', 'public_test':...
'f: the theano function whose work needs to be split nhid: we assume f takes a matrix of shape (m,nvis) and returns a matrix of shape (m,nhid)'
def __init__(self, f, nhid):
self.f = f self.nhid = nhid
'dataset_maker: A callable that returns a Dataset num_examples: the number of examples we expect the dataset to have (just for error checking purposes)'
def __init__(self, dataset_maker, num_examples, pipeline_path):
self.dataset_maker = dataset_maker self.num_examples = num_examples self.pipeline_path = pipeline_path
'batch_size: the number of images to process simultaneously this does not affect the final result, it is just for performance larger values allow more parallel processing but require more memory kmeans_path: a path to a .pkl file containing a pylearn2.kmeans.KMeans instance save_path: the base path to save to, shoul...
def __init__(self, batch_size, kmeans_path, save_path, dataset_family, which_set, num_output_features, chunk_size=None, restrict=None, pool_mode='mean'):
if ((chunk_size is not None) and (restrict is not None)): raise NotImplementedError('Currently restrict is used internally to implement chunk_size, so a client may not specify both') self.batch_size = batch_size self.model_path = kmeans_path self.restric...
'.. todo:: WRITEME'
def __init__(self, encoder, dataset, path, batch_size=None, topo=False):
self.encoder = encoder self.dataset = dataset self.path = path self.batch_size = batch_size self.topo = topo
'.. todo:: WRITEME Parameters **kwargs : dict, optional WRITEME'
def main_loop(self, **kwargs):
if (self.batch_size is None): if self.topo: data = self.dataset.get_topological_view() else: data = self.dataset.get_design_matrix() output = self.encoder.perform(data) else: myiterator = self.dataset.iterator(mode='sequential', batch_size=self.batch_size,...
'.. todo:: WRITEME'
def __call__(self, inputs):
if self.input_space: self.input_space.validate(inputs) return self.theano_rng.binomial(p=inputs, size=inputs.shape, dtype=inputs.dtype)
'.. todo:: WRITEME'
def set_input_space(self, space):
self.input_space = space
'.. todo:: WRITEME'
def get_input_space(self):
if (self.input_space is not None): return self.input_space raise ValueError(('No input space was specified for this Block (%s). You can call set_input_space to correct that.' % str(self)))
'.. todo:: WRITEME'
def get_output_space(self):
return self.get_input_space()
'.. todo:: WRITEME'
def __call__(self, bc01):
half = (self.n // 2) sq = T.sqr(bc01) (b, ch, r, c) = bc01.shape extra_channels = T.alloc(0.0, b, (ch + (2 * half)), r, c) sq = T.set_subtensor(extra_channels[:, half:(half + ch), :, :], sq) scale = self.k for i in xrange(self.n): scale += (self.alpha * sq[:, i:(i + ch), :, :]) s...
'.. todo:: WRITEME'
def __call__(self, c01b):
half = (self.n // 2) sq = T.sqr(c01b) (ch, r, c, b) = c01b.shape extra_channels = T.alloc(0.0, (ch + (2 * half)), r, c, b) sq = T.set_subtensor(extra_channels[half:(half + ch), :, :, :], sq) scale = self.k for i in xrange(self.n): scale += (self.alpha * sq[i:(i + ch), :, :, :]) s...
'.. todo:: WRITEME properly NOTE: c01b must be CudaNdarrayType.'
def __call__(self, c01b):
return self._op(c01b)[0]
'.. todo:: WRITEME'
def __call__(self, inputs):
if self.input_space: self.input_space.validate(inputs) return inputs
'.. todo:: WRITEME'
def set_input_space(self, space):
self.input_space = space
'.. todo:: WRITEME'
def get_input_space(self):
if (self.input_space is not None): return self.input_space raise ValueError(('No input space was specified for this Block (%s). You can call set_input_space to correct that.' % str(self)))
'.. todo:: WRITEME'
def get_output_space(self):
return self.get_input_space()
'Sets the shape of the display (in pixels) Parameters shape : tuple The (rows, columns) of the display.'
def set_shape(self, shape):
self.rows = shape[0] self.cols = shape[1]
'Sets the range of space that is plotted in the graph. Parameters xlim : tuple The range (xmin, xmax)'
def set_xlim(self, xlim):
self.xmin = xlim[0] self.xmax = xlim[1] self.delta_x = ((self.xmax - self.xmin) / float((self.cols - 1)))
'Sets the y coordinate of the central pixel of the display. Parameters ycenter : float The desired coordinate.'
def set_ycenter(self, ycenter):
self.delta_y = self.delta_x self.ymin = (ycenter - ((self.rows / 2) * self.delta_y)) self.ymax = (self.ymin + ((self.rows - 1) * self.delta_y))
'Renders the graph. Returns output : ndarray An ndarray in (rows, cols, RGB) format.'
def render(self):
rval = N.zeros((self.rows, self.cols, 3)) for component in self.components: rval = component.render(prev_layer=rval, parent=self) assert (rval is not None) return rval
'Returns the coordinates of every pixel in column i of the graph. Parameters i : int Column index Returns coords : ndarray A vector containing the real-number coordinates of every pixel in column i of the graph.'
def get_coords_for_col(self, i):
X = N.zeros((self.rows, 2), dtype=config.floatX) X[:, 0] = (self.xmin + (float(i) * self.delta_x)) X[:, 1] = (self.ymin + (N.cast[config.floatX](N.asarray(range((self.rows - 1), (-1), (-1)))) * self.delta_y)) return X
'Renders the heatmap. Parameters prev_layer : numpy ndarray An image that will be copied into the new output. The new image will be rendered on top of the first one, i.e., `prev_layer` will be visible through the new heatmap if the new heatmap is not rendered in fully opaque mode. parent : Graph2D A Graph2D object that...
def render(self, prev_layer, parent):
my_img = (prev_layer * 0.0) for i in xrange(prev_layer.shape[1]): X = parent.get_coords_for_col(i) f = self.f(X) if (len(f.shape) == 1): for j in xrange(3): my_img[:, i, j] = f else: my_img[:, i, :] = f if (self.normalizer is not None):...
'.. todo:: WRITEME'
def clear(self):
if self.is_color: for i in xrange(3): self.image[:, :, i] = ((self.background[i] * 0.5) + 0.5) else: self.image[:] = ((self.background * 0.5) + 0.5) self.cur_pos = (0, 0)
'Adds an image patch to the `PatchViewer`. Patches are added left to right, top to bottom. If this method is called when the `PatchViewer` is already full, it will clear the viewer and start adding patches at the upper left again. Parameters patch : ndarray If this `PatchViewer` is in color (controlled by the `is_color...
def add_patch(self, patch, rescale=True, recenter=True, activation=None, warn_blank_patch=True):
if (warn_blank_patch and (patch.min() == patch.max()) and (rescale or (patch.min() == 0.0))): warnings.warn('displaying totally blank patch') if self.is_color: assert (patch.ndim == 3) if (not (patch.shape[(-1)] == 3)): raise ValueError(('Expected color image ...
'.. todo:: WRITEME'
def show(self):
show(self.image)
'.. todo:: WRITEME'
def get_img(self):
x = np.cast['uint8']((self.image * 255.0)) if (x.shape[2] == 1): x = x[:, :, 0] ensure_Image() img = Image.fromarray(x) return img
'.. todo:: WRITEME'
def save(self, path):
self.get_img().save(path)
'.. todo:: WRITEME properly Returns a shape that fits n elements. If exact, fits exactly n elements'
def pick_shape(n, exact=False):
if (not isinstance(n, py_integer_types)): raise TypeError(('n must be an integer, but is ' + str(type(n)))) if exact: best_r = (-1) best_c = (-1) best_ratio = 0 for r in xrange(1, (int(np.sqrt(n)) + 1)): if ((n % r) != 0): ...
'The partition function makes this intractable. Parameters model : DBM data : Batch in get_data_specs format'
def expr(self, model, data):
self.get_data_specs(model)[0].validate(data) return None
'.. todo:: WRITEME'
def _get_toronto_neg(self, model, layer_to_chains):
assert isinstance(model.visible_layer, BinaryVector) assert isinstance(model.hidden_layers[0], BinaryVectorMaxPool) assert (model.hidden_layers[0].pool_size == 1) assert isinstance(model.hidden_layers[1], BinaryVectorMaxPool) assert (model.hidden_layers[1].pool_size == 1) assert isinstance(model...
'.. todo:: WRITEME TODO:reduce variance of negative phase by integrating out the even-numbered layers. The Rao-Blackwellize method can do this for you when expected gradient = gradient of expectation, but doing this in general is trickier.'
def _get_standard_neg(self, model, layer_to_chains):
params = list(model.get_params()) expected_energy_p = model.energy(layer_to_chains[model.visible_layer], [layer_to_chains[layer] for layer in model.hidden_layers]).mean() samples = flatten(layer_to_chains.values()) for (i, sample) in enumerate(samples): if (sample.name is None): samp...
'.. todo:: WRITEME'
def _get_variational_pos(self, model, X, Y):
if self.supervised: assert (Y is not None) assert isinstance(model.hidden_layers[(-1)], Softmax) q = model.mf(X, Y) '\n Use the non-negativity of the KL divergence to construct a lower\n ...
'.. todo:: WRITEME'
def _get_sampling_pos(self, model, X, Y):
layer_to_clamp = OrderedDict([(model.visible_layer, True)]) layer_to_pos_samples = OrderedDict([(model.visible_layer, X)]) if self.supervised: assert isinstance(model.hidden_layers[(-1)], Softmax) layer_to_clamp[model.hidden_layers[(-1)]] = True layer_to_pos_samples[model.hidden_laye...
'Computes the positive phase using Gibbs sampling. Returns gradients : OrderedDict A dictionary mapping parameters to positive phase gradients. updates : OrderedDict An empty dictionary'
def _get_positive_phase(self, model, X, Y=None):
return (self._get_sampling_pos(model, X, Y), OrderedDict())
'.. todo:: WRITEME'
def _get_negative_phase(self, model, X, Y=None):
layer_to_chains = model.make_layer_to_state(self.num_chains) def recurse_check(l): if isinstance(l, (list, tuple, collections.ValuesView)): for elem in l: recurse_check(elem) else: assert (l.get_value().shape[0] == self.num_chains) recurse_check(layer_...
'The partition function makes this intractable. Parameters model : Model data : Minibatch in get_data_specs format Returns None : (Always returns None)'
def expr(self, model, data):
self.get_data_specs(model)[0].validate(data) return None