Skip to main content

SGLD as PyTorch Optimizer

Project description

SGLD in PyTorch

PyPI version

This package implements SGLD and cSGLD as a PyTorch Optimizer.

Installation

Install from pip as:

pip install torch-sgld

To install the latest directly from source, run

pip install git+https://github.com/activatedgeek/torch-sgld.git

Usage

The general idea is to modify the usual gradient-based update loops in PyTorch with the SGLD optimizer.

from torch_sgld import SGLD

f = module()  ## construct PyTorch nn.Module.

sgld = SGLD(f.parameters(), lr=lr, momentum=.9)  ## Add momentum to make it SG-HMC.
sgld_scheduler = ## Optionally add a step-size scheduler.

for _ in range(num_steps):
    energy = f()
    energy.backward()

    sgld.step()

    sgld_scheduler.step()  ## Optional scheduler step.

cSGLD can be implemented by using a cyclical learning rate schedule. See the toy_csgld.ipynb notebook for a complete example.

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch-sgld-0.1.0.tar.gz (8.2 kB view hashes)

Uploaded Source

Built Distribution

torch_sgld-0.1.0-py3-none-any.whl (8.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page