Skip to content

light_hyper_dict

LightHyperDict

Bases: FileConfig

Lightning hyperparameter dictionary.

This class extends the FileConfig class to provide a dictionary for storing hyperparameters.

Attributes:

Name Type Description
filename str

The name of the file where the hyperparameters are stored.

Source code in spotPython/hyperdict/light_hyper_dict.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class LightHyperDict(base.FileConfig):
    """Lightning hyperparameter dictionary.

    This class extends the FileConfig class to provide a dictionary for storing hyperparameters.

    Attributes:
        filename (str):
            The name of the file where the hyperparameters are stored.
    """

    def __init__(
        self,
        filename: str = "light_hyper_dict.json",
        directory: None = None,
    ) -> None:
        super().__init__(filename=filename, directory=directory)
        self.filename = filename
        self.directory = directory
        self.hyper_dict = self.load()

    @property
    def path(self):
        if self.directory:
            return pathlib.Path(self.directory).joinpath(self.filename)
        return pathlib.Path(__file__).parent.joinpath(self.filename)

    def load(self) -> dict:
        """Load the hyperparameters from the file.

        Returns:
            dict: A dictionary containing the hyperparameters.

        Examples:
            >>> from spotPython.hyperdict.light_hyper_dict import LightHyperDict
                lhd = LightHyperDict()
                lhd.hyper_dict
                {'NetLightRegression': {'l1': {'type': 'int',
                'default': 3,
                'transform': 'transform_power_2_int',
                'lower': 3,
                'upper': 8},
                'epochs': {'type': 'int',
                'default': 4,
                'transform': 'transform_power_2_int',
                'lower': 4,
                'upper': 9},
                ...
                'transform': 'None',
                'class_name': 'torch.optim',
                'core_model_parameter_type': 'str',
                'lower': 0,
                'upper': 11}}}
            # Assume the user specified file `user_hyper_dict.json` is in the `./hyperdict/` directory.
            >>> user_lhd = LightHyperDict(filename='user_hyper_dict.json', directory='./hyperdict/')
        """
        with open(self.path, "r") as f:
            d = json.load(f)
        return d

load()

Load the hyperparameters from the file.

Returns:

Name Type Description
dict dict

A dictionary containing the hyperparameters.

Examples:

>>> from spotPython.hyperdict.light_hyper_dict import LightHyperDict
    lhd = LightHyperDict()
    lhd.hyper_dict
    {'NetLightRegression': {'l1': {'type': 'int',
    'default': 3,
    'transform': 'transform_power_2_int',
    'lower': 3,
    'upper': 8},
    'epochs': {'type': 'int',
    'default': 4,
    'transform': 'transform_power_2_int',
    'lower': 4,
    'upper': 9},
    ...
    'transform': 'None',
    'class_name': 'torch.optim',
    'core_model_parameter_type': 'str',
    'lower': 0,
    'upper': 11}}}
# Assume the user specified file `user_hyper_dict.json` is in the `./hyperdict/` directory.
>>> user_lhd = LightHyperDict(filename='user_hyper_dict.json', directory='./hyperdict/')
Source code in spotPython/hyperdict/light_hyper_dict.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def load(self) -> dict:
    """Load the hyperparameters from the file.

    Returns:
        dict: A dictionary containing the hyperparameters.

    Examples:
        >>> from spotPython.hyperdict.light_hyper_dict import LightHyperDict
            lhd = LightHyperDict()
            lhd.hyper_dict
            {'NetLightRegression': {'l1': {'type': 'int',
            'default': 3,
            'transform': 'transform_power_2_int',
            'lower': 3,
            'upper': 8},
            'epochs': {'type': 'int',
            'default': 4,
            'transform': 'transform_power_2_int',
            'lower': 4,
            'upper': 9},
            ...
            'transform': 'None',
            'class_name': 'torch.optim',
            'core_model_parameter_type': 'str',
            'lower': 0,
            'upper': 11}}}
        # Assume the user specified file `user_hyper_dict.json` is in the `./hyperdict/` directory.
        >>> user_lhd = LightHyperDict(filename='user_hyper_dict.json', directory='./hyperdict/')
    """
    with open(self.path, "r") as f:
        d = json.load(f)
    return d