-
Notifications
You must be signed in to change notification settings - Fork 49
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
Decouple kernel computation class initialisation from kernel #328
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b29728
decouple kernel init and computations
frazane bba25c8
Merge branch 'main' into decouple-kernel-computation
frazane f1c7bb4
pass all tests
frazane 875a51c
update docstrings
frazane b8a4442
make angry bear happy
frazane 6605038
add missing docstring
frazane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,76 @@ | ||
from dataclasses import dataclass | ||
import typing as tp | ||
|
||
import jax.numpy as jnp | ||
from jaxtyping import Float | ||
from jaxtyping import ( | ||
Array, | ||
Float, | ||
) | ||
|
||
from gpjax.kernels.computations.base import AbstractKernelComputation | ||
from gpjax.linops import DenseLinearOperator | ||
from gpjax.typing import Array | ||
|
||
Kernel = tp.TypeVar("Kernel", bound="gpjax.kernels.base.AbstractKernel") # noqa: F821 | ||
|
||
|
||
@dataclass | ||
class BasisFunctionComputation(AbstractKernelComputation): | ||
r"""Compute engine class for finite basis function approximations to a kernel.""" | ||
|
||
num_basis_fns: int = None | ||
|
||
def cross_covariance( | ||
self, x: Float[Array, "N D"], y: Float[Array, "M D"] | ||
self, kernel: Kernel, x: Float[Array, "N D"], y: Float[Array, "M D"] | ||
) -> Float[Array, "N M"]: | ||
r"""Compute an approximate cross-covariance matrix. | ||
|
||
For a pair of inputs, compute the cross covariance matrix between the inputs. | ||
|
||
Args: | ||
kernel (Kernel): the kernel function. | ||
x: (Float[Array, "N D"]): A $`N \times D`$ array of inputs. | ||
y: (Float[Array, "M D"]): A $`M \times D`$ array of inputs. | ||
|
||
Returns: | ||
Float[Array, "N M"]: A $N \times M$ array of cross-covariances. | ||
""" | ||
z1 = self.compute_features(x) | ||
z2 = self.compute_features(y) | ||
return self.scaling * jnp.matmul(z1, z2.T) | ||
z1 = self.compute_features(kernel, x) | ||
z2 = self.compute_features(kernel, y) | ||
return self.scaling(kernel) * jnp.matmul(z1, z2.T) | ||
|
||
def gram(self, inputs: Float[Array, "N D"]) -> DenseLinearOperator: | ||
def gram(self, kernel: Kernel, inputs: Float[Array, "N D"]) -> DenseLinearOperator: | ||
r"""Compute an approximate Gram matrix. | ||
|
||
For the Gram matrix, we can save computations by computing only one matrix | ||
multiplication between the inputs and the scaled frequencies. | ||
|
||
Args: | ||
kernel (Kernel): the kernel function. | ||
inputs (Float[Array, "N D"]): A $`N x D`$ array of inputs. | ||
|
||
Returns: | ||
DenseLinearOperator: A dense linear operator representing the | ||
$`N \times N`$ Gram matrix. | ||
""" | ||
z1 = self.compute_features(inputs) | ||
return DenseLinearOperator(self.scaling * jnp.matmul(z1, z1.T)) | ||
z1 = self.compute_features(kernel, inputs) | ||
return DenseLinearOperator(self.scaling(kernel) * jnp.matmul(z1, z1.T)) | ||
|
||
def compute_features(self, x: Float[Array, "N D"]) -> Float[Array, "N L"]: | ||
def compute_features( | ||
self, kernel: Kernel, x: Float[Array, "N D"] | ||
) -> Float[Array, "N L"]: | ||
r"""Compute the features for the inputs. | ||
|
||
Args: | ||
kernel (Kernel): the kernel function. | ||
x (Float[Array, "N D"]): A $`N \times D`$ array of inputs. | ||
|
||
Returns | ||
------- | ||
Float[Array, "N L"]: A $`N \times L`$ array of features where $`L = 2M`$. | ||
""" | ||
frequencies = self.kernel.frequencies | ||
scaling_factor = self.kernel.base_kernel.lengthscale | ||
frequencies = kernel.frequencies | ||
scaling_factor = kernel.base_kernel.lengthscale | ||
z = jnp.matmul(x, (frequencies / scaling_factor).T) | ||
z = jnp.concatenate([jnp.cos(z), jnp.sin(z)], axis=-1) | ||
return z | ||
|
||
@property | ||
def scaling(self): | ||
return self.kernel.base_kernel.variance / self.kernel.num_basis_fns | ||
def scaling(self, kernel): | ||
return kernel.base_kernel.variance / kernel.num_basis_fns | ||
frazane marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works with beartype but static type checkers complain about this way of annotating the bound, but all alternative solutions I tried resulted in circular imports errors at runtime type checking as explained in #293 (comment).