LOBench-A-share-processed / code /experiments /tbd_revision /scripts /run_hyperparameter_sensitivity.py
Download code/experiments/tbd_revision/scripts/run_hyperparameter_sensitivity.py from mythezone/LOBench-A-share-processed: direct link, hf CLI and curl.
- Browser
- Download file 4.21 kB
-
https://huggingface.co/datasets/mythezone/LOBench-A-share-processed/resolve/main/code/experiments/tbd_revision/scripts/run_hyperparameter_sensitivity.py
- Command line
-
hf download hf://datasets/mythezone/LOBench-A-share-processed/code/experiments/tbd_revision/scripts/run_hyperparameter_sensitivity.py
-
curl -L -o run_hyperparameter_sensitivity.py https://huggingface.co/datasets/mythezone/LOBench-A-share-processed/resolve/main/code/experiments/tbd_revision/scripts/run_hyperparameter_sensitivity.py
4.21 kB
| #!/usr/bin/env python3 | |
| """Run the prespecified five-stock hyperparameter sensitivity grid.""" | |
| from __future__ import annotations | |
| import argparse | |
| import copy | |
| import json | |
| import sys | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parents[3] | |
| SCRIPT_DIR = ROOT / "experiments/tbd_revision/scripts" | |
| if str(SCRIPT_DIR) not in sys.path: | |
| sys.path.insert(0, str(SCRIPT_DIR)) | |
| from train_representation_baselines import run_one, summarize # noqa: E402 | |
| STOCKS = ("asset_001", "asset_002", "asset_017", "asset_005", "asset_033") | |
| METHODS = ("timae", "lobgrid_timae", "simlob2026") | |
| SEEDS = (2026, 2027, 2028) | |
| # Each axis changes one value from the retained configuration. The baseline | |
| # additionally supplies the three label-budget conditions used elsewhere. | |
| VARIANTS: dict[str, dict[str, float | int]] = { | |
| "baseline": {}, | |
| "latent64": {"latent_dim": 64}, | |
| "latent256": {"latent_dim": 256}, | |
| "width64": {"d_model": 64}, | |
| "width128": {"d_model": 128}, | |
| "heads2": {"nhead": 2}, | |
| "heads8": {"nhead": 8}, | |
| "mask20": {"mask_ratio": 0.2}, | |
| "mask60": {"mask_ratio": 0.6}, | |
| "lr100": {"learning_rate": 0.0001}, | |
| "lr1000": {"learning_rate": 0.001}, | |
| "batch64": {"batch_size": 64}, | |
| "batch256": {"batch_size": 256}, | |
| "wd0": {"weight_decay": 0.0}, | |
| "wd1e4": {"weight_decay": 0.0001}, | |
| } | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--base-config", type=Path, required=True) | |
| parser.add_argument("--output-root", type=Path, required=True) | |
| parser.add_argument( | |
| "--variants", | |
| nargs="+", | |
| choices=tuple(VARIANTS), | |
| required=True, | |
| ) | |
| parser.add_argument("--force", action="store_true") | |
| return parser.parse_args() | |
| def resolved(path: Path) -> Path: | |
| return path if path.is_absolute() else ROOT / path | |
| def main() -> None: | |
| args = parse_args() | |
| base = json.loads(resolved(args.base_config).read_text(encoding="utf-8")) | |
| output_root = resolved(args.output_root) | |
| output_root.mkdir(parents=True, exist_ok=True) | |
| for variant in args.variants: | |
| config = copy.deepcopy(base) | |
| config["training"].setdefault("nhead", 4) | |
| config["training"].setdefault("mask_ratio", 0.4) | |
| config["training"].update(VARIANTS[variant]) | |
| result_dir = output_root / variant | |
| config["results_dir"] = str(result_dir) | |
| config["pretrain_id"] = f"final_revision_hp_{variant}" | |
| config["stocks"] = list(STOCKS) | |
| config["methods"] = list(METHODS) | |
| config["settings"] = ["from_scratch", "fine_tune"] | |
| config["seeds"] = list(SEEDS) | |
| config["label_budgets"] = ( | |
| [0.25, 0.5, 1.0] if variant == "baseline" else [0.5] | |
| ) | |
| config["purpose"] = ( | |
| "Prespecified five-stock, three-seed, one-factor-at-a-time " | |
| f"sensitivity condition: {variant}." | |
| ) | |
| result_dir.mkdir(parents=True, exist_ok=True) | |
| (result_dir / "resolved_config.json").write_text( | |
| json.dumps(config, indent=2) + "\n", | |
| encoding="utf-8", | |
| ) | |
| total = ( | |
| len(STOCKS) | |
| * len(METHODS) | |
| * len(SEEDS) | |
| * len(config["label_budgets"]) | |
| ) | |
| completed = 0 | |
| for stock in STOCKS: | |
| for method in METHODS: | |
| for seed in SEEDS: | |
| for label_budget in config["label_budgets"]: | |
| completed += 1 | |
| print( | |
| f"VARIANT {variant} {completed}/{total} " | |
| f"{stock} {method} seed={seed} " | |
| f"budget={label_budget}", | |
| flush=True, | |
| ) | |
| run_one( | |
| config, | |
| stock, | |
| method, | |
| seed, | |
| label_budget, | |
| config["settings"], | |
| force=args.force, | |
| ) | |
| summarize(config, f"hyperparameter_{variant}") | |
| if __name__ == "__main__": | |
| main() | |