1 2
from typing import Any, Dict
2

3 2
from snorkel.types import Config
4

5

6 2
def merge_config(config: Config, config_updates: Dict[str, Any]) -> Config:
7
    """Merge a (potentially nested) dict of kwargs into a config (NamedTuple).
8

9
    Parameters
10
    ----------
11
    config
12
        An instantiated Config to update
13
    config_updates
14
        A potentially nested dict of settings to update in the Config
15

16
    Returns
17
    -------
18
    Config
19
        The updated Config
20

21
    Example
22
    -------
23
    ```
24
    config_updates = {
25
        "n_epochs": 5,
26
        "optimizer_config": {
27
            "lr": 0.001,
28
        }
29
    }
30
    trainer_config = merge_config(TrainerConfig(), config_updates)
31
    ```
32
    """
33 2
    for key, value in config_updates.items():
34 2
        if isinstance(value, dict):
35 2
            config_updates[key] = merge_config(getattr(config, key), value)
36 2
    return config._replace(**config_updates)

Read our documentation on viewing source code .

Loading