Skip to content

Getting Started with TorchEBM

Welcome to the TorchEBM tutorials section! These comprehensive tutorials will help you understand how to use TorchEBM effectively for your energy-based modeling tasks.

Core Concepts

Quick Start

If you're new to energy-based models, we recommend the following learning path:

  1. Follow the Installation/Introduction guide to set up TorchEBM and understand basic concepts
  2. Read the Energy Models API to understand model implementations
  3. Explore the Samplers guide to learn how to generate samples
  4. Study the Training guide to learn how to train your models

Basic Example

Here's a simple example to get you started with TorchEBM:

import torch
from torchebm.core import GaussianModel
from torchebm.samplers.langevin_dynamics import LangevinDynamics

# Create an energy model (2D Gaussian)
energy_fn = GaussianModel(
    mean=torch.zeros(2),
    cov=torch.eye(2)
)

# Create a sampler
sampler = LangevinDynamics(
    energy_function=energy_fn,
    step_size=0.01
)

# Generate samples
samples = sampler.sample(
    dim=2, n_steps=100, n_samples=1000
)

# Print sample statistics
print(f"Sample mean: {samples.mean(0)}")
print(f"Sample std: {samples.std(0)}")

Common Patterns

Here are some common patterns you'll encounter throughout the guides:

Energy Model Definition

from torchebm.core import BaseModel
import torch


class MyEnergyModel(BaseModel):
    def __init__(self):
        super().__init__()

    def forward(self, x):
        return torch.sum(x ** 2, dim=-1)

Sampler Usage

from torchebm.samplers.langevin_dynamics import LangevinDynamics

sampler = LangevinDynamics(
    energy_function=energy_fn,
    step_size=0.01
)

samples = sampler.sample(
    dim=2, n_steps=100, n_samples=1000
)

Next Steps

Once you're familiar with the basics, you can:

Remember that all examples in these guides are tested with the latest version of TorchEBM, and you can run them in your own environment to gain hands-on experience.