Skip to content

matrix_variate_multitask_kernel

MatrixVariateIndexKernel

Bases: Kernel

Wraps IndexKernel to represent https://en.wikipedia.org/wiki/Matrix_normal_distribution

P(X | M, U, V) = exp(-0.5 tr[ V⁻¹ (X - M)ᵀ U⁻¹ (X-M) ] ) / √((2π)ⁿᵖ|V|ⁿ|U|ᵖ)

vec(X) ~ 𝒩(M, V ⊗ U)

This kernel represents the covariance_matrix V ⊗ U given V and U.

Source code in bayes_cbf/matrix_variate_multitask_kernel.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class MatrixVariateIndexKernel(Kernel):
    """
    Wraps IndexKernel to represent
    https://en.wikipedia.org/wiki/Matrix_normal_distribution

    P(X | M, U, V) = exp(-0.5 tr[ V⁻¹ (X - M)ᵀ U⁻¹ (X-M) ] ) / √((2π)ⁿᵖ|V|ⁿ|U|ᵖ)

    vec(X) ~ 𝒩(M, V ⊗ U)

    This kernel represents the covariance_matrix V ⊗ U given V and U.
    """
    def __init__(self, U : IndexKernel, V: IndexKernel):
        super(MatrixVariateIndexKernel, self).__init__()
        self.U = U
        self.V = V
        n = self.U.raw_var.shape[-1]
        p = self.V.raw_var.shape[-1]
        self.matshape = (n,p)

    @property
    def covar_matrix(self):
        U = self.U.covar_matrix
        V = self.V.covar_matrix
        return KroneckerProductLazyTensor(V, U)

    def forward(self, i1, i2, **params):
        assert i1.dtype in (torch.int64, torch.int32)
        assert i2.dtype in (torch.int64, torch.int32)
        covar_matrix = self.covar_matrix
        res = InterpolatedLazyTensor(base_lazy_tensor=covar_matrix,
                                     left_interp_indices=i1, right_interp_indices=i2)
        return res

MatrixVariateKernel

Bases: Kernel

Kernel supporting Kronecker style matrix variate Gaussian processes (where every data point is evaluated at every task).

Given a base covariance module to be used for the data, :math:K_{XX}, this kernel computes a task kernel of specified size :math:K_{TT} and returns :math:K = K_{TT} \otimes K_{XX}. as an :obj:gpytorch.lazy.KroneckerProductLazyTensor.

Parameters:

Name Type Description Default
task_covar_module

obj:gpytorch.kernels.IndexKernel): Kernel to use as the task kernel

required
data_covar_module

obj:gpytorch.kernels.Kernel): Kernel to use as the data kernel.

required
num_tasks int

Number of tasks

required
batch_size int

Set if the MultitaskKernel is operating on batches of data (and you want different parameters for each batch)

required
rank int

Rank of index kernel to use for task covariance matrix.

required
task_covar_prior

obj:gpytorch.priors.Prior): Prior to use for task kernel. See :class:gpytorch.kernels.IndexKernel for details.

required
Source code in bayes_cbf/matrix_variate_multitask_kernel.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class MatrixVariateKernel(Kernel):
    r"""
    Kernel supporting Kronecker style matrix variate Gaussian processes (where every
    data point is evaluated at every task).

    Given a base covariance module to be used for the data, :math:`K_{XX}`,
    this kernel computes a task kernel of specified size :math:`K_{TT}` and
    returns :math:`K = K_{TT} \otimes K_{XX}`. as an
    :obj:`gpytorch.lazy.KroneckerProductLazyTensor`.

    Args:
        task_covar_module (:obj:`gpytorch.kernels.IndexKernel`):
            Kernel to use as the task kernel
        data_covar_module (:obj:`gpytorch.kernels.Kernel`):
            Kernel to use as the data kernel.
        num_tasks (int):
            Number of tasks
        batch_size (int, optional):
            Set if the MultitaskKernel is operating on batches of data (and you
            want different parameters for each batch)
        rank (int):
            Rank of index kernel to use for task covariance matrix.
        task_covar_prior (:obj:`gpytorch.priors.Prior`):
            Prior to use for task kernel. See :class:`gpytorch.kernels.IndexKernel` for details."""
    @property
    def num_tasks(self):
        return prod(self.task_covar_module.matshape)

    def __init__(self, task_covar_module, data_covar_module, decoder, **kwargs):
        """
        """
        super().__init__(**kwargs)
        self.task_covar_module = task_covar_module
        self.data_covar_module = data_covar_module
        self.decoder = decoder