hparams_save-load.py
· 1.2 KiB · Python
Sin formato
from datetime import datetime
from pathlib import Path
import yaml
# Define the model
class MyModel:
def __init__(
self,
n_layers: int,
n_heads: int,
dropout: float,
outdir: str | Path,
):
self.n_layers = n_layers
self.n_heads = n_heads
self.dropout = dropout
self.outdir = outdir
@classmethod
def from_config(cls, config: Path | str):
with open(config, "r") as f:
hparams = yaml.safe_load(f)
return cls(**hparams)
def train(self):
print("Training...")
def evaluate(self):
print("Evaluating...")
# Create the output directory
now = datetime.now().strftime("%Y%m%d_%H%M%S")
outdir = Path(f"{now}_runs")
outdir.mkdir(parents=True, exist_ok=True)
# Instantiate the model
hparams = {
"n_layers": 3,
"n_heads": 8,
"dropout": 0.1,
"outdir": str(outdir),
}
model = MyModel(**hparams)
# Train the model
model.train()
# Save the hyperparameters in the same directory
with open(outdir / "hparams.yaml", "w") as f:
yaml.dump(hparams, f)
# Evaluate the model
model = MyModel.from_config(outdir / "hparams.yaml")
model.evaluate()
| 1 | from datetime import datetime |
| 2 | from pathlib import Path |
| 3 | |
| 4 | import yaml |
| 5 | |
| 6 | |
| 7 | # Define the model |
| 8 | class 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 |
| 35 | now = datetime.now().strftime("%Y%m%d_%H%M%S") |
| 36 | outdir = Path(f"{now}_runs") |
| 37 | outdir.mkdir(parents=True, exist_ok=True) |
| 38 | |
| 39 | # Instantiate the model |
| 40 | hparams = { |
| 41 | "n_layers": 3, |
| 42 | "n_heads": 8, |
| 43 | "dropout": 0.1, |
| 44 | "outdir": str(outdir), |
| 45 | } |
| 46 | |
| 47 | model = MyModel(**hparams) |
| 48 | |
| 49 | # Train the model |
| 50 | model.train() |
| 51 | |
| 52 | # Save the hyperparameters in the same directory |
| 53 | with open(outdir / "hparams.yaml", "w") as f: |
| 54 | yaml.dump(hparams, f) |
| 55 | |
| 56 | # Evaluate the model |
| 57 | model = MyModel.from_config(outdir / "hparams.yaml") |
| 58 | model.evaluate() |
| 59 |