-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathregularization.py
More file actions
119 lines (99 loc) · 3.73 KB
/
Copy pathregularization.py
File metadata and controls
119 lines (99 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
# ----- Step 1: Generate Mixture of Gaussians Data -----
def sample_mog(n_samples=1000):
means = [np.array([0, 0]), np.array([5, 5]), np.array([-5, 5])]
covs = [
np.array([[1, 0.8], [0.8, 1]]),
np.array([[1, -0.6], [-0.6, 1]]),
np.array([[0.5, 0], [0, 0.5]])
]
weights = [0.4, 0.35, 0.25]
components = np.random.choice(len(weights), size=n_samples, p=weights)
samples = np.array([
np.random.multivariate_normal(means[k], covs[k])
for k in components
])
return samples.astype(np.float32)
# ----- Step 2: Define Energy MLP -----
class EnergyModel(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Linear(2, 128),
nn.ReLU(),
nn.Linear(128, 128), # Output: scalar energy
nn.ReLU(),
nn.Linear(128, 1)
)
def forward(self, x):
return self.net(x).squeeze() # shape: (batch,)
# ----- Step 3: Energy Loss with Regularization -----
def energy_regularized_loss(model, data_batch, lambda_reg=1.0):
data_batch.requires_grad_(True) # enable gradients w.r.t inputs
energy = model(data_batch)
# Compute gradients of energy w.r.t inputs
grad_outputs = torch.ones_like(energy)
grads = torch.autograd.grad(outputs=energy, inputs=data_batch,
grad_outputs=grad_outputs,
create_graph=True, retain_graph=True)[0]
# Gradient norm squared (||∇E||²)
grad_norm_sq = torch.sum(grads ** 2, dim=1)
# Loss: energy on data + lambda * gradient penalty
loss = torch.mean(energy) + lambda_reg * torch.mean(grad_norm_sq)
return loss
def dsm_loss(model, data_batch, sigma=0.5):
noise = torch.randn_like(data_batch) * sigma
noisy_data = data_batch + noise
noisy_data.requires_grad_(True)
energy = model(noisy_data)
grads = torch.autograd.grad(outputs=energy, inputs=noisy_data,
grad_outputs=torch.ones_like(energy),
create_graph=True)[0]
target = -noise / (sigma ** 2)
loss = torch.mean((grads - target) ** 2)
return loss
# ----- Step 4: Training Loop -----
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = EnergyModel().to(device)
optimizer = optim.Adam(model.parameters(), lr=1e-3)
# Dataset
data = sample_mog(5000)
data_tensor = torch.tensor(data).to(device)
# Training
batch_size = 128
n_epochs = 1000
# lambda_reg = 1.0 # gradient regularization strength
lambda_reg = 10
for epoch in range(n_epochs):
idx = torch.randint(0, data_tensor.shape[0], (batch_size,))
batch = data_tensor[idx]
# loss = energy_regularized_loss(model, batch, lambda_reg)
loss = dsm_loss(model, batch, sigma=1)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 100 == 0:
print(f"Epoch {epoch} | Loss: {loss.item():.4f}")
# ----- Step 5: Visualize Energy Landscape -----
def plot_energy(model):
model.eval()
x = torch.linspace(-10, 10, 200)
y = torch.linspace(-5, 15, 200)
X, Y = torch.meshgrid(x, y, indexing='ij')
coords = torch.stack([X.flatten(), Y.flatten()], dim=1).to(device)
with torch.no_grad():
energies = model(coords).reshape(200, 200).cpu()
plt.figure(figsize=(8, 6))
plt.contourf(X.cpu(), Y.cpu(), energies, levels=50, cmap='inferno')
plt.colorbar(label='Energy')
plt.title('Energy Function with Regularization')
plt.xlabel('x')
plt.ylabel('y')
plt.scatter(data[:, 0], data[:, 1], s=1, color='cyan', alpha=0.3, label='Data')
plt.legend()
plt.show()
plot_energy(model)