LangevinDynamics
Methods and Attributes¶
Bases: BaseSampler
Langevin Dynamics sampler implementing discretized gradient-based MCMC.
This class implements the Langevin Dynamics algorithm, a gradient-based MCMC method that samples from a target distribution defined by an energy function. It uses a stochastic update rule combining gradient descent with Gaussian noise to explore the energy landscape.
Each step updates the state \(x_t\) according to the discretized Langevin equation:
where \(\epsilon_t \sim \mathcal{N}(0, I)\) and \(\eta\) is the step size.
This process generates samples that asymptotically follow the Boltzmann distribution:
where \(U(x)\) defines the energy landscape.
Algorithm Summary
- If
x
is not provided, initialize it with Gaussian noise. - Iteratively update
x
fork_steps
usingself.langevin_step()
. - Optionally track trajectory (
return_trajectory=True
). - Optionally collect diagnostics such as mean, variance, and energy gradients.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
energy_function
|
BaseEnergyFunction
|
Energy function to sample from. |
required |
step_size
|
float
|
Step size for the Langevin update. |
0.001
|
noise_scale
|
float
|
Scale of the Gaussian noise. |
1.0
|
decay
|
float
|
Damping coefficient (not supported yet). |
0.0
|
dtype
|
dtype
|
Data type to use for the computations. |
float32
|
device
|
str
|
Device to run the computations on (e.g., "cpu" or "cuda"). |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
For invalid parameter ranges |
Methods:
Name | Description |
---|---|
langevin_step |
Perform a Langevin step. |
sample_chain |
Run the sampling process. |
_setup_diagnostics |
Initialize the diagnostics |
Basic Usage
# Define energy function
energy_fn = QuadraticEnergy(A=torch.eye(2), b=torch.zeros(2))
# Initialize sampler
sampler = LangevinDynamics(
energy_function=energy_fn,
step_size=0.01,
noise_scale=0.1
)
# Sample 100 points from 5 parallel chains
samples = sampler.sample_chain(
dim=2,
k_steps=50,
n_samples=100
)
Parameter Relationships
The effective temperature is controlled by: \(\text{Temperature} = \frac{\text{noise_scale}^2}{2 \cdot \text{step_size}}\) Adjust both parameters together to maintain constant temperature.
Source code in torchebm/samplers/langevin_dynamics.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
|
langevin_step
¶
Perform a single Langevin dynamics update step.
Implements the discrete Langevin equation:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prev_x
|
Tensor
|
Current state tensor of batch_shape (batch_size, dim) |
required |
noise
|
Tensor
|
Gaussian noise tensor of batch_shape (batch_size, dim) |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Updated state tensor of same batch_shape as prev_x |
Example
Source code in torchebm/samplers/langevin_dynamics.py
sample
¶
sample(x: Optional[Tensor] = None, dim: int = 10, n_steps: int = 100, n_samples: int = 1, thin: int = 1, return_trajectory: bool = False, return_diagnostics: bool = False, *args, **kwargs) -> Union[torch.Tensor, Tuple[torch.Tensor, List[dict]]]
Generate Markov chain samples using Langevin dynamics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Optional[Tensor]
|
Initial state to start the sampling from. |
None
|
dim
|
int
|
Dimension of the state space. |
10
|
n_steps
|
int
|
Number of steps to take between samples. |
100
|
n_samples
|
int
|
Number of samples to generate. |
1
|
return_trajectory
|
bool
|
Whether to return the trajectory of the samples. |
False
|
return_diagnostics
|
bool
|
Whether to return the diagnostics of the sampling process. |
False
|
Returns:
Type | Description |
---|---|
Union[Tensor, Tuple[Tensor, List[dict]]]
|
Final samples:
|
Raises:
Type | Description |
---|---|
ValueError
|
If input dimensions mismatch |
Note
- Automatically handles device placement (CPU/GPU)
- Uses mixed-precision training when available
- Diagnostics include:
- Mean and variance across dimensions
- Energy gradients
- Noise statistics
Example
Source code in torchebm/samplers/langevin_dynamics.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
|