Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to merge two config? #29

Open
unsky opened this issue Aug 22, 2019 · 3 comments
Open

how to merge two config? #29

unsky opened this issue Aug 22, 2019 · 3 comments

Comments

@unsky
Copy link

unsky commented Aug 22, 2019

config1.py:

from yacs.config import CfgNode as CN
_C = CN()
_C.SYSTEM = CN()
# Number of GPUS to use in the experiment
_C.SYSTEM.NUM_GPUS = 8
# Number of workers for doing things
_C.SYSTEM.NUM_WORKERS = 4

config2.py:

from yacs.config import CfgNode as CN
_C = CN()
_C.TRAIN = CN()
# A very important hyperparameter
_C.TRAIN.HYPERPARAMETER_1 = 0.1
# The all important scales for the stuff
_C.TRAIN.SCALES = (2, 4, 8, 16)

how meger two config?

@lancejchen
Copy link

lancejchen commented Aug 22, 2019

package yacs is actually a dict wrapper. So first you read the first config as usual, let's say as Variable CONFIG, then you read the second config as a new python dict (e.g. variable a), then update the CONFIG variable using items in "a". The following function can be used:

def deep_update(source, overrides):
    for key, value in overrides.iteritems():
        if isinstance(value, collections.Mapping) and value:
            returned = deep_update(source.get(key, {}), value)
            returned = CfgNode(returned)
            source[key] = returned
        else:
            source[key] = overrides[key]
    return source

@RizhaoCai
Copy link

_C = CN(new_allowed=True)

It works for me.

@harrygcoppock
Copy link

You can use the 'merge_from_other_cfg' method:

def merge_from_other_cfg(self, cfg_other):

e.g.

config1 = CN()
config1.BLAH = blah

config2 = CN()
config2.FOO = bar

config1.set_new_allowed(TRUE) # recursively update set_new_allowed to allow merging of configs and subconfigs
config1.merge_from_other_cfg(config2)```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants