Giter VIP home page Giter VIP logo

Comments (8)

kkyamada avatar kkyamada commented on June 29, 2024 1

Thank you for the response!
Changing the datatype of inputs for the GP heads from torch.float to torch.double solved the error!

from lambo.

samuelstanton avatar samuelstanton commented on June 29, 2024 1

I just pushed a commit that should make it much easier to change dtypes

In short, just change this line to torch.double

Hopefully this resolves your issue.

from lambo.

Thomaswbt avatar Thomaswbt commented on June 29, 2024 1

Thank you for your response! It's so nice of you to modify the code and that really helps! Changing torch.float to torch.double resolved my issue :)

from lambo.

kkyamada avatar kkyamada commented on June 29, 2024

When I checked the calculation process, it seems like torch.matmul has poor precision.
I manually changed the source code of gpytorch.priors.lkj_prior.LKJCovariancePrior.log_prob as follows.

    def log_prob(self, X):
        print("\n\nLKJCovariancePrior.log_prob, input:\n", X)
        marginal_var = torch.diagonal(X, dim1=-2, dim2=-1)
        print("\nLKJCovariancePrior.log_prob, marginal_var:\n", marginal_var)
        if not torch.all(marginal_var >= 0):
            raise ValueError("Variance(s) cannot be negative")
        marginal_sd = marginal_var.sqrt()
        print("\nLKJCovariancePrior.log_prob, marginal_sd:\n", marginal_sd)
        sd_diag_mat = _batch_form_diag(1 / marginal_sd)
        print("\nLKJCovariancePrior.log_prob, sd_diag_mat:\n", sd_diag_mat)
        correlations = torch.matmul(torch.matmul(sd_diag_mat, X), sd_diag_mat)
        print("\nLKJCovariancePrior.log_prob, corrs:\n", correlations, "\n")
        log_prob_corr = self.correlation_prior.log_prob(correlations)
        log_prob_sd = self.sd_prior.log_prob(marginal_sd)
        return log_prob_corr + log_prob_sd

and the output was the following.

LKJCovariancePrior.log_prob, input:
 tensor([[ 2.7795,  0.3485,  0.0401],
        [ 0.3485,  2.1543, -2.6390],
        [ 0.0401, -2.6390,  5.3450]], device='cuda:0')

LKJCovariancePrior.log_prob, marginal_var:
 tensor([2.7795, 2.1543, 5.3450], device='cuda:0')

LKJCovariancePrior.log_prob, marginal_sd:
 tensor([1.6672, 1.4677, 2.3119], device='cuda:0')

LKJCovariancePrior.log_prob, sd_diag_mat:
 tensor([[0.5998, 0.0000, 0.0000],
        [0.0000, 0.6813, 0.0000],
        [0.0000, 0.0000, 0.4325]], device='cuda:0')

LKJCovariancePrior.log_prob, corrs:
 tensor([[ 0.9990,  0.1424,  0.0104],
        [ 0.1424,  0.9998, -0.7774],
        [ 0.0104, -0.7776,  1.0004]], device='cuda:0') 

[2022-05-10 21:36:44,990][root][ERROR] - Input is not a valid correlation matrix

where its precise result must be

LKJCovariancePrior.log_prob, corrs:
 tensor([[ 1.0000,  0.1424,  0.0104],
        [ 0.1424,  1.0000, -0.7777],
        [ 0.0104, -0.7777,  1.0000]]) 

from lambo.

samuelstanton avatar samuelstanton commented on June 29, 2024

sorry for the delayed response

you're correct this is a numerical precision issue, though I'm surprised you're having problems, I've run this code many times and never seen this specific error. If you're sure this run wasn't a fluke you have a couple options

  • switch from single to double precision (if running on a GPU)
  • tweak GPyTorch settings (https://docs.gpytorch.ai/en/stable/settings.html)
  • dig into the source of numerical instability (if it happens in the middle of model training the learning rate may be a bit too aggressive)

from lambo.

samuelstanton avatar samuelstanton commented on June 29, 2024

glad to hear it! closing the issue.

from lambo.

Thomaswbt avatar Thomaswbt commented on June 29, 2024

@kkyamada Hello! May I ask what specific modifications you have made to the code to solve the numerical issues? I tried a bunch of ways to change the input of GP heads from torch.float to torch.double (e.g. in the gp_train_step function of gp_utils.py), but changing the tensor type would result in further errors in the gpytorch package (the message is "RuntimeError: expected scalar type Float but found Double"). How did you avoid this kind of type inconsistency? Thanks a lot in advance!

from lambo.

samuelstanton avatar samuelstanton commented on June 29, 2024

That's great!

from lambo.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.