How do I skip an argument of a callable?
In this example
from jsonargparse import ArgumentParser, CLI
import torch
from typing import Callable
from dataclasses import dataclass
@dataclass
class Foo:
opt: Callable[[torch.nn.Parameter], torch.optim.Optimizer]
parser = ArgumentParser()
parser.add_class_arguments(Foo)
args = parser.parse_args()
print(args)
# python script.py --opt torch.optim.Adam
How would I skip the lr?
I tried both:
parser.add_class_arguments(Foo, skip={"opt.lr"})
parser.add_class_arguments(Foo, skip={"opt.init_args.lr"})
but they do nothing.
I noticed that:
opt: Callable[[torch.nn.Parameter, float], torch.optim.Optimizer]
will work assuming that the lr is the second positional argument of all optimizers, which might not be always true.
Thanks for the help!
You don't need to skip lr. The type would be Callable[[torch.nn.Parameter], torch.optim.Optimizer]. If a class path is given to an optimizer that requires lr, then lr must also be provided. The fact that lr is positional is not important. At least, this is how it is supposed to work. Did something not work correctly for you?
I tried both:
parser.add_class_arguments(Foo, skip={"opt.lr"}) parser.add_class_arguments(Foo, skip={"opt.init_args.lr"})
but they do nothing.
I think skips with a dot notation are not supported. But still, no need to skip for the case of optimizers.
Sorry, what I meant is that I want to skip the lr. I dont want to expose it to the command line or config. My code will set a value automatically (not represented in the repro snippet above).
Okay, then this is not currently supported.