debeir.training.hparm_tuning.config

 1import dataclasses
 2import json
 3from typing import Dict
 4
 5from debeir.core.config import Config
 6from debeir.training.hparm_tuning.types import HparamTypes
 7
 8
 9@dataclasses.dataclass(init=True)
10class HparamConfig(Config):
11    """
12        Hyperparameter configuration file
13
14        Expects a dictionary of hyperparameters
15
16        hparams: Dict
17        {
18            "learning_rate": {
19               "type": float
20               "low": 0.1
21               "high": 1.0
22               "step": 0.1
23               # OR
24               args: [0.1, 1.0, 0.1]
25            },
26        }
27    """
28
29    hparams: Dict[str, Dict]
30
31    @classmethod
32    def from_json(cls, fp) -> "HparamConfig":
33        return HparamConfig(json.load(open(fp)))
34
35    def validate(self):
36        # Self-validating, errors will be raised if initializations of any object fails.
37        return True
38
39    def parse_config_to_py(self):
40        """
41        Parses configuration file into usable python objects
42        """
43        hparams = {}
44
45        for hparam, value in self.hparams.items():
46            # if "args" in value:  # Of the form {"learning rate": {args: [0.1, 1.0, 0.1]}}
47            #    hparam_obj = hparam_type(name=hparam, *value["args"])
48            if isinstance(value, Dict) and "type" in value:
49                hparam_type = HparamTypes[value['type']]
50                value.pop("type")
51                hparam_obj = hparam_type(name=hparam, **value)
52            else:
53                hparam_obj = value
54
55            hparams[hparam] = hparam_obj
56
57        return hparams
@dataclasses.dataclass(init=True)
class HparamConfig(debeir.core.config.Config):
10@dataclasses.dataclass(init=True)
11class HparamConfig(Config):
12    """
13        Hyperparameter configuration file
14
15        Expects a dictionary of hyperparameters
16
17        hparams: Dict
18        {
19            "learning_rate": {
20               "type": float
21               "low": 0.1
22               "high": 1.0
23               "step": 0.1
24               # OR
25               args: [0.1, 1.0, 0.1]
26            },
27        }
28    """
29
30    hparams: Dict[str, Dict]
31
32    @classmethod
33    def from_json(cls, fp) -> "HparamConfig":
34        return HparamConfig(json.load(open(fp)))
35
36    def validate(self):
37        # Self-validating, errors will be raised if initializations of any object fails.
38        return True
39
40    def parse_config_to_py(self):
41        """
42        Parses configuration file into usable python objects
43        """
44        hparams = {}
45
46        for hparam, value in self.hparams.items():
47            # if "args" in value:  # Of the form {"learning rate": {args: [0.1, 1.0, 0.1]}}
48            #    hparam_obj = hparam_type(name=hparam, *value["args"])
49            if isinstance(value, Dict) and "type" in value:
50                hparam_type = HparamTypes[value['type']]
51                value.pop("type")
52                hparam_obj = hparam_type(name=hparam, **value)
53            else:
54                hparam_obj = value
55
56            hparams[hparam] = hparam_obj
57
58        return hparams

Hyperparameter configuration file

Expects a dictionary of hyperparameters

hparams: Dict { "learning_rate": { "type": float "low": 0.1 "high": 1.0 "step": 0.1 # OR args: [0.1, 1.0, 0.1] }, }

HparamConfig(hparams: Dict[str, Dict])
@classmethod
def from_json(cls, fp) -> debeir.training.hparm_tuning.config.HparamConfig:
32    @classmethod
33    def from_json(cls, fp) -> "HparamConfig":
34        return HparamConfig(json.load(open(fp)))
def validate(self):
36    def validate(self):
37        # Self-validating, errors will be raised if initializations of any object fails.
38        return True

Validates if the config is correct. Must be implemented by inherited classes.

def parse_config_to_py(self):
40    def parse_config_to_py(self):
41        """
42        Parses configuration file into usable python objects
43        """
44        hparams = {}
45
46        for hparam, value in self.hparams.items():
47            # if "args" in value:  # Of the form {"learning rate": {args: [0.1, 1.0, 0.1]}}
48            #    hparam_obj = hparam_type(name=hparam, *value["args"])
49            if isinstance(value, Dict) and "type" in value:
50                hparam_type = HparamTypes[value['type']]
51                value.pop("type")
52                hparam_obj = hparam_type(name=hparam, **value)
53            else:
54                hparam_obj = value
55
56            hparams[hparam] = hparam_obj
57
58        return hparams

Parses configuration file into usable python objects