question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with predict() in Implementing a custom kernel in GPyTorch

See original GitHub issue

Seems like predict() function in Implementing a custom kernel in GPyTorch should operate in eval mode. Otherwise, using predict() on on an untrained model (e.g. after torch.load() is used to load saved parameters) will give an error

should this:

def predict(model, likelihood, test_x=torch.linspace(0, 1, 51)):
    # Make predictions by feeding model through likelihood
    with torch.no_grad(), gpytorch.settings.fast_pred_var():
        # Test points are regularly spaced along [0,1]
        return likelihood(model(test_x))

be this (or better yet save the current eval state and return to model to the previous state

def predict(model, likelihood, test_x=torch.linspace(0, 1, 51)):
    #set to eval mode
    model.eval()
    # Make predictions by feeding model through likelihood
    with torch.no_grad(), gpytorch.settings.fast_pred_var():
        # Test points are regularly spaced along [0,1]
        return likelihood(model(test_x))

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gpleisscommented, Feb 18, 2021

@arthus701 yeah - it doesn’t hurt to have the model.eval() statement in the predict function. I’m making a few improvements to the docs today, so I can implement this change.

0reactions
arthus701commented, Feb 12, 2021

Maybe not required strictly speaking. But it seems like it would be helpful to get as many people up and running with the basics and to make the library as accessible as possible. If there is a good way to make a general predict function, why not include it? The tutorial did say that the functions were to allow reuse (or to that effect). The examples are really good overall, but I did spend some time scratching my because of side-issues like these. Ignore it if you don’t think others will appreciate the effort. It would have helped me.

What do you think @gpleiss? It wouldn’t be much work and I could certainly open a PR that implements the predict function the proposed way (i.e. with setting the model into eval mode inside of the function), but I do not want to carry the PRs for this example to excess.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing a custom kernel in GPyTorch
In this notebook we are looking at how to implement a custom kernel in GPyTorch. ... predictions by feeding model through likelihood with...
Read more >
gpytorch/Implementing_a_custom_Kernel.ipynb at master - GitHub
In this notebook we are looking at how to implement a custom kernel in GPyTorch. ... predictions by feeding model through likelihood with...
Read more >
Problem reproducing the predicted covariance of a gaussian ...
The idea is to train a GP using GPytorch, then take the learned hyperparameters, ... ConstantMean() self.covar_module = gpytorch.kernels.
Read more >
Gaussian Process Regression using GPyTorch
We'll use the Spectral Mixture (SM) kernel ( gpytorch.kernels.SpectralMixtureKernels() ) for this tutorial. A multivariate normal distribution: ...
Read more >
Gaussian Process Regression using GPyTorch
Building a scalable and flexible GP model using GPyTorch ... be applied to supervised learning problems like regression and classification.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found