Download scripts/train_temporal_mlp.py from SparseWake/sparsewake: direct link, hf CLI and curl.
- Browser
- Download file 2.39 kB
-
https://huggingface.co/datasets/SparseWake/sparsewake/resolve/fea459b697ba88cd3c2528566906cc32db8b59f4/scripts/train_temporal_mlp.py
- Command line
-
hf download hf://datasets/SparseWake/sparsewake@fea459b697ba88cd3c2528566906cc32db8b59f4/scripts/train_temporal_mlp.py
-
curl -L -o train_temporal_mlp.py https://huggingface.co/datasets/SparseWake/sparsewake/resolve/fea459b697ba88cd3c2528566906cc32db8b59f4/scripts/train_temporal_mlp.py
2.39 kB
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import sys | |
| import numpy as np | |
| import yaml | |
| ROOT = Path(__file__).resolve().parents[1] | |
| sys.path.insert(0, str(ROOT / "src")) | |
| from sparsewake.data import load_h5 | |
| from sparsewake.evaluate import evaluate_predictions, predict | |
| from sparsewake.features import build_design_matrix | |
| from sparsewake.splits import pose_holdout_split | |
| from sparsewake.train import standardize_train_val_test, train_temporal_mlp | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--config", required=True) | |
| parser.add_argument("--data", default=None) | |
| parser.add_argument("--quick", action="store_true") | |
| parser.add_argument("--out", default="tables/quick_train_metrics.json") | |
| args = parser.parse_args() | |
| cfg = yaml.safe_load(Path(args.config).read_text()) | |
| data_path = Path(args.data) if args.data else ROOT / cfg["data"] | |
| data = load_h5(data_path, input_key=cfg.get("input_key", "X_raw")) | |
| history = 4 if args.quick else int(cfg.get("history", 24)) | |
| x, idx = build_design_matrix(data, feature_set=cfg.get("feature_set", "raw_norm"), history=history) | |
| y = data["target"][idx] | |
| pose_id = data["pose_id"][idx] | |
| train_idx, val_idx, test_idx = pose_holdout_split(pose_id, seed=int(cfg.get("seed", 1))) | |
| if args.quick: | |
| train_idx = train_idx[: min(len(train_idx), 512)] | |
| val_idx = val_idx[: min(len(val_idx), 128)] | |
| test_idx = test_idx[: min(len(test_idx), 128)] | |
| x, _, _ = standardize_train_val_test(x, train_idx) | |
| output_dim = 3 if cfg.get("target", "location") == "location_theta" else 2 | |
| model = train_temporal_mlp( | |
| x, | |
| y, | |
| train_idx, | |
| val_idx, | |
| output_dim=output_dim, | |
| epochs=3 if args.quick else int(cfg.get("epochs", 50)), | |
| batch_size=int(cfg.get("batch_size", 1024)), | |
| seed=int(cfg.get("seed", 1)), | |
| ) | |
| pred = predict(model, x[test_idx]) | |
| metrics = evaluate_predictions(y[test_idx, :output_dim], pred) | |
| metrics["quick_mode"] = bool(args.quick) | |
| metrics["n_train"] = int(len(train_idx)) | |
| metrics["n_test"] = int(len(test_idx)) | |
| out = ROOT / args.out | |
| out.parent.mkdir(parents=True, exist_ok=True) | |
| out.write_text(json.dumps(metrics, indent=2) + "\n") | |
| print(json.dumps(metrics, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |