cs4486-amp-pd-checkpoints / code /ampd /top_methods.py
fryabcfry's picture
Publish AMP-PD checkpoints, portable loaders and verified metrics (part 4)
0d48417 verified
Raw History Blame Contribute Delete
7.6 kB
"""Auditable adaptations of the AMP-PD first/fourth/fifth-place approaches.
The fifth-place numerical optimizers are loaded from the attributed audit
snapshot. No submission code, pretrained weights, or external labels are run.
"""
from __future__ import annotations
import ast
from pathlib import Path
import numpy as np
import pandas as pd
import scipy.optimize
import torch
from sklearn.utils import check_consistent_length
from torch import nn
def decision_from_distribution(probabilities, candidates=87):
"""Minimize expected SMAPE+1 over integer predictions, without true labels."""
p = np.asarray(probabilities, dtype=float)
values = np.arange(p.shape[1], dtype=float)[:, None]
choices = np.arange(candidates, dtype=float)[None, :]
loss = 200 * np.abs(values - choices) / (values + choices + 2)
return (p @ loss).argmin(axis=1).astype(float)
def history_groups(samples, clinical, months):
"""Fifth-place sticky group update, restricted to the visible prefix."""
calendars = clinical[clinical.visit_month.isin(months)].groupby("uid").visit_month.apply(list).to_dict()
groups = []
for row in samples.itertuples():
past = [m for m in calendars[row.uid] if m <= row.visit_month]
if any(m % 12 != 0 for m in past):
groups.append("ill")
elif row.visit_month >= 12:
groups.append("healthy")
else:
groups.append("unknown")
return np.asarray(groups)
def load_fifth_optimizers(path):
names = {"smapep1", "optimize_smapep1", "calculate_predictions",
"optimize_smapep1_linear", "optimize_smapep1_isotonic"}
nodes = [node for node in ast.parse(Path(path).read_text()).body
if isinstance(node, ast.FunctionDef) and node.name in names]
assert {node.name for node in nodes} == names
namespace = {"np": np, "scipy": scipy, "check_consistent_length": check_consistent_length}
exec(compile(ast.Module(body=nodes, type_ignores=[]), str(path), "exec"), namespace)
return namespace
class GroupTrends:
"""Fifth-place unknown constant / healthy isotonic / ill linear model.
Only observed training targets are optimized. For healthy months absent from
training, extend the last known step, with constant boundary extrapolation.
"""
def __init__(self, optimizers):
self.optimizers = optimizers
self.parameters = {}
def fit(self, frame):
for target, df in frame.groupby("target"):
fallback = self.optimizers["optimize_smapep1"](df.y_true.to_numpy())
for group in ["unknown", "healthy", "ill"]:
rows = df[df.group == group]
if rows.empty:
self.parameters[(target, group)] = ("constant", float(fallback))
elif group == "unknown":
value = self.optimizers["optimize_smapep1"](rows.y_true.to_numpy())
self.parameters[(target, group)] = ("constant", float(value))
elif group == "healthy":
months, _, values = self.optimizers["optimize_smapep1_isotonic"](
rows.target_month.to_numpy(), rows.y_true.to_numpy())
self.parameters[(target, group)] = ("step", months, values)
else:
parameters = self.optimizers["optimize_smapep1_linear"](
rows.target_month.to_numpy(), rows.y_true.to_numpy(), target)
self.parameters[(target, group)] = ("linear", parameters)
return self
def predict(self, frame):
frame = frame.reset_index(drop=True)
prediction = np.zeros(len(frame))
for (target, group), rows in frame.groupby(["target", "group"]):
spec = self.parameters[(target, group)]
months = rows.target_month.to_numpy()
if spec[0] == "constant":
value = np.full(len(rows), spec[1])
elif spec[0] == "step":
index = np.searchsorted(spec[1], months, side="right") - 1
value = spec[2][np.clip(index, 0, len(spec[2]) - 1)]
else:
value = self.optimizers["calculate_predictions"](months, spec[1], target)
prediction[rows.index.to_numpy()] = value
return prediction
def fourth_mlp(input_dim):
layers = []
for _ in range(10):
layers.extend([nn.Linear(input_dim, 24), nn.ReLU()])
input_dim = 24
layers.append(nn.Linear(24, 1))
return nn.Sequential(*layers)
def first_mlp(input_dim):
return nn.Sequential(nn.Linear(input_dim, 64), nn.ReLU(), nn.Linear(64, 64),
nn.ReLU(), nn.Linear(64, 1), nn.LeakyReLU(.01))
def rounded(predictions):
return np.rint(np.clip(predictions, 0, None))
def smape(y, predictions):
return float(np.mean(200 * np.abs(y - predictions) / (np.abs(y) + np.abs(predictions) + 2)))
def train_nn(X, y, seed, family, epochs, validation=None):
"""Fixed fourth-place schedule, or inner-selected first-style SMAPE training."""
torch.manual_seed(seed)
model = fourth_mlp(X.shape[1]) if family == "fourth" else first_mlp(X.shape[1])
optimizer = torch.optim.Adam(model.parameters(), lr=.001)
inputs = torch.tensor(X, dtype=torch.float32)
labels = torch.tensor(y, dtype=torch.float32)
rng = np.random.default_rng(seed)
batch = 32 if family == "fourth" else 256
history, best_epoch, best_score, stale = [], epochs, float("inf"), 0
if validation is not None:
vx = torch.tensor(validation[0], dtype=torch.float32)
vy = validation[1]
for epoch in range(1, epochs + 1):
if family == "fourth" and epoch == 13:
# The original recompiles with a fresh Adam optimizer at this point.
optimizer = torch.optim.Adam(model.parameters(), lr=.0001)
elif family == "first":
rate = .00001 + .5 * (.001 - .00001) * (1 + np.cos(np.pi * (epoch - 1) / 100))
for group in optimizer.param_groups:
group["lr"] = rate
model.train()
total = 0.0
for indices in np.array_split(rng.permutation(len(X)), max(1, int(np.ceil(len(X) / batch)))):
prediction = model(inputs[indices]).ravel()
if family == "fourth":
loss = (prediction - labels[indices]).abs().mean()
else:
loss = ((prediction - labels[indices]).abs() /
((prediction.abs() + labels[indices].abs()) / 2 + 1)).mean()
if not torch.isfinite(loss):
raise RuntimeError("Non-finite training loss")
optimizer.zero_grad()
loss.backward()
optimizer.step()
total += float(loss.detach()) * len(indices)
entry = {"epoch": epoch, "train_loss": total / len(X), "lr": optimizer.param_groups[0]["lr"]}
if validation is not None:
model.eval()
with torch.no_grad():
value = model(vx).ravel().numpy()
score = smape(vy, rounded(value))
entry["inner_smape"] = score
if score < best_score - .0001:
best_score, best_epoch, stale = score, epoch, 0
else:
stale += 1
if epoch >= 30 and stale >= 20:
history.append(entry)
break
history.append(entry)
model.eval()
return model, history, best_epoch
def nn_predict(model, X):
with torch.no_grad():
return model(torch.tensor(X, dtype=torch.float32)).ravel().numpy().astype(float)