Download code/ampd/simple_mlp_baseline.py from fryabcfry/cs4486-amp-pd-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 5.44 kB
-
https://huggingface.co/fryabcfry/cs4486-amp-pd-checkpoints/resolve/main/code/ampd/simple_mlp_baseline.py
- Command line
-
hf download hf://fryabcfry/cs4486-amp-pd-checkpoints/code/ampd/simple_mlp_baseline.py
-
curl -L -o simple_mlp_baseline.py https://huggingface.co/fryabcfry/cs4486-amp-pd-checkpoints/resolve/main/code/ampd/simple_mlp_baseline.py
5.44 kB
| """Simple current-month/protein MLP baseline with fold-local preprocessing.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import warnings | |
| import numpy as np | |
| import pandas as pd | |
| import torch | |
| from ampd.evaluation.metrics import smape_plus_1 | |
| from ampd.models import build_model | |
| from ampd.training.losses import masked_mae | |
| FEATURE_SETS = ("month_protein", "month_only", "month_missingness") | |
| def raw_features(months, proteins, feature_set): | |
| months = np.asarray(months, dtype=float) | |
| proteins = np.asarray(proteins, dtype=float) | |
| if np.any(proteins < 0): | |
| raise ValueError("Protein abundances must be nonnegative") | |
| if feature_set == "month_only": | |
| return months[:, None] | |
| if feature_set == "month_protein": | |
| log = np.where(np.isfinite(proteins), np.log1p(proteins), np.nan) | |
| return np.c_[months, log] | |
| if feature_set == "month_missingness": | |
| missing = ~np.isfinite(proteins) | |
| return np.c_[months, missing, (~missing).sum(1)].astype(float) | |
| raise ValueError(feature_set) | |
| def fit_preprocessor(x): | |
| with warnings.catch_warnings(): | |
| warnings.simplefilter("ignore", RuntimeWarning) | |
| impute = np.nan_to_num(np.nanmedian(x, axis=0)) | |
| filled = np.where(np.isfinite(x), x, impute) | |
| mean, scale = filled.mean(0), filled.std(0) | |
| return {"impute": impute, "mean": mean, "scale": np.where(scale > 0, scale, 1.)} | |
| def transform(x, pre): | |
| filled = np.where(np.isfinite(x), x, pre["impute"]) | |
| return ((filled-pre["mean"])/pre["scale"]).astype(np.float32) | |
| def train(x, y, config, epochs, validation=None): | |
| torch.manual_seed(int(config["seed"])) | |
| model = build_model(config["model"], x.shape[1], 16) | |
| optimizer = torch.optim.Adam(model.parameters(), lr=config["optimizer"]["lr"], | |
| weight_decay=config["optimizer"]["weight_decay"]) | |
| xt = torch.tensor(x, dtype=torch.float32) | |
| yt = torch.tensor(np.where(np.isfinite(y), y, 0.), dtype=torch.float32) | |
| mask = torch.tensor(np.isfinite(y), dtype=torch.float32) | |
| rng = np.random.default_rng(config["seed"]) | |
| best, selected, stale, state = float("inf"), epochs, 0, None | |
| logs = [] | |
| if validation is not None: | |
| vx, vy = validation | |
| vt = torch.tensor(vx, dtype=torch.float32) | |
| vm = np.isfinite(vy) | |
| for epoch in range(1, epochs+1): | |
| model.train() | |
| order = rng.permutation(len(x)) | |
| total, count = 0., 0. | |
| for start in range(0, len(x), config["trainer"]["batch_size"]): | |
| rows = order[start:start+config["trainer"]["batch_size"]] | |
| loss = masked_mae(model(xt[rows]), yt[rows], mask[rows]) | |
| if not torch.isfinite(loss): | |
| raise ValueError("Nonfinite training loss") | |
| optimizer.zero_grad() | |
| loss.backward() | |
| optimizer.step() | |
| n = float(mask[rows].sum()) | |
| total += float(loss.detach())*n | |
| count += n | |
| record = {"epoch": epoch, "train_masked_mae": total/count} | |
| if validation is not None: | |
| model.eval() | |
| with torch.no_grad(): | |
| point = model(vt).numpy() | |
| score = smape_plus_1(vy[vm], point[vm]) | |
| record["inner_smape"] = score | |
| if score < best-config["trainer"]["min_delta"]: | |
| best, selected, stale = score, epoch, 0 | |
| state = {k: v.detach().clone() for k, v in model.state_dict().items()} | |
| else: | |
| stale += 1 | |
| logs.append(record) | |
| if validation is not None and stale >= config["trainer"]["early_stopping_patience"]: | |
| break | |
| if state is not None: | |
| model.load_state_dict(state) | |
| return model.eval(), pd.DataFrame(logs), selected | |
| class SimpleMLPPredictor: | |
| def __init__(self, bundle, feature_sets=None): | |
| bundle = Path(bundle) | |
| self.spec = json.loads((bundle/"model.json").read_text()) | |
| self.models, self.preprocessors = {}, {} | |
| for feature_set in (self.spec["feature_sets"] if feature_sets is None else feature_sets): | |
| if feature_set not in self.spec["feature_sets"]: | |
| raise ValueError(f"Unknown feature set: {feature_set}") | |
| pre = dict(np.load(bundle/f"{feature_set}_preprocessor.npz", allow_pickle=False)) | |
| model = build_model(self.spec["model"], len(pre["mean"]), 16) | |
| model.load_state_dict(torch.load(bundle/f"{feature_set}.pt", weights_only=True, map_location="cpu")) | |
| self.models[feature_set], self.preprocessors[feature_set] = model.eval(), pre | |
| def predict(self, events): | |
| for event in events: | |
| if set(event) != {"visit_id", "visit_month", "proteins"}: | |
| raise ValueError("Only current visit_id, visit_month and proteins are accepted") | |
| if len(event["proteins"]) != len(self.spec["proteins"]): | |
| raise ValueError("Protein vocabulary mismatch") | |
| months = np.asarray([e["visit_month"] for e in events], dtype=float) | |
| proteins = np.asarray([e["proteins"] for e in events], dtype=float) | |
| result = {} | |
| for feature_set, model in self.models.items(): | |
| raw = raw_features(months, proteins, feature_set) | |
| x = transform(raw, self.preprocessors[feature_set]) | |
| with torch.no_grad(): | |
| result[feature_set] = model(torch.tensor(x)).numpy() | |
| return result | |