GithubHelp home page GithubHelp logo

nonconvexopt / pytorch-ssge Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 157 KB

Unofficial PyTorch Reimplementation of the Spectral Stein Gradient Estimator, compatiable to use gpytorch kernel modules.

License: MIT License

Python 12.43% Jupyter Notebook 87.57%
gpytorch pytorch

pytorch-ssge's People

Contributors

nonconvexopt avatar

Watchers

 avatar

pytorch-ssge's Issues

Issue with simple Gaussian distribution

Hi,

First of all thanks for this wonderful implementation.

I am now trying to work a simple example of using SSGE for estimating score function for N(0, 1) function. Please find below my code:

import torch
import gpytorch
from ssge import SSGE

def test_gaussian():
    import matplotlib.pyplot as plt
    plt.style.use('seaborn')

    dist = torch.distributions.normal.Normal(
        torch.tensor(0.0), torch.tensor(1.0)
    )

    kernel = gpytorch.kernels.RBFKernel()

    # estimator = SSGE(gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel()))
    estimator = SSGE(
        gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel()), 
        noise=1e-4
    )

    sample = dist.sample((100, 1))
    logp = dist.log_prob(sample)
    sample.requires_grad_(True)
    estimator.fit(sample)


    # test_points = dist.sample((25, 1))
    test_points = torch.linspace(-2, +2, 250)
    test_points.requires_grad_(True)
    grad_the = -test_points
    grad_est = estimator(test_points)

    # Plot results
    fig, ax = plt.subplots(1, 1)
    ax.scatter(sample.detach().numpy(), logp.detach().numpy(),
               marker='o', s=2.0, label='Sample points')
    ax.plot(test_points.detach().numpy(), grad_the.detach().numpy(),
            c='red', lw=1, label=r"$\nabla_x")
    ax.plot(test_points.detach().numpy(), grad_est.detach().numpy(),
            c='blue', lw=1,  label=r"$\hat{\nabla_x}")
    plt.savefig("out/test_gaussian.png")

test_gaussian()

However, when I run the example, the estimated gradients are very much different than the actual ones. (See the figure attached)

How this can be improved?

test_gaussian

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.