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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Generic typing for scale kernels #2491

Open
TobyBoyne opened this issue Mar 6, 2024 · 0 comments
Open

[Feature Request] Generic typing for scale kernels #2491

TobyBoyne opened this issue Mar 6, 2024 · 0 comments

Comments

@TobyBoyne
Copy link

馃殌 Feature Request

Improve type hinting - using generic types - for kernels that operate on other kernels (such as the ScaleKernel).

Motivation

Is your feature request related to a problem? Please describe.
Currently, after building a model following the Simple GP Regression tutorial, the type hint for the base kernel is not fully expressive.

model = ExactGPModel(train_x, train_y, likelihood)
model.covar_module.base_kernel  
# Pylance tells me this has type "Kernel"
# I know the type is actually "RBFKernel"

This isn't too much of a problem with the RBFKernel, which uses the lengthscale parameter that belongs to the base class. However, when you start to work with non-standard kernels, that have different attributes, then it can become annoying to lose the type hints.

Pitch

Describe the solution you'd like
Use Generic types to implement this. It would look something like:

from typing import TypeVar, Generic

KernelType = TypeVar("KernelType", bound=Kernel)

class ScaleKernel(Kernel, Generic[KernelType]):
    def __init__(self, base_kernel: KernelType, ...):
        ....

kernel = ScaleKernel(RBFKernel())
kernel.base_kernel
# Pylance tells me this is an "RBFKernel"!

Describe alternatives you've considered
I'm not sure there are any alternatives? One might argue that the type hinting isn't that important, but I think the modern dev experience uses a lot of autocomplete (for me at least), and this would make that a lot smoother.

Are you willing to open a pull request?
Yes :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant