-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_base_flows.py
More file actions
184 lines (153 loc) · 6.65 KB
/
Copy pathtrain_base_flows.py
File metadata and controls
184 lines (153 loc) · 6.65 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
120
121
122
123
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
"""
Train base score and kinematic normalizing flows (no nuisance parameters).
Extracted from ToyDataset_inputs.ipynb (Cells 14-35).
Steps:
train_score -- train conditional score flow p(y|x,c)
train_kin -- train kinematic flow p(x|c)
"""
import argparse
import os
from typing import Any, Dict
import torch
import yaml
import zuko
from generator import ParametricLikelihoodDataset
from utils import LinearWarmupCosineDecay
def _resolve_path(path: str, cfg_dir: str) -> str:
if os.path.isabs(path):
return path
return os.path.normpath(os.path.join(cfg_dir, path))
def _make_generator(gen_cfg: Dict[str, Any], device: str) -> ParametricLikelihoodDataset:
kwargs: Dict[str, Any] = dict(device=device)
for key in ("center_A", "center_B", "sigma_A", "sigma_B", "shift_dir"):
if key in gen_cfg:
kwargs[key] = tuple(gen_cfg[key])
for key in ("variation_shift", "variation_rot", "variation_squeeze",
"shift_scale", "rot_scale", "squeeze_scale",
"y_shift_scale", "y_squeeze_scale", "distortion_strength",
"distortion_shift_scale", "distortion_squeeze_scale"):
if key in gen_cfg:
kwargs[key] = float(gen_cfg[key])
if "sigmoid_y" in gen_cfg:
kwargs["sigmoid_y"] = bool(gen_cfg["sigmoid_y"])
return ParametricLikelihoodDataset(**kwargs)
def _train_score(cfg: Dict[str, Any], device: str) -> None:
gen_cfg = cfg["generator"]
gen = _make_generator(gen_cfg, device)
score_cfg = cfg["score_flow"]
model = zuko.flows.NSF(
features=score_cfg["features"],
context=score_cfg["context"],
bins=score_cfg["bins"],
transforms=score_cfg["transforms"],
hidden_features=tuple(score_cfg["hidden_features"]),
).to(device)
train_cfg = cfg["training"]["score"]
batch_size = int(train_cfg["batch_size"])
nepochs = int(train_cfg["nepochs"])
steps_per_epoch = int(train_cfg["steps_per_epoch"])
lr = float(train_cfg["lr"])
optimizer = torch.optim.AdamW(model.parameters(), lr=lr)
total_steps = nepochs * steps_per_epoch
warmup_steps = steps_per_epoch
scheduler = LinearWarmupCosineDecay(optimizer, warmup_steps, total_steps)
print(f"Training score flow: {nepochs} epochs x {steps_per_epoch} steps, batch={batch_size}")
for epoch in range(nepochs):
model.train()
train_loss = 0.0
for step in range(steps_per_epoch):
optimizer.zero_grad()
c, X, y, _, _ = gen.generate_batch(batch_size, distorsion=False)
context = torch.cat([c.to(torch.float32), X], dim=-1)
log_prob = model(context).log_prob(y)
loss = -log_prob.mean()
loss.backward()
optimizer.step()
scheduler.step()
train_loss += loss.item()
if step % 500 == 0:
print(f" epoch={epoch} step={step}/{steps_per_epoch} loss={loss.item():.4f}")
# Validation
model.eval()
val_loss = 0.0
with torch.no_grad():
for _ in range(10):
c, X, y, _, _ = gen.generate_batch(batch_size, distorsion=False)
context = torch.cat([c.to(torch.float32), X], dim=-1)
val_loss += -model(context).log_prob(y).mean().item()
val_loss /= 10
print(f" epoch={epoch} train_loss={train_loss/steps_per_epoch:.4f} val_loss={val_loss:.4f}")
out_path = cfg["paths"]["score_model"]
torch.save(model.state_dict(), out_path)
print(f"Saved score model to {out_path}")
def _train_kin(cfg: Dict[str, Any], device: str) -> None:
gen_cfg = cfg["generator"]
gen = _make_generator(gen_cfg, device)
kin_cfg = cfg["kin_flow"]
model = zuko.flows.NSF(
features=kin_cfg["features"],
context=kin_cfg["context"],
bins=kin_cfg["bins"],
transforms=kin_cfg["transforms"],
hidden_features=tuple(kin_cfg["hidden_features"]),
).to(device)
train_cfg = cfg["training"]["kin"]
batch_size = int(train_cfg["batch_size"])
nepochs = int(train_cfg["nepochs"])
steps_per_epoch = int(train_cfg["steps_per_epoch"])
lr = float(train_cfg["lr"])
optimizer = torch.optim.AdamW(model.parameters(), lr=lr)
total_steps = nepochs * steps_per_epoch
warmup_steps = steps_per_epoch
scheduler = LinearWarmupCosineDecay(optimizer, warmup_steps, total_steps)
print(f"Training kin flow: {nepochs} epochs x {steps_per_epoch} steps, batch={batch_size}")
for epoch in range(nepochs):
model.train()
train_loss = 0.0
for step in range(steps_per_epoch):
optimizer.zero_grad()
c, X, _, _, _ = gen.generate_batch(batch_size, distorsion=False)
log_prob = model(c.to(torch.float32)).log_prob(X)
loss = -log_prob.mean()
loss.backward()
optimizer.step()
scheduler.step()
train_loss += loss.item()
if step % 500 == 0:
print(f" epoch={epoch} step={step}/{steps_per_epoch} loss={loss.item():.4f}")
model.eval()
val_loss = 0.0
with torch.no_grad():
for _ in range(10):
c, X, _, _, _ = gen.generate_batch(batch_size, distorsion=False)
val_loss += -model(c.to(torch.float32)).log_prob(X).mean().item()
val_loss /= 10
print(f" epoch={epoch} train_loss={train_loss/steps_per_epoch:.4f} val_loss={val_loss:.4f}")
out_path = cfg["paths"]["kin_model"]
torch.save(model.state_dict(), out_path)
print(f"Saved kin model to {out_path}")
def main() -> None:
parser = argparse.ArgumentParser(description="Train base score/kin flows from YAML config.")
parser.add_argument("-c", "--cfg", type=str, required=True)
parser.add_argument("-s", "--steps", type=str, required=True,
help="Comma-separated: train_score,train_kin")
args = parser.parse_args()
cfg_path = os.path.abspath(args.cfg)
with open(cfg_path, "r", encoding="utf-8") as f:
cfg = yaml.safe_load(f)
cfg_dir = os.path.dirname(cfg_path)
cfg.setdefault("runtime", {})
requested_device = cfg["runtime"].get("device", "cuda")
if requested_device == "cuda" and not torch.cuda.is_available():
print("CUDA not available, falling back to CPU.")
requested_device = "cpu"
for key, value in list(cfg["paths"].items()):
if isinstance(value, str):
cfg["paths"][key] = _resolve_path(value, cfg_dir)
steps = [s.strip() for s in args.steps.split(",") if s.strip()]
if "train_score" in steps:
_train_score(cfg, requested_device)
if "train_kin" in steps:
_train_kin(cfg, requested_device)
if __name__ == "__main__":
main()