From 2cb97dfb8a924929889f99b8ddf0d480e5cf9b03 Mon Sep 17 00:00:00 2001 From: Neil Ferguson Date: Thu, 9 Jul 2020 12:34:13 +0100 Subject: [PATCH 1/3] Added base_conditional_with_lm function, which accepts Lm instead of Kmm --- gpflow/conditionals/util.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/gpflow/conditionals/util.py b/gpflow/conditionals/util.py index 7d54739d7..0b59babcb 100644 --- a/gpflow/conditionals/util.py +++ b/gpflow/conditionals/util.py @@ -38,6 +38,27 @@ 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] @@ -58,7 +79,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"]), ] @@ -75,7 +96,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] From 85390c29d842a2992e2cbe31d5b669326b820f24 Mon Sep 17 00:00:00 2001 From: Neil Ferguson Date: Thu, 9 Jul 2020 12:36:53 +0100 Subject: [PATCH 2/3] Applied formatter --- gpflow/conditionals/util.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gpflow/conditionals/util.py b/gpflow/conditionals/util.py index 0b59babcb..496d47abd 100644 --- a/gpflow/conditionals/util.py +++ b/gpflow/conditionals/util.py @@ -39,8 +39,9 @@ def base_conditional( :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) + 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( From b38f351068cf2a4c054e5f2f993c27e8f96dfa0a Mon Sep 17 00:00:00 2001 From: Neil Ferguson Date: Mon, 3 Aug 2020 14:50:08 +0100 Subject: [PATCH 3/3] Fix a typo --- gpflow/conditionals/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpflow/conditionals/util.py b/gpflow/conditionals/util.py index 496d47abd..0a6ee4ecf 100644 --- a/gpflow/conditionals/util.py +++ b/gpflow/conditionals/util.py @@ -65,7 +65,7 @@ def base_conditional_with_lm( 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(