Última actividad 9 months ago

Save and load hyperparameters for model training and evaluation

Revisión c70b4fa2149facdd5c3c3e900d2a628589d23cbf

hparams_save-load.py Sin formato
1from datetime import datetime
2from pathlib import Path
3
4import yaml
5
6
7# Define the model
8class MyModel:
9 def __init__(
10 self,
11 n_layers: int,
12 n_heads: int,
13 dropout: float,
14 outdir: str | Path,
15 ):
16 self.n_layers = n_layers
17 self.n_heads = n_heads
18 self.dropout = dropout
19 self.outdir = outdir
20
21 @classmethod
22 def from_config(cls, config: Path | str):
23 with open(config, "r") as f:
24 hparams = yaml.safe_load(f)
25 return cls(**hparams)
26
27 def train(self):
28 print("Training...")
29
30 def evaluate(self):
31 print("Evaluating...")
32
33
34# Create the output directory
35now = datetime.now().strftime("%Y%m%d_%H%M%S")
36outdir = Path(f"{now}_runs")
37outdir.mkdir(parents=True, exist_ok=True)
38
39# Instantiate the model
40hparams = {
41 "n_layers": 3,
42 "n_heads": 8,
43 "dropout": 0.1,
44 "outdir": str(outdir),
45}
46
47model = MyModel(**hparams)
48
49# Train the model
50model.train()
51
52# Save the hyperparameters in the same directory
53with open(outdir / "hparams.yaml", "w") as f:
54 yaml.dump(hparams, f)
55
56# Evaluate the model
57model = MyModel.from_config(outdir / "hparams.yaml")
58model.evaluate()
59