Utility functions

climin.util.empty_with_views(shapes, empty_func=<built-in function empty>)

Create an array and views shaped according to shapes.

The shapes parameter is a list of tuples of ints. Each tuple represents a desired shape for an array which will be allocated in a bigger memory region. This memory region will be represented by an array as well.

For example, the shape speciciation [2, (3, 2)] will create an array flat of size 8. The first view will have a size of (2,) and point to the first two entries, i.e. flat`[:2]`, while the second array will have a shape of ``(3, 2) and point to the elements flat[2:8].

Parameters:

spec : list of tuples of ints

Specification of the desired shapes.

empty_func : callable

function that returns a memory region given an integer of the desired size. (Examples include numpy.empty, which is the default, gnumpy.empty and theano.tensor.empty.

Returns:

flat : array_like (depending on empty_func)

Memory region containing all the views.

views : list of array_like

Variable number of results. Each contains a view into the array flat.

Examples

>>> from climin.util import empty_with_views
>>> flat, (w, b) = empty_with_views([(3, 2), 2])
>>> w[...] = 1
>>> b[...] = 2
>>> flat
array([ 1.,  1.,  1.,  1.,  1.,  1.,  2.,  2.])
>>> flat[0] = 3
>>> w
array([[ 3.,  1.],
       [ 1.,  1.],
       [ 1.,  1.]])
climin.util.shaped_from_flat(flat, shapes)

Given a one dimensional array flat, return a list of views of shapes shapes on that array.

Each view will point to a distinct memory region, consecutively allocated in flat.

Parameters:

flat : array_like

Array of one dimension.

shapes : list of tuples of ints

Each entry of this list specifies the shape of the corresponding view into flat.

Returns:

views : list of arrays

Each entry has the shape given in shapes and points as a view into flat.

climin.util.minibatches(arr, batch_size, d=0)

Return a list of views of the given arr.

Each view represents a mini bach of the data.

Parameters:

arr : array_like

Array to obtain batches from. Needs to be slicable. If d > 0, needs to have a .shape attribute from which the number of samples can be obtained.

batch_size : int

Size of a batch. Last batch might be smaller if batch_size is not a divisor of arr.

d : int, optional, default: 0

Dimension along which the data samples are separated and thus slicing should be done.

Returns:

mini_batches : list

Each item of the list is a view of arr. Views are ordered.

climin.util.iter_minibatches(lst, batch_size, dims, n_cycles=None, random_state=None, discard_illsized_batch=False)

Return an iterator that successively yields tuples containing aligned minibatches of size batch_size from slicable objects given in lst, in random order without replacement. Because different containers might require slicing over different dimensions, the dimension of each container has to be givens as a list dims.

Parameters:

lst : list of array_like

Each item of the list will be sliced into mini batches in alignment with the others.

batch_size : int

Size of each batch. Last batch might be smaller.

dims : list

Aligned with lst, gives the dimension along which the data samples are separated.

n_cycles : int, optional [default: None]

Number of cycles after which to stop the iterator. If None, will yield forever.

random_state : a numpy.random.RandomState object, optional [default

Random number generator that will act as a seed for the minibatch order.

discard_illsized_batch : bool, optional [default

If True and the length of the sliced dimension is not divisible by batch_size, the leftover samples are discarded.

Returns:

batches : iterator

Infinite iterator of mini batches in random order (without replacement).