Git Product home page Git Product logo

Comments (4)

gpleiss avatar gpleiss commented on June 12, 2024 2

Actually - I ported your example to work on the alpha_release branch:

import gpytorch
import torch
from gpytorch.kernels import RBFKernel, AdditiveGridInterpolationKernel
from gpytorch.likelihoods import GaussianLikelihood
from gpytorch.means import ConstantMean
from gpytorch.random_variables import GaussianRandomVariable
from torch import nn
from torch.autograd import Variable


class GP(gpytorch.models.ExactGP):
    def __init__(self, train_x, train_y, likelihood):
        super(GP, self).__init__(train_x, train_y, likelihood)
        self.embedding = nn.Embedding(10, 2)
        self.mean_module = ConstantMean(constant_bounds=[-1e-5, 1e-5])
        self.base_covar_module = RBFKernel(log_lengthscale_bounds=(-5, 6))
        self.covar_module = AdditiveGridInterpolationKernel(self.base_covar_module,
                                                            grid_size=100, grid_bounds=[(-10, 10)], n_components=2)
        self.register_parameter('log_outputscale', nn.Parameter(torch.Tensor([0])), bounds=(-5, 6))

    def forward(self, x):
        embs = self.embedding(x)
        mean_embs = self.mean_module(embs)
        covar_embs = self.covar_module(embs)
        covar_embs = covar_embs.mul(self.log_outputscale.exp())
        latent_pred = GaussianRandomVariable(mean_embs, covar_embs)
        return latent_pred


class GPRegressionModel(gpytorch.Module):
    def __init__(self, train_x, train_y):
        super(GPRegressionModel, self).__init__()
        self.likelihood = GaussianLikelihood()
        self.gp = GP(train_x, train_y, self.likelihood)

    def forward(self, x):
        return self.likelihood(self.gp(x))


if __name__ == '__main__':
    n = 10
    train_x = (torch.rand(n) * 10).type(torch.LongTensor)
    train_x = Variable(train_x)
    train_y = torch.randn(n)
    train_y = Variable(train_y)

    model = GPRegressionModel(train_x.data, train_y.data)
    output = model(train_x)
    loss = -model.gp.marginal_log_likelihood(model.likelihood, output, train_y)
    loss.backward()
    print('Embedding has gradients!', model.gp.embedding.weight.grad)

"""
Output:
('Embedding has gradients!', Variable containing:
 0.0000  0.0000
 0.0000  0.0000
 0.0000  0.0000
 0.0175 -0.0341
 0.0000  0.0000
-0.0136  0.0151
-0.0094  0.0722
 0.0290 -0.0124
 0.0907  0.0171
-0.0657 -0.1519
[torch.FloatTensor of size 10x2]
)
"""

Note that the nn.Embeddings layer has to live inside the GP module.

from gpytorch.

gpleiss avatar gpleiss commented on June 12, 2024 1

Alright, I reproduced your issue, and I think I have pinpointed the problem. Some of our interpolation code is not computing gradients, which then prevents embeddings from getting gradients.

Hopefully we'll have a fix for this soon.

from gpytorch.

gpleiss avatar gpleiss commented on June 12, 2024

Hmmm you should be getting gradients! I'll look into this.

from gpytorch.

gpleiss avatar gpleiss commented on June 12, 2024

Okay - we have a fix for this on the alpha_release branch.

The interface has changed slightly on this branch, so you'll probably have to update your model a bit.
Could you test out your model on the alpha_release branch and see if it works?

from gpytorch.

Related Issues (20)

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.