Exemplo n.º 1
0
    def __init__(self, **kwargs):
        dictlist.replace(kwargs, lambda x: (x, ) if isinstance(x, int) else x)
        self.n_pars = n_pars_by_partition(kwargs)

        # Create two representations of the parameters of the object. The first
        # is the symbolic theano variable (of which the type is GPU/CPU
        # specific), the second either a gnumpy or numpy array (depending on
        # GPU/CPU again). Also set a default size for testing.
        if GPU:
            self.data = gnumpy.zeros(self.n_pars)
            self.flat = theano.sandbox.cuda.fvector('parameters')
        else:
            self.data = np.empty(self.n_pars).astype(theano.config.floatX)
            self.flat = T.vector('parameters')

        self.flat.tag.test_value = self.data

        # Go through parameters and assign space and variable.
        self.views = array_partition_views(self.data, kwargs)

        # Make sure the keys are legit -- that they do not overwrite
        # anything.
        for key in kwargs:
            if hasattr(self, key):
                raise ValueError("%s is an illegal name for a variable")

        variables = array_partition_views(self.flat, kwargs)
        variables = dictlist.copy(variables, dct_maker=attrdict.AttrDict)
        self.__dict__.update(variables)
Exemplo n.º 2
0
Arquivo: util.py Projeto: k9triz/breze
    def __init__(self, **kwargs):
        dictlist.replace(kwargs, lambda x: (x,) if isinstance(x, int) else x)
        self.n_pars = n_pars_by_partition(kwargs)

        # Create two representations of the parameters of the object. The first
        # is the symbolic theano variable (of which the type is GPU/CPU
        # specific), the second either a gnumpy or numpy array (depending on
        # GPU/CPU again). Also set a default size for testing.
        if GPU:
            self.data = gnumpy.zeros(self.n_pars)
            self.flat = theano.sandbox.cuda.fvector('parameters')
        else:
            self.data = np.empty(self.n_pars).astype(theano.config.floatX)
            self.flat = T.vector('parameters')

        self.flat.tag.test_value = self.data

        # Go through parameters and assign space and variable.
        self.views = array_partition_views(self.data, kwargs)

        # Make sure the keys are legit -- that they do not overwrite
        # anything.
        for key in kwargs:
            if hasattr(self, key):
                raise ValueError("%s is an illegal name for a variable")

        variables = array_partition_views(self.flat, kwargs)
        variables = dictlist.copy(variables, dct_maker=attrdict.AttrDict)
        self.__dict__.update(variables)
Exemplo n.º 3
0
Arquivo: util.py Projeto: slucia/breze
def array_partition_views(array, partition):
    """Return a dictlist with different items corresponding to different sub
    views into an array.

    This function can be used to use different parts of a one-dimensional
    array as a collection of arrays with differing without the need to
    reallocate. E.g the first half of an array might correspond to a matrix,
    the next entry to a scalar and the rest to a vector.

    The function takes two arguments. The first, ``array`` is supposed to
    resemble the flat array, while the second is a dictlist partitioning the
    array into views of given shapes.

    Here, the leaves of ``partition`` will be treated as shapes, of which a
    view into ``array`` should be contained in the result with the same path.


    Parameters
    ----------

    array : np.array or gp.garray
        numpy or gnumpy array of ndim 1.

    partition : dictlist
        Dictlist -- that is a a dictionary containing either lists or dicts as
        its elements, except leave nodes. These should be either tuples or ints
        to represent shapes of arays.


    Examples
    --------

    >>> from breze.arch.util import array_partition_views
    >>> partition = {'first': (2,),
    ...              'second': (2, 3),}
    >>> flat = np.arange(8)
    >>> views = array_partition_views(flat, partition)
    >>> views['first']
    ... # doctest: +NORMALIZE_WHITESPACE
        array([0, 1])
    >>> views['second']
    ... # doctest: +NORMALIZE_WHITESPACE
        array([[2, 3, 4],
               [5, 6, 7]])
    """
    views = dictlist.copy(partition)
    pathsshapes = sorted(list(dictlist.leafs(partition)))

    n_used = 0
    for path, shape in pathsshapes:
        item = dictlist.get(partition, path)
        shape = (item,) if isinstance(item, (int, long)) else item
        size = int(np.prod(shape))
        dictlist.set_(views, path, array[n_used:n_used + size].reshape(shape))
        n_used += size

    return views
Exemplo n.º 4
0
def array_partition_views(array, partition):
    """Return a dictlist with different items corresponding to different sub
    views into an array.

    This function can be used to use different parts of a one-dimensional
    array as a collection of arrays with differing without the need to
    reallocate. E.g the first half of an array might correspond to a matrix,
    the next entry to a scalar and the rest to a vector.

    The function takes two arguments. The first, ``array`` is supposed to
    resemble the flat array, while the second is a dictlist partitioning the
    array into views of given shapes.

    Here, the leaves of ``partition`` will be treated as shapes, of which a
    view into ``array`` should be contained in the result with the same path.


    Parameters
    ----------

    array : np.array or gp.garray
        numpy or gnumpy array of ndim 1.

    partition : dictlist
        Dictlist -- that is a a dictionary containing either lists or dicts as
        its elements, except leave nodes. These should be either tuples or ints
        to represent shapes of arays.


    Examples
    --------

    >>> from breze.arch.util import array_partition_views
    >>> partition = {'first': (2,),
    ...              'second': (2, 3),}
    >>> flat = np.arange(8)
    >>> views = array_partition_views(flat, partition)
    >>> views['first']
    ... # doctest: +NORMALIZE_WHITESPACE
        array([0, 1])
    >>> views['second']
    ... # doctest: +NORMALIZE_WHITESPACE
        array([[2, 3, 4],
               [5, 6, 7]])
    """
    views = dictlist.copy(partition)
    pathsshapes = sorted(list(dictlist.leafs(partition)))

    n_used = 0
    for path, shape in pathsshapes:
        item = dictlist.get(partition, path)
        shape = (item, ) if isinstance(item, (int, long)) else item
        size = int(np.prod(shape))
        dictlist.set_(views, path, array[n_used:n_used + size].reshape(shape))
        n_used += size

    return views
Exemplo n.º 5
0
Arquivo: util.py Projeto: k9triz/breze
def array_partition_views(array, partition):
    views = dictlist.copy(partition)
    pathsshapes = sorted(list(dictlist.leafs(partition)))

    n_used = 0
    for path, shape in pathsshapes:
        item = dictlist.get(partition, path)
        shape = (item,) if isinstance(item, (int, long)) else item
        size = int(np.prod(shape))
        dictlist.set_(views, path, array[n_used:n_used + size].reshape(shape))
        n_used += size

    return views
Exemplo n.º 6
0
def array_partition_views(array, partition):
    views = dictlist.copy(partition)
    pathsshapes = sorted(list(dictlist.leafs(partition)))

    n_used = 0
    for path, shape in pathsshapes:
        item = dictlist.get(partition, path)
        shape = (item, ) if isinstance(item, int) else item
        size = int(np.prod(shape))
        dictlist.set_(views, path, array[n_used:n_used + size].reshape(shape))
        n_used += size

    return views
Exemplo n.º 7
0
def test_copy():
    tree = make_dictlist()
    tree2 = dictlist.copy(tree, dct_maker=AttrDict)

    print tree2
Exemplo n.º 8
0
def test_copy():
    tree = make_dictlist()
    tree2 = dictlist.copy(tree, dct_maker=AttrDict)

    print tree2