Spaces:
Sleeping
Sleeping
Download src/utils/experiment.py from koushik2109/mindscope-Model: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/koushik2109/mindscope-Model/resolve/main/src/utils/experiment.py
- Command line
-
hf download hf://spaces/koushik2109/mindscope-Model/src/utils/experiment.py
-
curl -L -o experiment.py https://huggingface.co/spaces/koushik2109/mindscope-Model/resolve/main/src/utils/experiment.py
1.07 kB
| from __future__ import annotations | |
| import json | |
| import random | |
| from dataclasses import dataclass, asdict | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| class ExperimentConfig: | |
| name: str | |
| seed: int = 42 | |
| run_directory: str = "runs" | |
| class ExperimentManager: | |
| def __init__(self, config: ExperimentConfig): | |
| self.config = config | |
| self.root = Path(config.run_directory) / config.name | |
| self.root.mkdir(parents=True, exist_ok=True) | |
| self.seed(config.seed) | |
| def seed(self, seed: int) -> None: | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| torch.manual_seed(seed) | |
| if torch.cuda.is_available(): | |
| torch.cuda.manual_seed_all(seed) | |
| def save_config(self, filename: str = "config.json") -> Path: | |
| path = self.root / filename | |
| with open(path, "w", encoding="utf-8") as handle: | |
| json.dump(asdict(self.config), handle, indent=2) | |
| return path | |
| def checkpoint_path(self, name: str = "best_checkpoint.pt") -> Path: | |
| return self.root / name | |