You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Argument linking when passing model and datamodule to the cli constructor works; like so: MyLightningCLI(MyModel, MyData).
Argument linking when setting model and datamodule via the command line interface using flags like --model, --data and/or --config raises a ValueError saying Target key "model.foo" must be for an individual argument.
See code below.
How to reproduce the bug
frompytorch_lightning.cliimportLightningCLIfrompytorch_lightningimportLightningModule, LightningDataModuleactivations= {
'MaxAbsScaler': 'Sigmoid',
'Normalizer': 'Sigmoid',
'QuantileTransformer': 'Sigmoid',
'RobustScaler': 'PReLU',
'StandardScaler': 'PReLU',
'PowerTransformer': 'TanhShrink',
}
classMyLightningCLI(LightningCLI):
defadd_arguments_to_parser(self, parser):
parser.link_arguments(
"data.scaler", "model.scaler",
compute_fn=lambdascaler: activations.get(scaler, 'linear'),
apply_on='instantiate'
)
classMyModel(LightningModule):
def__init__(self, activation: str) ->None:
super().__init__()
self.save_hyperparameters()
print(activation)
classMyData(LightningDataModule):
def__init__(self, scaler: str) ->None:
super().__init__()
self.save_hyperparameters()
print(scaler)
defmain():
# this works :)# cli = MyLightningCLI(MyModel, MyData)# > python fit .\debug.py --data.scaler MaxAbsScaler# > prints Sigmoid# this does not work :(cli=MyLightningCLI()
# > python .\debug.py fit --data MyData --data.scaler MaxAbsScaler --model MyModel# > ValueError: Target key "model.activation" must be for an individual argument.if__name__=='__main__':
main()
Error messages and logs
Traceback (most recent call last):
File "D:\Users\MC\Documents\ALGO_TRADING\0 BIAS\VAE\debug.py", line 50, in <module>
main()
File "D:\Users\MC\Documents\ALGO_TRADING\0 BIAS\VAE\debug.py", line 45, in main
cli = MyLightningCLI()
File "D:\Users\MC\Documents\ALGO_TRADING\0 BIAS\VAE\.venv\lib\site-packages\pytorch_lightning\cli.py", line 343, in __init__
self.setup_parser(run, main_kwargs, subparser_kwargs)
File "D:\Users\MC\Documents\ALGO_TRADING\0 BIAS\VAE\.venv\lib\site-packages\pytorch_lightning\cli.py", line 403, in setup_parser
self._add_subcommands(self.parser, **subparser_kwargs)
File "D:\Users\MC\Documents\ALGO_TRADING\0 BIAS\VAE\.venv\lib\site-packages\pytorch_lightning\cli.py", line 480, in _add_subcommands
subcommand_parser = self._prepare_subcommand_parser(trainer_class, subcommand, **subparser_kwargs)
File "D:\Users\MC\Documents\ALGO_TRADING\0 BIAS\VAE\.venv\lib\site-packages\pytorch_lightning\cli.py", line 486, in _prepare_subcommand_parser
self._add_arguments(parser)
File "D:\Users\MC\Documents\ALGO_TRADING\0 BIAS\VAE\.venv\lib\site-packages\pytorch_lightning\cli.py", line 439, in _add_arguments
self.add_arguments_to_parser(parser)
File "D:\Users\MC\Documents\ALGO_TRADING\0 BIAS\VAE\debug.py", line 17, in add_arguments_to_parser
parser.link_arguments(
File "D:\Users\MC\Documents\ALGO_TRADING\0 BIAS\VAE\.venv\lib\site-packages\jsonargparse\link_arguments.py", line 379, in link_arguments
ActionLink(self, source, target, compute_fn, apply_on)
File "D:\Users\MC\Documents\ALGO_TRADING\0 BIAS\VAE\.venv\lib\site-packages\jsonargparse\link_arguments.py", line 141, in __init__
raise ValueError(f'Target key "{target}" must be for an individual argument.')
ValueError: Target key "model.scaler" must be for an individual argument.
In pull request omni-us/jsonargparse#218 this error message has been changed to make it more clear what the problem is. Note that the link_arguments in the code above is a mistake and should give an error. The correct link would be:
Great, this works!
For completeness sake I would like to mention that since data.scaler will already be instantiated by the time it goes through the linking process it is no longer a string but an object.
Bug description
Argument linking when passing model and datamodule to the cli constructor works; like so:
MyLightningCLI(MyModel, MyData)
.Argument linking when setting model and datamodule via the command line interface using flags like
--model
,--data
and/or--config
raises a ValueError sayingTarget key "model.foo" must be for an individual argument.
See code below.
How to reproduce the bug
Error messages and logs
Environment
Current environment
More info
No response
cc @carmocca @mauvilsa
The text was updated successfully, but these errors were encountered: