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

Add new resolver oc.create #645

Closed
odelalleau opened this issue Mar 25, 2021 · 0 comments · Fixed by #677
Closed

Add new resolver oc.create #645

odelalleau opened this issue Mar 25, 2021 · 0 comments · Fixed by #677
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@odelalleau
Copy link
Collaborator

Motivation

A new resolver oc.create would make it easier to dynamically generate config objects from lists and dictionaries.

For instance imagine you have the following JSON file model.json with the following content:

{"name": "resnet", "n_units_per_layer": [100, 1000, 500]}

You want to load this JSON within an existing config. This could be achieved with:

import json
from omegaconf import DictConfig, OmegaConf

# Define a custom resolver to load JSON files.

def load_json(path):
    with open(path) as f:
        return json.load(f)

OmegaConf.register_new_resolver("load_json", load_json)

# Combine it with `oc.create` to generate a DictConfig with the content of this file.

cfg = OmegaConf.create({"model": "${oc.create:${load_json:'model.json'}}"})
assert isinstance(cfg.model, DictConfig)
assert cfg.model.n_units_per_layer[1]  == 1000

If oc.create wasn't used, then cfg.model would be a plain dictionary that wouldn't support all DictConfig features (like accessing a field with the syntax cfg.model.n_units_per_layer)

Alternatives

It is possible for custom resolvers to explicitly call OmegaConf.create(..., parent=_parent_) to always generate config nodes. The objective of introducing oc.create is (i) to make this more straightforward and (ii) to add more flexibility, so that config nodes are only generated when explicitly requested (there are situations where keeping plain dicts / lists may be preferred).

Additional context

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

Successfully merging a pull request may close this issue.

3 participants