Simple

YAHP can load a YAML file and parse it into the dataclass.

Consider the following YAML called simple.yaml:

foo: 2
bar: 3.0

Now, consider the following script that defines the Hparams and loads this YAML file:

 1# Copyright 2021 MosaicML. All Rights Reserved.
 2
 3import os
 4from dataclasses import dataclass
 5from typing import Optional
 6
 7import yahp as hp
 8
 9
10@dataclass
11class SimpleExample(hp.Hparams):
12    foo: int = hp.required('foo field')
13    bar: float = hp.optional('bar field', default=1.0)
14    baz: Optional[str] = hp.optional('baz', default=None)
15
16
17# load parameters from simple.yaml
18hparams = SimpleExample.create(os.path.join(os.path.dirname(__file__), 'simple.yaml'))
19
20print(hparams)

This script will output:

SimpleExample:
    bar: 3.0
    baz: null
    foo: 2