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

Passing nested dict key on CLI gets written to the wrong level #388

Closed
florianblume opened this issue Sep 19, 2023 · 3 comments
Closed

Passing nested dict key on CLI gets written to the wrong level #388

florianblume opened this issue Sep 19, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@florianblume
Copy link

🐛 Bug report

Related to my other issue (#387), when passing the respective nested dict key on the CLI makes the param be written to the wrong level unfortunately.

To reproduce

from jsonargparse import ArgumentParser, ActionConfigFile
import yaml
import tempfile

class ModelBaseClass():
    def __init__(self, batch_size: int=0):
        self.batch_size = batch_size

class ImageModel(ModelBaseClass):
    def __init__(self, optimizer_init: dict, **kwargs):
        super().__init__(**kwargs)
        self.image_size = image_size

parser = ArgumentParser(
    prog='app',
    description='Description for my app.'
)

parser.add_subclass_arguments(ModelBaseClass, 'model')
parser.add_argument("--config", action=ActionConfigFile)

model_config_file = tempfile.NamedTemporaryFile(suffix='.yaml')

with open(model_config_file.name, 'w') as _config_file:
    model = yaml.safe_dump({
        'class_path': '__main__.ImageModel',
        'init_args': {
            'batch_size': 10,
            'optimizer_init': {
                'init_args': {
                    'lr': 0.001,
                    'weight_decay': 0.00001
                }
            }
        }
    }, _config_file)

print(parser.parse_args(['--model=' + model_config_file.name, '--model.init_args.optimizer_init.init_args.lr=0.1']))

Output:

Namespace(config=None, model=Namespace(class_path='__main__.ImageModel', init_args=Namespace(optimizer_init={'init_args': {'lr': 0.001, 'weight_decay': 1e-05}, 'lr': '0.1'}, batch_size=10), __path__=Path_fr(/shared/tmp/tmpa9rk94r2.yaml)))

The new learning rate is not a child of the nested inner init_args but is on the same level as it.

Expected behavior

The new learning rate should be a child of init_args and overwrite the one of the previous config:

Namespace(config=None, model=Namespace(class_path='__main__.ImageModel', init_args=Namespace(optimizer_init={'init_args': {'lr': 0.1, 'weight_decay': 1e-05}}, batch_size=10), __path__=Path_fr(/shared/tmp/tmpa9rk94r2.yaml)))

Environment

jsonargparse version 4.24.1

@florianblume florianblume added the bug Something isn't working label Sep 19, 2023
@mauvilsa
Copy link
Member

Thank you for reporting! I haven't tried to reproduce it yet, but for certain this would be a bug.

Independent from the bug, for a case like an optimizer, instead of an optimizer_init: dict parameter, it is recommended to use dependency injection as explained in callable-type.

@florianblume
Copy link
Author

I modified my code to work with this approach now but it's only a solution for the optimizer init. The problem with the scheduler init is that they all receive a different number and different type of positional args - at least I couldn't get it to work. So I'm sticking with the dict for the schedulers since that's something I will probably not be tuning in the near future. But I guess a concatenation function for dictionaries would be great, too.

@mauvilsa
Copy link
Member

The problem with the scheduler init is that they all receive a different number and different type of positional args

What problem exactly did you face? In lightning how to use schedulers can be seen in multiple-optimizers-and-schedulers. And the type hint used is Callable[[Optimizer], Union[_TORCH_LRSCHEDULER, ReduceLROnPlateau]].

Is the problem ReduceLROnPlateau or with some other scheduler?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants