Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base_conditional_with_lm function #1528

Merged
merged 7 commits into from
Aug 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 24 additions & 3 deletions gpflow/conditionals/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,34 @@ def base_conditional(
:param white: bool
:return: [N, R] or [R, N, N]
"""
Lm = tf.linalg.cholesky(Kmm)
return base_conditional_with_lm(
Kmn=Kmn, Lm=Lm, Knn=Knn, f=f, full_cov=full_cov, q_sqrt=q_sqrt, white=white
)


def base_conditional_with_lm(
Kmn: tf.Tensor,
Lm: tf.Tensor,
Knn: tf.Tensor,
f: tf.Tensor,
*,
full_cov=False,
q_sqrt: Optional[tf.Tensor] = None,
white=False,
):
r"""
Has the same functionality as the `base_conditional` function, except that instead of
`Kmm` this function accepts `Lm`, which is the Cholesky decomposition of `Kmm`.

This allows `Lm` to be precomputed, which can improve performance.
"""
# compute kernel stuff
num_func = tf.shape(f)[-1] # R
N = tf.shape(Kmn)[-1]
M = tf.shape(f)[-2]

# get the leadings dims in Kmn to the front of the tensor
# get the leading dims in Kmn to the front of the tensor
# if Kmn has rank two, i.e. [M, N], this is the identity op.
K = tf.rank(Kmn)
perm = tf.concat(
Expand All @@ -58,7 +80,7 @@ def base_conditional(

shape_constraints = [
(Kmn, [..., "M", "N"]),
(Kmm, ["M", "M"]),
(Lm, ["M", "M"]),
(Knn, [..., "N", "N"] if full_cov else [..., "N"]),
(f, ["M", "R"]),
]
Expand All @@ -75,7 +97,6 @@ def base_conditional(
)

leading_dims = tf.shape(Kmn)[:-2]
Lm = tf.linalg.cholesky(Kmm) # [M, M]

# Compute the projection matrix A
Lm = tf.broadcast_to(Lm, tf.concat([leading_dims, tf.shape(Lm)], 0)) # [..., M, M]
Expand Down