https://github.com/GPflow/GPflow
Revision 20894c845ace8f64e9ea68223a7bb25cd46221c2 authored by Artem Artemev on 14 March 2019, 18:09:30 UTC, committed by Artem Artemev on 14 March 2019, 18:09:30 UTC
1 parent 0741129
Raw File
Tip revision: 20894c845ace8f64e9ea68223a7bb25cd46221c2 authored by Artem Artemev on 14 March 2019, 18:09:30 UTC
Remove some tests
Tip revision: 20894c8
products.py
from functools import reduce

import tensorflow as tf

from . import dispatch
from .. import kernels
from ..features import InducingPoints
from ..probability_distributions import DiagonalGaussian
from ..util import NoneType
from .expectations import expectation


@dispatch.expectation.register(DiagonalGaussian, kernels.Product, NoneType, NoneType, NoneType)
def _E(p, kern, _, __, ___, nghp=None):
    """
    Compute the expectation:
    <\HadamardProd_i diag(Ki_{X[:, active_dims_i], X[:, active_dims_i]})>_p(X)
        - \HadamardProd_i Ki_{.,.} :: Product kernel
        - p                        :: DiagonalGaussian distribution (p.cov NxD)

    :return: N
    """
    if not kern.on_separate_dimensions:
        raise NotImplementedError(
            "Product currently needs to be defined on separate dimensions.")  # pragma: no cover

    exps = [expectation(p, k, nghp=nghp) for k in kern.kernels]
    return reduce(tf.multiply, exps)


@dispatch.expectation.register(DiagonalGaussian, kernels.Product, InducingPoints, NoneType, NoneType)
def _E(p, kern, feat, __, ___, nghp=None):
    """
    Compute the expectation:
    <\HadamardProd_i Ki_{X[:, active_dims_i], Z[:, active_dims_i]}>_p(X)
        - \HadamardProd_i Ki_{.,.} :: Product kernel
        - p                        :: DiagonalGaussian distribution (p.cov NxD)

    :return: NxM
    """
    if not kern.on_separate_dimensions:
        raise NotImplementedError(
            "Product currently needs to be defined on separate dimensions.")  # pragma: no cover

    exps = [expectation(p, (k, feat), nghp=nghp) for k in kern.kernels]
    return reduce(tf.multiply, exps)


@dispatch.expectation.register(DiagonalGaussian, kernels.Product, InducingPoints, kernels.Product, InducingPoints)
def _E(p, kern1, feat1, kern2, feat2, nghp=None):
    """
    Compute the expectation:
    expectation[n] = < prodK_{Z, x_n} prodK_{x_n, Z} >_p(x_n)
                   = < (\HadamardProd_i Ki_{Z[:, active_dims_i], x[n, active_dims_i]})  <-- Mx1
               1xM -->  (\HadamardProd_j Kj_{x[n, active_dims_j], Z[:, active_dims_j]}) >_p(x_n)  (MxM)

        - \HadamardProd_i Ki_{.,.}, \HadamardProd_j Kj_{.,.} :: Product kernels
        - p                        :: DiagonalGaussian distribution (p.cov NxD)

    :return: NxMxM
    """
    if feat1 != feat2:
        raise NotImplementedError("Different features are not supported.")
    if kern1 != kern2:
        raise NotImplementedError("Calculating the expectation over two "
                                  "different Product kernels is not supported.")

    kern = kern1
    feat = feat1

    if not kern.on_separate_dimensions:
        raise NotImplementedError(
            "Product currently needs to be defined on separate dimensions.")  # pragma: no cover

    exps = [expectation(p, (k, feat), (k, feat), nghp=nghp) for k in kern.kernels]
    return reduce(tf.multiply, exps)
back to top