Revision 48270681afc13081094f7f398a1e194c6b07ba9b authored by vdutor on 03 January 2018, 17:44:53 UTC, committed by Mark van der Wilk on 03 January 2018, 17:44:53 UTC
* Outline of new expectations code.

* Quadrature code now uses TensorFlow shape inference.

* General expectations work.

* Expectations RBF kern, not tested

* Add Identity mean function

* General unittests for Expectations

* Add multipledispatch package to travis

* Update tests_expectations

* Expectations of mean functions

* Mean function uncertain conditional

* Uncertain conditional with mean_function. Tested.

* Support for Add and Prod kernels and quadrature fallback decorator

* Refactor expectations unittests

* Psi stats Linear kernel

* Split expectations in different files

* Expectation Linear kernel and Linear mean function

* Remove None's from expectations api

* Removed old ekernels framework

* Add multipledispatch to setup file

* Work on PR feedback, not finished

* Addressed PR feedback

* Support for pairwise xKxz

* Enable expectations unittests

* Renamed `TimeseriesGaussian` to `MarkovGaussian` and added tests.

* Rename some variable, plus note for later test of <x Kxz>_q.

* Update conditionals.py

Add comment

* Change order of inputs to (feat, kern)

* Stef/expectations (#601)

* adding gaussmarkov quad

* don't override the markvogaussian in the quadrature

* can't test

* adding external test

* quadrature code done and works for MarkovGauss

* MarkovGaussian with quad implemented. All tests pass

* Shape comments.

* Removed superfluous autoflow functions for kernel expectations

* Update kernels.py

* Update quadrature.py
1 parent 2182bf0
Raw File
paramlist.py
# Copyright 2016 James Hensman, Mark van der Wilk,
#                Valentine Svensson, alexggmatthews,
#                PabloLeon, fujiisoup
# Copyright 2017 Artem Artemev @awav
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from ..core.tensor_converter import TensorConverter
from ..core.errors import GPflowError
from ..core.compilable import Build

from .parameter import Parameter
from .parameterized import Parameterized


class ParamList(Parameterized):
    """
    ParamList is special case of parameterized object. It implements different
    access pattern for its childres. Instead saving node like objects as attributes
    it keeps them in the list, providing indexed access and acts as simple list.

    :param list_of_params: list of node like objects.
    :param trainable: Boolean flag. It indicates whether children parameters
        should be trainable or not.
    :param name: ParamList name.
    """

    def __init__(self, list_of_params, trainable=True, name=None):
        super(ParamList, self).__init__(name=None)
        if not isinstance(list_of_params, list):
            raise ValueError('Not acceptable argument type at list_of_params.')
        self._list = [self._valid_list_input(item, trainable)
                      for item in list_of_params]
        for index, item in enumerate(self._list):
            self._set_param(index, item)

    @property
    def params(self):
        for item in self._list:
            yield item

    def append(self, item):
        if not isinstance(item, Parameter):
            raise ValueError(
                'Non parameter type cannot be appended to the list.')
        length = self.__len__()
        item.set_parent(self)
        item.set_name(self._item_name(length))
        self._list.append(item)

    def _get_param(self, name):
        return self._list[name]

    def _set_param(self, name, value):
        self._list[name] = value
        value.set_parent(self)
        value.set_name(self._item_name(name))

    def _item_name(self, index):
        return '{name}{index}'.format(name='item', index=index)

    def _valid_list_input(self, value, trainable):
        if not Parameterized._is_param_like(value):
            try:
                return Parameter(value, trainable=trainable)
            except ValueError:
                raise ValueError(
                    'A list item must be either parameter, '
                    'tensorflow variable, an array or a scalar.')
        return value

    def __len__(self):
        return len(self._list)

    def __getitem__(self, key):
        param = self._get_param(key)
        if TensorConverter.tensor_mode(self) and isinstance(param, Parameter):
            return Parameterized._tensor_mode_parameter(param)
        return param

    def __setitem__(self, index, value):
        if not isinstance(value, Parameter):
            raise ValueError(
                'Non parameter type cannot be assigned to the list.')
        if not self.empty and self.is_built_coherence(value.graph) is Build.YES:
            raise GPflowError(
                'ParamList is compiled and items are not modifiable.')
        self._update_param_attribute(index, value)
back to top