https://github.com/GPflow/GPflow
Tip revision: 6f7f0d8bcbcaa499fa4477885e6c964468e6dcf9 authored by st-- on 07 October 2020, 15:58:01 UTC
Merge pull request #1593 from GPflow/develop
Merge pull request #1593 from GPflow/develop
Tip revision: 6f7f0d8
kufs.py
# Copyright 2017-2020 The GPflow Contributors. All Rights Reserved.
#
# 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.
import tensorflow as tf
from ..base import TensorLike
from ..inducing_variables import InducingPatches, InducingPoints, Multiscale
from ..kernels import Convolutional, Kernel, SquaredExponential
from .dispatch import Kuf
@Kuf.register(InducingPoints, Kernel, TensorLike)
def Kuf_kernel_inducingpoints(inducing_variable: InducingPoints, kernel: Kernel, Xnew):
return kernel(inducing_variable.Z, Xnew)
@Kuf.register(Multiscale, SquaredExponential, TensorLike)
def Kuf_sqexp_multiscale(inducing_variable: Multiscale, kernel: SquaredExponential, Xnew):
Xnew, _ = kernel.slice(Xnew, None)
Zmu, Zlen = kernel.slice(inducing_variable.Z, inducing_variable.scales)
idlengthscales = kernel.lengthscales + Zlen
d = inducing_variable._cust_square_dist(Xnew, Zmu, idlengthscales)
lengthscales = tf.reduce_prod(kernel.lengthscales / idlengthscales, 1)
lengthscales = tf.reshape(lengthscales, (1, -1))
return tf.transpose(kernel.variance * tf.exp(-0.5 * d) * lengthscales)
@Kuf.register(InducingPatches, Convolutional, object)
def Kuf_conv_patch(feat, kern, Xnew):
Xp = kern.get_patches(Xnew) # [N, num_patches, patch_len]
bigKzx = kern.base_kernel.K(feat.Z, Xp) # [M, N, P] -- thanks to broadcasting of kernels
Kzx = tf.reduce_sum(bigKzx * kern.weights if hasattr(kern, "weights") else bigKzx, [2])
return Kzx / kern.num_patches