Langevin dynamics is a powerful sampling technique that allows us to draw samples from complex probability distributions. In this tutorial, we'll explore how to use TorchEBM's implementation of Langevin dynamics for sampling from various energy landscapes.
importtorchimportmatplotlib.pyplotaspltfromtorchebm.coreimportGaussianEnergyfromtorchebm.samplers.langevin_dynamicsimportLangevinDynamics# Create energy function for a 2D Gaussiandevice=torch.device("cuda"iftorch.cuda.is_available()else"cpu")dim=2# dimension of the state spacen_steps=100# steps between samplesn_samples=1000# num of samplesmean=torch.tensor([1.0,-1.0])cov=torch.tensor([[1.0,0.5],[0.5,2.0]])energy_fn=GaussianEnergy(mean,cov,device=device)# Initialize samplersampler=LangevinDynamics(energy_function=energy_fn,step_size=0.01,noise_scale=0.1,device=device,)# Generate samplesinitial_state=torch.zeros(n_samples,dim,device=device)samples=sampler.sample_chain(x=initial_state,n_steps=n_steps,n_samples=n_samples,)# Plot resultssamples=samples.cpu().numpy()plt.figure(figsize=(10,5))plt.scatter(samples[:,0],samples[:,1],alpha=0.1)plt.title("Samples from 2D Gaussian using Langevin Dynamics")plt.xlabel("x₁")plt.ylabel("x₂")plt.show()
fromtorchebm.coreimportDoubleWellEnergy# Create energy function and samplerdevice=torch.device("cuda"iftorch.cuda.is_available()else"cpu")energy_fn=DoubleWellEnergy(barrier_height=2.0)sampler=LangevinDynamics(energy_function=energy_fn,step_size=0.001,noise_scale=0.1,decay=0.1,# for stabilitydevice=device,)# Generate trajectory with diagnosticsinitial_state=torch.tensor([0.0],device=device)trajectory,diagnostics=sampler.sample(x=initial_state,n_steps=1000,return_trajectory=True,return_diagnostics=True,)# Plot resultsfig,(ax1,ax2)=plt.subplots(1,2,figsize=(15,5))# Plot trajectoryax1.plot(trajectory[0,:,0].cpu().numpy())ax1.set_title("Single Chain Trajectory")ax1.set_xlabel("Step")ax1.set_ylabel("Position")# Plot energy over timeax2.plot(diagnostics[:,2,0,0].cpu().numpy())ax2.set_title("Energy Evolution")ax2.set_xlabel("Step")ax2.set_ylabel("Energy")plt.tight_layout()plt.show()
Key Benefits of TorchEBM's Langevin Dynamics Implementation¶
GPU Acceleration - Sampling is performed efficiently on GPUs when available
Flexible API - Easy to use with various energy functions and initialization strategies
Diagnostic Tools - Track energy, gradient norms, and acceptance rates during sampling
Configurable Parameters - Fine-tune step size, noise scale, and decay for optimal performance
Langevin dynamics is a versatile sampling method for energy-based models, and TorchEBM makes it easy to use in your projects. Whether you're sampling from simple analytical distributions or complex neural network energy functions, the same API works seamlessly.
Stay tuned for more tutorials on other samplers and energy functions!