MCMC + Diffusion Sampling
Denoising Diffusion Probabilistic Models
Imports / Setup
from __future__ import absolute_import, print_function, annotations, division
from dataclasses import dataclass
import sys
import os
import math
import numpy as np
import scipy
import time
from random import randrange
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from ezpz.dist import setup_torch
= np.random.randint(5000, 6000)
port print(f"Using port: {port}")
= setup_torch(
RANK ="DDP",
backend=f"{port}"
port )
Using port: 5561
Using DDP for distributed training
Global Rank: 0 / 0
:::
%matplotlib inline
import matplotlib_inline
'svg')
matplotlib_inline.backend_inline.set_matplotlib_formats(
from l2hmc.main import build_experiment
from l2hmc.utils.rich import get_console
from l2hmc.utils.plot_helpers import set_plot_style
import opinionated
from l2hmc.diffusion.diffusion import PureMH, MH_Diffusion
from l2hmc.utils.plot_helpers import set_plot_style
from pandas.io.formats import style
import scipy
import time
from random import randrange
from l2hmc.diffusion.diffusion import PureMH, MH_Diffusion
set_plot_style()= get_console()
console print(console.is_jupyter)
if console.is_jupyter:
= False
console.is_jupyter print(console.is_jupyter)
Using device: cpu
Failed to download font: Source Sans Pro, skipping! Failed to download font: Titillium WebRoboto Condensed, skipping!
True
False
'opinionated_min'])
plt.style.use(opinionated.STYLES['notebook') sns.set_context(
2D U(1)
from l2hmc.configs import dict_to_list_of_overrides
= np.random.randint(0, 2**32)
seed print(f"seed = {seed}")
console.
= {
overrides "seed": f"{seed}",
"precision": "float32",
"init_wandb": False,
"init_aim": False,
"use_wandb": False,
"dynamics": {
"latvolume": [32, 32],
"nleapfrog": 10,
"nchains": 16,
"eps": 0.05,
},"network": {
"use_batch_norm": False,
},'annealing_schedule': {
'beta_init': 6.0,
'beta_final': 6.0,
},
}= dict_to_list_of_overrides(overrides) OVERRIDES
seed = 1675333995
from pathlib import Path
from l2hmc.common import get_timestamp
from enrich.console import get_theme, Console
= Console(theme=get_theme())
console
= Path(
OUTDIR 'l2hmc-diffusion-2dU1'
"%Y-%m-%d"))
).joinpath(get_timestamp(=True, parents=True)
OUTDIR.mkdir(exist_okprint(f"OUTDIR: {OUTDIR}")
console.
= get_timestamp('%Y-%m-%d')
date = OUTDIR.joinpath('plots')
PLOTS_DIR =True, parents=True)
PLOTS_DIR.mkdir(exist_okprint(f"Saving figures to: {PLOTS_DIR}") console.
OUTDIR: l2hmc-diffusion-2dU1/2023-09-21
Saving figures to: l2hmc-diffusion-2dU1/2023-09-21/plots
#os.environ['MASTER_PORT'] = '5436'
= build_experiment(
exp =[
overrides*OVERRIDES,
'framework=pytorch',
'backend=DDP'
] )
[09/21/23 12:23:55][INFO][dist.py:226] - Caught MASTER_PORT:5561 from environment! [09/21/23 12:23:55][INFO][dist.py:338] - Global Rank: 0 / 0 [09/21/23 12:23:58][INFO][experiment.py:251] - Creating outputs/2023-09-21-122358/pytorch/train [09/21/23 12:23:58][INFO][experiment.py:251] - Creating outputs/2023-09-21-122358/pytorch/eval [09/21/23 12:23:58][INFO][experiment.py:251] - Creating outputs/2023-09-21-122358/pytorch/hmc [09/21/23 12:23:58][INFO][dist.py:226] - Caught MASTER_PORT:5561 from environment! [09/21/23 12:23:58][INFO][dist.py:226] - Caught MASTER_PORT:5561 from environment! [09/21/23 12:24:06][INFO][trainer.py:441] - Looking for checkpoints in: /Users/samforeman/projects/saforem2/l2hmc-qcd/src/l2hmc/checkpoints/U1/2-32-32/nlf-10/xsplit-True/sepnets-True/merge-True/conv-8-16-32-64-128_5-3-3-3-2_2-2-2-2-2/net-16-16-16-16_dp-0.2_bn-False/pytorch [09/21/23 12:24:06][WARNING][trainer.py:437] - No checkpoints found to load from [09/21/23 12:24:06][WARNING][trainer.py:437] - Restoring global step from ckpt! self._gstep: 0 [09/21/23 12:24:06][WARNING][trainer.py:437] - Using `torch.optim.Adam` optimizer [09/21/23 12:24:06][INFO][trainer.py:284] - num_params in model: 958628260 [09/21/23 12:24:09][WARNING][trainer.py:250] - logging with freq 50 for wandb.watch
= exp.trainer.dynamics.random_state(6.0)
state = state.x.flatten().shape[0]
xdim
= xdim
dim = (-np.pi) * np.ones(dim)
low_bound = (np.pi) * np.ones(dim)
high_bound = 0.15
sigma = 10
retrains = 100
samples_per_retrain = 0.1 diffusion_prob
'notebook')
sns.set_context(
= {}
outputs 'hmc'] = exp.trainer.eval(
outputs[='hmc',
job_type=6.0,
beta=100,
nprint=16,
nchains=1000
eval_steps
)#hdset = exp.save_dataset(job_type='hmc', nchains=1)
[09/21/23 12:24:21][WARNING][trainer.py:437] - Step size `eps` not specified for HMC! Using default: 0.1000 for generic HMC
[09/21/23 12:24:21][WARNING][trainer.py:437] - x.shape (original): torch.Size([16, 2, 32, 32])
[09/21/23 12:24:21][WARNING][trainer.py:437] - x[:nchains].shape: torch.Size([16, 2, 32, 32])
[09/21/23 12:24:21][INFO][trainer.py:1058] - eps=0.1
beta=6.0
nlog=10
table=<rich.table.Table object at 0x2e1b98520>
nprint=100
eval_steps=1000
nleapfrog=20
[09/21/23 12:24:24][INFO][trainer.py:1188] - hstep=0 dt=0.024 beta=6.000 loss=3.410 dQsin=0.125 dQint=0.000 energy=1586.502 logprob=1586.502 logdet=0.000 acc=0.472 sumlogdet=0.000 acc_mask=0.500 plaqs=0.909 intQ=0.000 sinQ=0.051
[09/21/23 12:24:27][INFO][trainer.py:1188] - hstep=100 dt=0.026 beta=6.000 loss=2.876 dQsin=0.163 dQint=0.000 energy=1555.800 logprob=1555.800 logdet=0.000 acc=0.593 sumlogdet=0.000 acc_mask=0.688 plaqs=0.912 intQ=-0.125 sinQ=-0.159
[09/21/23 12:24:31][INFO][trainer.py:1188] - hstep=200 dt=0.025 beta=6.000 loss=4.678 dQsin=0.088 dQint=0.063 energy=1569.994 logprob=1569.994 logdet=0.000 acc=0.451 sumlogdet=0.000 acc_mask=0.250 plaqs=0.912 intQ=-0.187 sinQ=-0.149
[09/21/23 12:24:34][INFO][trainer.py:1188] - hstep=300 dt=0.024 beta=6.000 loss=14.041 dQsin=0.094 dQint=0.000 energy=1554.118 logprob=1554.118 logdet=0.000 acc=0.438 sumlogdet=0.000 acc_mask=0.438 plaqs=0.914 intQ=-0.125 sinQ=-0.114
[09/21/23 12:24:38][INFO][trainer.py:1188] - hstep=400 dt=0.024 beta=6.000 loss=-0.739 dQsin=0.199 dQint=0.000 energy=1566.516 logprob=1566.516 logdet=0.000 acc=0.509 sumlogdet=0.000 acc_mask=0.562 plaqs=0.912 intQ=-0.437 sinQ=-0.452
[09/21/23 12:24:41][INFO][trainer.py:1188] - hstep=500 dt=0.045 beta=6.000 loss=1.545 dQsin=0.100 dQint=0.000 energy=1570.837 logprob=1570.837 logdet=0.000 acc=0.448 sumlogdet=0.000 acc_mask=0.562 plaqs=0.911 intQ=0.125 sinQ=0.189
[09/21/23 12:24:45][INFO][trainer.py:1188] - hstep=600 dt=0.025 beta=6.000 loss=3.780 dQsin=0.094 dQint=0.000 energy=1568.012 logprob=1568.012 logdet=0.000 acc=0.463 sumlogdet=0.000 acc_mask=0.500 plaqs=0.913 intQ=0.438 sinQ=0.466
[09/21/23 12:24:50][INFO][trainer.py:1188] - hstep=700 dt=0.023 beta=6.000 loss=-0.902 dQsin=0.113 dQint=0.000 energy=1563.778 logprob=1563.778 logdet=0.000 acc=0.475 sumlogdet=0.000 acc_mask=0.375 plaqs=0.913 intQ=0.688 sinQ=0.628
[09/21/23 12:24:53][INFO][trainer.py:1188] - hstep=800 dt=0.024 beta=6.000 loss=11.416 dQsin=0.061 dQint=0.000 energy=1561.427 logprob=1561.427 logdet=0.000 acc=0.339 sumlogdet=0.000 acc_mask=0.438 plaqs=0.913 intQ=0.813 sinQ=0.755
[09/21/23 12:24:57][INFO][trainer.py:1188] - hstep=900 dt=0.028 beta=6.000 loss=1.114 dQsin=0.127 dQint=0.000 energy=1564.465 logprob=1564.465 logdet=0.000 acc=0.699 sumlogdet=0.000 acc_mask=0.625 plaqs=0.913 intQ=0.938 sinQ=0.893
# %matplotlib inline
from l2hmc.common import plot_dataset
'notebook')
sns.set_context(= outputs['hmc']['history'].get_dataset()
hdataset =PLOTS_DIR, job_type='HMC') plot_dataset(hdataset, outdir
[09/21/23 12:25:06][INFO][plot_helpers.py:1049] - Saving figure to: l2hmc-diffusion-2dU1/2023-09-21/plots/ridgeplots/svgs/energy_ridgeplot.svg
[09/21/23 12:25:09][INFO][plot_helpers.py:1049] - Saving figure to: l2hmc-diffusion-2dU1/2023-09-21/plots/ridgeplots/svgs/logprob_ridgeplot.svg
[09/21/23 12:25:11][INFO][plot_helpers.py:1049] - Saving figure to: l2hmc-diffusion-2dU1/2023-09-21/plots/ridgeplots/svgs/logdet_ridgeplot.svg
import torch
= []
initial_states = exp.trainer.dynamics.random_state(6.0)
state_init = state_init.x
x = state_init.beta
beta
= 1000
NSAMPLES for idx in range(NSAMPLES + int(0.1 * NSAMPLES)):
if idx % 100 == 0:
print(f"step: {idx}")
console.
= exp.trainer.hmc_step((x, beta))
x, metrics if idx > int((0.1 * NSAMPLES)):
initial_states.append(x)
= torch.stack(initial_states).squeeze()
initial_states = initial_states.detach().cpu().numpy() initial_states_np
step: 0
step: 100
step: 200
step: 300
step: 400
step: 500
step: 600
step: 700
step: 800
step: 900
step: 1000
initial_states_np.shape
(999, 16, 2048)
= initial_states_np.reshape(-1, 16, 2, 32, 32)
x_ = x_[:, 0, ...]
tmp_ print(f'{x_.shape}')
console.print(f'{tmp_.shape}') console.
(999, 16, 2, 32, 32)
(999, 2, 32, 32)
from l2hmc.common import savefig
#x_ = initial_states_np[:100].reshape(-1, 2, 32, 32)
= x_[:, 0, ...]
tmp_ = plt.subplots()
fig, ax
sns.kdeplot(=tmp_[-100:, 0].flatten(),
x=tmp_[-100:, 1].flatten(),
y# ax=ax,
='viridis',
cmap# ax=axes[0],
# cmap="Blues",
=False,
shade# bw_adjust=0.5,
=0
thresh
)-4, 4))
ax.set_xlim((-4, 4))
ax.set_ylim((
savefig(f'hmc_samples-{NSAMPLES}',
Path(PLOTS_DIR),=True,
tstamp )
Saving hmc_samples-1000-2023-09-21-122840 to l2hmc-diffusion-2dU1/2023-09-21/plots
class Diffusion:
def __init__(
self,
int = 1000,
noise_steps: float = 1e-4,
beta_start: float = 0.02,
beta_end: int = 2,
nchannels: int = 256,
img_size: str = "cuda"
device:
):self.noise_steps = noise_steps
self.beta_start = beta_start
self.beta_end = beta_end
self.img_size = img_size
self.device = device
self.nchannels = nchannels
self.beta = self.prepare_noise_schedule().to(device)
self.alpha = 1. - self.beta
self.alpha_hat = torch.cumprod(self.alpha, dim=0)
def prepare_noise_schedule(self):
return torch.linspace(self.beta_start, self.beta_end, self.noise_steps)
def noise_images(self, x, t):
= torch.sqrt(self.alpha_hat[t])[:, None, None, None]
sqrt_alpha_hat = torch.sqrt(
sqrt_one_minus_alpha_hat 1 - self.alpha_hat[t]
None, None, None]
)[:, = torch.randn_like(x)
eps return sqrt_alpha_hat * x + sqrt_one_minus_alpha_hat * eps, eps
def sample_timesteps(self, n):
return torch.randint(low=1, high=self.noise_steps, size=(n,))
def sample(self, model, n):
# console.print(f"Sampling {n} new images....")
eval()
model.with torch.no_grad():
= torch.randn(
x self.nchannels, self.img_size, self.img_size)
(n, self.device)
).to(= tqdm(
sample_bar reversed(range(1, self.noise_steps)),
=0,
position=self.noise_steps - 1,
total=True,
dynamic_ncols
)for i in sample_bar:
= (torch.ones(n) * i).long().to(self.device)
t = model(x, t)
predicted_noise = self.alpha[t][:, None, None, None]
alpha = self.alpha_hat[t][:, None, None, None]
alpha_hat = self.beta[t][:, None, None, None]
beta if i > 1:
= torch.randn_like(x)
noise else:
= torch.zeros_like(x)
noise = (
x 1 / torch.sqrt(alpha))
(* (
x - ((1 - alpha) / (torch.sqrt(1 - alpha_hat)))
* predicted_noise
) + (torch.sqrt(beta) * noise)
)
model.train()= (x + np.pi) % (2 * np.pi) - np.pi
x return x
initial_states.shape
torch.Size([999, 16, 2048])
Train Diffusion Model
import torchvision
import os
import random
from pathlib import Path
import torch
import torchvision
import torchvision.transforms as T
import numpy as np
from PIL import Image
#from fastdownload import FastDownload
from torch.utils.data import DataLoader
def save_images(images, path, **kwargs):
= torchvision.utils.make_grid(images, **kwargs)
grid = grid.permute(1, 2, 0).to('cpu').numpy()
ndarr = Image.fromarray(ndarr)
im im.save(path)
Build Diffusion Model with UNet Architecure
from torch import nn
from torch import optim
import torch.nn.functional as F
from tqdm.auto import tqdm
from torch.utils.data import DataLoader
from torch.utils.data import TensorDataset
from l2hmc.common import savefig
from l2hmc.diffusion.modules import NoiseScheduler, UNet
from l2hmc.diffusion import ddpm
= 'cuda' if torch.cuda.is_available() else 'cpu'
DEVICE
= {
config 'channels_in': 2,
'channels_out': 2,
'train_batch_size': 5,
'learning_rate': 0.001,
'num_epochs': 1,
'noise_steps': 100,
'beta': 6.0,
'img_size': 32,
'retrains': 10,
'samples_per_retrain': 500,
'diffusion_prob': 0.1,
}
= UNet(c_in=2, c_out=2)
model
= TensorDataset(initial_states.reshape(-1, 2, 32, 32))
dataset = DataLoader(
dataloader
dataset,=config["train_batch_size"],
batch_size=False,
shuffle=True
drop_last
)
= optim.AdamW(model.parameters(), lr=config['learning_rate'])
optimizer = nn.MSELoss()
mse = Diffusion(
diffusion =100,
noise_steps=32,
img_size=DEVICE,
device=2,
nchannels
)#logger = SummaryWriter(os.path.join("runs", args.run_name))
= len(dataloader)
l
= 'diffusion2dU1' run_name
Perform initial training on HMC samples
from torch import optim
= 'cpu'
device #dataloader = get_data(args)
#model = UNet().to(device)
= []
sampled_images_history
for epoch in range(config['num_epochs']):
print(f"Starting epoch {epoch}:")
console.= tqdm(dataloader)
pbar for i, images in enumerate(pbar):
if isinstance(images, (tuple, list)) and len(images) == 1:
= images[0]
images = diffusion.sample_timesteps(images.shape[0]).to(device)
t = diffusion.noise_images(images, t)
x_t, noise = model(x_t, t)
predicted_noise = mse(noise, predicted_noise)
loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
'epoch': epoch, 'batch': i, 'MSE': loss.item()})
pbar.set_postfix({print(f'epoch: {epoch}, loss: {loss.item()}')
console.= diffusion.sample(model, n=images.shape[0])
sampled_images
sampled_images_history.append(sampled_images)'notebook')
sns.set_context(#tmp = initial_states.reshape(-1, 2, 32, 32)
= plt.subplots(ncols=2)
fig, ax = ax[0].imshow(sampled_images[0, 0, :, :])
_ = ax[1].imshow(sampled_images[0, 1, :, :])
_ = ax[0].set_xticklabels([])
_ = ax[1].set_xticklabels([])
_ = ax[0].set_yticklabels([])
_ = ax[1].set_yticklabels([])
_ = ax[0].set_title(r"$U_{0}$", loc='center')
_ = ax[1].set_title(r"$U_{1}$", loc='center')
_ = fig.suptitle('Diffusion Samples', y=0.8)
_
plt.show()=f'sampled_image_epoch{epoch}', outdir=PLOTS_DIR, tstamp=True)
savefig(fname= OUTDIR.joinpath("models", f"unet-diffusion-epoch{epoch}.pt")
MODEL_FILE =True, parents=True)
MODEL_FILE.parent.mkdir(exist_okprint(f"Saving model checkpoint to: {MODEL_FILE}")
console. torch.save(model.state_dict(), MODEL_FILE)
Starting epoch 0:
{"model_id":"19b415c346b24bef8b60336d7f7bc355","version_major":2,"version_minor":0}
epoch: 0, loss: 0.6023472547531128
{"model_id":"eea24504754f4cb9ab4d9925a6225c10","version_major":2,"version_minor":0}
Saving sampled_image_epoch0-2023-09-21-124506 to l2hmc-diffusion-2dU1/2023-09-21/plots
Saving model checkpoint to: l2hmc-diffusion-2dU1/2023-09-21/models/unet-diffusion-epoch0.pt
<Figure size 640x480 with 0 Axes>
'notebook')
sns.set_context(= initial_states.reshape(-1, 2, 32, 32)
tmp = plt.subplots(ncols=2)
fig, ax = ax[0].imshow(tmp[0, 0, :, :])
_ = ax[1].imshow(tmp[0, 1, :, :])
_ = ax[0].set_title(r"$U_{0}$", loc='center')
_ = ax[0].set_xticklabels([])
_ = ax[1].set_xticklabels([])
_ = ax[0].set_yticklabels([])
_ = ax[1].set_yticklabels([])
_ = ax[1].set_title(r"$U_{1}$", loc='center')
_ = fig.suptitle('HMC Samples', y=0.8) _
= torch.stack(sampled_images_history) sampled_images_history_
sampled_images_history_.shape
torch.Size([1, 5, 2, 32, 32])
'notebook')
sns.set_context(= plt.subplots(ncols=2)
fig, ax = ax[0].imshow(sampled_images_history_[0][0][0])
_ = ax[1].imshow(sampled_images_history_[0][0][1])
_ = ax[0].set_xticklabels([])
_ = ax[1].set_xticklabels([])
_ = ax[0].set_yticklabels([])
_ = ax[1].set_yticklabels([])
_ = ax[0].set_title(r"$U_{0}$", loc='center')
_ = ax[1].set_title(r"$U_{1}$", loc='center')
_ = fig.suptitle('Diffusion Samples', y=0.85) _
for idx in range(sampled_images_history_.shape[0]):
= exp.trainer.lattice.charges(x=sampled_images_history_[idx])
q print(f'{idx}: {q}') console.
0: Charges(intQ=tensor([ 5.0000e+00, -4.0000e+00, -6.0000e+00, -4.5535e-07, 1.0000e+00]), sinQ=tensor([ 1.6426, -1.7244, -4.4651, 0.5680, 0.7046]))
HMC Sampling with Diffusion
#for retrain_iter in range(config['retrains']):
= exp.trainer.dynamics.random_state(config['beta'])
state = state.x
x
= {}
histories = []
samples = []
hmc_samples = []
diffusion_samples
= 0
global_step = {}
watcher = []
update_types = {} combined_samples
global_step
0
for retrain_iter in range(2):
print(f'retrain_iter: {retrain_iter}')
console.= 0
ndiff_acc = 0
ndiff_proposed = {
histories[retrain_iter] 'diffusion': [],
'hmc': [],
}#for idx in range(config['samples_per_retrain']):
= tqdm(range(10))
sbar for idx in sbar:
= time.perf_counter()
t0_ if idx % 100 == 0:
print(f'sample idx: {idx}')
console.= np.random.uniform()
rand if (retrain_iter >= 1) and rand < diffusion_prob:
print(f'rand: {rand} < {diffusion_prob}')
console.# Sample from diffusion model
= diffusion.sample(model, n=x.shape[0])
x_ = exp.trainer.dynamics.potential_energy(x_, config['beta'])
ll_ = exp.trainer.dynamics.potential_energy(x, config['beta'])
ll = ll_ / ll
ratio = torch.min(torch.ones_like(ratio), ratio)
a = torch.rand(a.shape)
u #u = np.random.uniform()
#for jdx in range(u.shape[0]):
# if u[jdx] < a[jdx]:
# samples.append(x_[jdx])
# diffusion_samples.append(x_[jdx])
#x = torch.where((u < a), x_, x.reshape_as(x_)).reshape_as(x)
= torch.where((u < a)[:, None, None, None], x_, x.reshape_as(x_))
x
samples.append(x)
diffusion_samples.append(x)= x
combined_samples[global_step] = 'diffusion'
watcher[global_step] #diffusion_samples.extend(x)
#samples.extend(x)
#ndiff_acc +=
#if u < a:
# console.print('Accepted diffusion sample!')
# console.print(f'{ndiff_acc} / {ndiff_proposed}')
# ndiff_acc += 1
# x = x_
# diffusion_samples.append(x)
# samples.append(x)
else:
# Oherwise, HMC
= exp.trainer.hmc_step((x, config['beta']))
x, metrics
hmc_samples.append(x)
samples.append(x)= x
combined_samples[global_step] = 'HMC'
watcher[global_step] = {
smetrics 'idx': idx,
'global_step': global_step,
'dt': time.perf_counter() - t0_,
}+= 1
global_step #smetrics |= {
# f'{k}': {torch.tensor(v).mean().item()} for k, v in metrics.items()
#}
sbar.set_postfix(smetrics)# Train loop
= TensorDataset(
dataset -1, 2, 32, 32)
torch.stack(hmc_samples).reshape(
)= DataLoader(
dataloader
dataset,=False,
shuffle=True,
drop_last=config["train_batch_size"],
batch_size
)= tqdm(dataloader)
pbar for i, batch in enumerate(pbar):
if i == 0:
print('Retraining...')
console.if isinstance(batch, (tuple, list)) and len(batch) == 1:
= batch
batch, = batch.reshape(-1, 2, 32, 32)
batch = time.time()
t0 = diffusion.sample_timesteps(batch.shape[0]).to(device)
t = diffusion.noise_images(batch, t)
x_t, noise = model(x_t, t)
predicted_noise = mse(noise, predicted_noise)
loss
optimizer.zero_grad()
loss.backward()
optimizer.step()= time.time()
t1
pbar.set_postfix(
{'global_step': global_step,
'retrain_iter': retrain_iter,
'batch': i,
'dt': t1 - t0,
'MSE': loss.item()
} )
retrain_iter: 0
{"model_id":"17132d7ca8624fa387ee9467e4f1fa4d","version_major":2,"version_minor":0}
sample idx: 0
{"model_id":"0ed1080fdebd4f7b9aae80db0d36b96b","version_major":2,"version_minor":0}
Retraining...
retrain_iter: 1
{"model_id":"d0346019e21b4d2a9b624dc59e84015b","version_major":2,"version_minor":0}
sample idx: 0
rand: 0.05506106760134255 < 0.1
{"model_id":"c02b09d53ada46a194a47921f0ab3cba","version_major":2,"version_minor":0}
rand: 0.07860283644524213 < 0.1
{"model_id":"184df3f1c9714ece9756866b2617ed02","version_major":2,"version_minor":0}
{"model_id":"eaa0d84229c04618b7a2bffe2a4b1739","version_major":2,"version_minor":0}
Retraining...
print('\n'.join([f"{i.shape}" for i in samples[:100]])) console.
torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2048]) torch.Size([16, 2, 32, 32]) torch.Size([16, 2048]) torch.Size([16, 2, 32, 32])
= torch.stack([i.reshape(-1, 2, 32, 32) for i in samples])
samples_ samples_.shape
torch.Size([30, 16, 2, 32, 32])
len(hmc_samples)
28
len(diffusion_samples)
2
= torch.stack([i.reshape(-1, 2, 32, 32) for i in hmc_samples])
hmc_samples_ = torch.stack(
diffusion_samples_ -1, 2, 32, 32) for i in diffusion_samples]
[i.reshape( )
hmc_samples_.shape
torch.Size([28, 16, 2, 32, 32])
diffusion_samples_.shape
torch.Size([2, 16, 2, 32, 32])
samples_.shape
torch.Size([30, 16, 2, 32, 32])
def calc_plaqs(x):
return torch.stack([
exp.trainer.lattice.plaqs(
x[:, idx]for idx in range(x.shape[1])
) -1)
],
def calc_intQ(x):
return torch.stack([
exp.trainer.lattice.int_charges(
x[:, idx]for idx in range(x.shape[1])
) -1)
],
def calc_sinQ(x):
return torch.stack([
exp.trainer.lattice.sin_charges(
x[:, idx]for idx in range(x.shape[1])
) -1) ],
= initial_states.reshape(-1, initial_states.shape[1], 2, 32, 32)
samples_init_ samples_init_.shape
torch.Size([999, 16, 2, 32, 32])
= {
metrics_init_ 'plaqs': calc_plaqs(samples_init_),
'intQ': calc_intQ(samples_init_),
'sinQ': calc_sinQ(samples_init_)
}
= {
metrics_ 'plaqs': calc_plaqs(samples_),
'intQ': calc_intQ(samples_),
'sinQ': calc_sinQ(samples_)
}
= {
metrics_hmc_ 'plaqs': calc_plaqs(hmc_samples_),
'intQ': calc_intQ(hmc_samples_),
'sinQ': calc_sinQ(hmc_samples_)
}
= {
metrics_diffusion_ 'plaqs': calc_plaqs(diffusion_samples_),
'intQ': calc_intQ(diffusion_samples_),
'sinQ': calc_sinQ(diffusion_samples_)
}
'plaqs'].shape metrics_[
torch.Size([30, 16])
print('\n'.join([f"{k}: {v}" for k, v in watcher.items()])) console.
0: HMC 1: HMC 2: HMC 3: HMC 4: HMC 5: HMC 6: HMC 7: HMC 8: HMC 9: HMC 10: HMC 11: HMC 12: HMC 13: HMC 14: HMC 15: HMC 16: HMC 17: HMC 18: HMC 19: HMC 20: HMC 21: HMC 22: HMC 23: HMC 24: HMC 25: HMC 26: HMC 27: diffusion 28: HMC 29: diffusion
= plt.subplots()
fig, ax
= ax.plot(metrics_['plaqs'][:, 0], label='Combined')
_ = ax.plot(metrics_hmc_['plaqs'][:, 0], label='HMC')
_ = ax.plot(metrics_diffusion_['plaqs'][:, 0], label='Diffusion')
_ #_ = ax.plot(metrics_hmc1['plaqs'], label='HMC 1')
#_ = ax.plot(metrics_diff_['plaqs'], label='Diffusion')
= ax.legend(loc='upper left', bbox_to_anchor=(1.05, 1.00)) _
= plt.subplots(ncols=3, figsize=(14, 4))
fig, ax for idx, (key, val) in enumerate(metrics_init_.items()):
= ax[idx].plot(val[:, 0], label='HMC (Initial Samples)')
_ = ax[idx].set_ylabel(key, loc='center')
_ = ax[idx].set_xlabel('Draw', loc='center')
_ #_ = ax[idx].legend(loc='best', frameon=True, edgecolor="#838383")
= fig.suptitle(f"Initial HMC States", y=0.92) _
= plt.subplots(ncols=3, figsize=(14, 4))
fig, ax for idx, (key, val) in enumerate(metrics_.items()):
= ax[idx].plot(val[:, 0], label='Combined')
_ = ax[idx].set_ylabel(key, loc='center')
_ = ax[idx].set_xlabel('Draw', loc='center')
_ #_ = ax[idx].legend(loc='best', frameon=True, edgecolor="#838383")
= fig.suptitle(f"Combined Samples", y=0.92) _
= plt.subplots(ncols=3, figsize=(14, 4))
fig, ax for idx, (key, val) in enumerate(metrics_hmc_.items()):
= ax[idx].plot(val[:, 0], label='HMC')
_ = ax[idx].set_ylabel(key, loc='center')
_ = ax[idx].set_xlabel('Draw', loc='center')
_ = fig.suptitle(f"Generated HMC States", y=0.92) _
= plt.subplots(ncols=3, figsize=(14, 4))
fig, ax for idx, (key, val) in enumerate(metrics_diffusion_.items()):
= ax[idx].plot(val[:, 0], label='Diffusion')
_ = ax[idx].set_ylabel(key, loc='center')
_ = ax[idx].set_xlabel('Draw', loc='center')
_ = fig.suptitle(f"Generated Diffusion States", y=0.92) _
from l2hmc.lattice.u1.pytorch.lattice import plaq_exact
6.0)) plaq_exact(torch.tensor(
tensor(0.9124)
= plt.subplots()
fig, ax #_ = plt.hist(metrics_['intQ'].flatten(), color='C0', alpha=0.6, label='Combined', edgecolor='none')
= ax.hist(
_ 'intQ'].flatten(),
metrics_diffusion_[='C0',
color=0.6,
alpha='none',
edgecolor='Diffusion',
label=True,
density
)= ax.hist(
_ 'intQ'].flatten(),
metrics_hmc_[='C1',
color=0.6,
alpha='none',
edgecolor='HMC',
label=True,
density
)= ax.legend(loc='best', frameon=True, edgecolor='#666666')
_ = ax.set_xlabel(r"$Q$", loc='center')
_ = ax.set_title('Topological Charge ($Q$) Distribution', loc='center') _
= plt.subplots()
fig, ax = plt.plot(metrics_['plaqs'][:, 0], color='C0', label='Diffusion')
_ = plt.plot(metrics_hmc_['plaqs'][:, 0], color='C1', label='HMC')
_ = ax.legend(loc='best', frameon=True, edgecolor='#666666', ncols=2)
_ = ax.set_ylabel(r"$\left\langle U_{\mu\nu}\right\rangle $", loc='center')
_ = ax.set_xlabel(f"Draw", loc='center') _
= {
wloops 'hmc': [
for i in hmc_samples_
exp.trainer.lattice.wilson_loops(i)
],'diffusion': [
for i in diffusion_samples_
exp.trainer.lattice.wilson_loops(i)
],
}
= {
plaqs 'hmc': [
for i in hmc_samples_
exp.trainer.lattice.plaqs(i)
],'diffusion': [
for i in diffusion_samples_
exp.trainer.lattice.plaqs(i)
], }
= torch.stack(wloops['hmc']).squeeze()
wlhmc = torch.stack(wloops['diffusion']).squeeze() wldiff
wlhmc.shape
torch.Size([28, 16, 32, 32])
= plt.tight_layout()
_ for idx in range(2):
= plt.subplots(ncols=2)
fig, ax = ax[0].imshow(wlhmc[idx, 0])
_ = ax[0].set_title("HMC", loc='center')
_ = ax[1].imshow(wldiff[idx, 0])
_ = ax[1].set_title("Diffusion", loc='center')
_ = fig.suptitle(r"$U_{\mu\nu}$", y=0.8)
_ for ax_ in ax:
= ax_.set_xticklabels([])
_ = ax_.set_yticklabels([]) _
<Figure size 640x480 with 0 Axes>
= metrics_hmc_['intQ']
qhmc = metrics_diffusion_['intQ']
qdiff qhmc.shape
torch.Size([28, 16])
= torch.stack(plaqs['hmc']).squeeze()
phmc = torch.stack(plaqs['diffusion']).squeeze()
pdiff phmc.shape
torch.Size([28, 16])
pdiff.shape
torch.Size([2, 16])
= plt.subplots()
fig, ax
= ax.hist(
_ 'plaqs'].flatten(),
metrics_[='C1',
color='step',
histtype=True,
stacked=True,
density='HMC',
label=1.5
linewidth
)= ax.hist(
_ 'plaqs'].flatten(),
metrics_diffusion_[='C0',
color='step',
histtype=True,
stacked=True,
density='Diffusion',
label=1.5
linewidth
)= ax.hist(
_ 'plaqs'].flatten(),
metrics_hmc_[='C2',
color='step',
histtype=True,
stacked=True,
density='Combined',
label=1.5
linewidth
)= ax.set_xlabel(r"$U_{\mu\nu}$", loc='center')
_ = ax.legend(
_ ='upper left',
loc=True,
frameon#ncols=2,
=(0.55, 1.00),
bbox_to_anchor="#838383",
edgecolor
)= ax.set_title('Plaquette Distribution', loc='center') _
= plt.subplots()
fig, ax
= ax.hist(
_ 'intQ'].flatten(),
metrics_[='C1',
color='step',
histtype=True,
stacked=True,
density='HMC',
label=1.5
linewidth
)= ax.hist(
_ 'intQ'].flatten(),
metrics_diffusion_[='C0',
color='step',
histtype=True,
stacked=True,
density='Diffusion',
label=1.5
linewidth
)= ax.hist(
_ 'intQ'].flatten(),
metrics_hmc_[='C2',
color='step',
histtype=True,
stacked=True,
density='Combined',
label=1.5
linewidth
)= ax.set_xlabel('$Q_{\mathbb{Z}}$', loc='center')
_ = ax.legend(
_ ='upper left',
loc=True,
frameon#ncols=2,
=(0.55, 1.00),
bbox_to_anchor="#838383",
edgecolor
)= ax.set_title('Charge Distribution', loc='center') _
= 0
global_step = []
frames = []
losses print("Training model...")
for epoch in range(config["num_epochs"]):
model.train()= tqdm(total=len(dataloader))
progress_bar f"Epoch {epoch}")
progress_bar.set_description(for step, batch in enumerate(dataloader):
= diffusion.sample_timesteps(images.shape[0]).to(device)
t
= torch.randn(batch.shape)
noise = torch.randint(
timesteps 0, noise_scheduler.num_timesteps, (batch.shape[0],)
long()
).
#noisy = noise_scheduler.add_noise(batch, noise, timesteps)
= noise_scheduler.noise_images(batch, timesteps)
noisy = model(noisy, timesteps)
noise_pred = F.mse_loss(noise_pred, noise)
loss
loss.backward(loss)
1.0)
nn.utils.clip_grad_norm_(model.parameters(),
optimizer.step()
optimizer.zero_grad()
1)
progress_bar.update(= {"loss": loss.detach().item(), "step": global_step}
logs
losses.append(loss.detach().item())**logs)
progress_bar.set_postfix(+= 1
global_step
progress_bar.close()
if epoch % config["save_images_step"] == 0 or epoch == config["num_epochs"] - 1:
# generate data with the model to later visualize the learning process
eval()
model.= torch.randn(config["eval_batch_size"], 2)
sample = list(range(len(noise_scheduler)))[::-1]
timesteps for i, t in enumerate(tqdm(timesteps)):
= torch.from_numpy(np.repeat(t, config["eval_batch_size"])).long()
t with torch.no_grad():
= model(sample, t)
residual = noise_scheduler.step(residual, t[0], sample)
sample frames.append(sample.numpy())
6] dataset[
len(dataloader)
= 10
eval_batch_size = 50
num_timesteps = 5
plot_step = ddpm.NoiseScheduler(num_timesteps=num_timesteps)
noise_scheduler = torch.randn(eval_batch_size, 2)
sample = list(range(num_timesteps))[::-1]
timesteps = []
samples = []
steps
= 10
retrains = 0.3
diffusion_prob = 100 samples_per_retrain
= 10
eval_batch_size = torch.from_numpy(np.repeat(timesteps[0], eval_batch_size)).long()
t with torch.no_grad():
= model(sample, t)
residual = noise_scheduler.step(residual, t[0], sample) sample_
sample.shape
residual.shape
sample_.shape
= []
diffusion_samples = []
hmc_samples = 1.
beta for retrain_iter in range(retrains):
print(f'retrain_iter: {retrain_iter}')
console.= 0
ndiff_acc = 0
ndiff_proposed for idx in range(samples_per_retrain):
print(f'sample idx: {idx}')
console.= np.random.uniform()
rand if rand < diffusion_prob:
+= 1
ndiff_proposed = randrange(len(dataloader))
rand_pick #theta_prime = dataset[rand_pick]
= torch.from_numpy(np.repeat(t, eval_batch_size)).long()
t with torch.no_grad():
= model(sample, t)
residual = noise_scheduler.step(residual, t[0], sample)
sample_ = (
ratio 2)
log_likelihood_2dU1(sample_, / log_likelihood_2dU1(sample, 2)
)= min(1, ratio)
a = np.random.uniform()
u if u < a:
+= 1
ndiff_acc = sample_
sample
diffusion_samples.append(sample)else:
= exp.trainer.hmc_step((sample_, beta))
sample_, metrics hmc_samples.append(sample)
for i, t in enumerate(tqdm(timesteps)):
= torch.from_numpy(np.repeat(t, eval_batch_size)).long()
t with torch.no_grad():
= model(sample, t)
residual = noise_scheduler.step(residual, t[0], sample)
sample if (i + 1) % plot_step == 0:
samples.append(sample.numpy())+ 1) steps.append(i
Alternate
= DiffusionAlt(img_size=64, device='cpu') diffusion_
unet
= torch.rand(1, 2, 64, 64)
image = diffusion_.sample_timesteps(image.shape[0]).to('cpu')
t unet(image, t)
diffusion_.sample(
Citation
BibTeX citation:
@online{foreman2023,
author = {Foreman, Sam},
title = {MCMC + {Diffusion} {Sampling}},
date = {2023-12-06},
url = {https://saforem2.github.io/l2hmc-qcd/qmd/diffusion/diffusion.html},
langid = {en}
}
For attribution, please cite this work as:
Foreman, Sam. 2023. “MCMC + Diffusion Sampling.” December
6, 2023. https://saforem2.github.io/l2hmc-qcd/qmd/diffusion/diffusion.html.