lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
384 stars 46 forks source link

scientific notation in dict values doesn't get converted to float #298

Open stas00 opened 6 months ago

stas00 commented 6 months ago

Describe the bug

If I have:

@dataclass
class OptimizerParams:
    name: str = "AdamW"
    params: Dict[str, Any] = dict_field(
        dict(
            # learning rate
            lr=3e-4,
        )
    )

and in a config file I say:

    optimizer:
        name: AdamW
        params:
            lr: 1e-4

it fails to convert 1e-4 to float, leaving it as a string.

it works fine if I use lr: 0.001

I had to hack around it with:

    def __post_init__(self):
        # bug in simple_parsing - in the config file 0.001 becomes a float, but 1e4 remains a string
        if isinstance(self.params["lr"], str):
            self.params["lr"] = float(self.params["lr"])

simple_parsing==0.1.4

Thanks.

stas00 commented 6 months ago

Oddly, there is a related problem, if I have:

@dataclass
class OptimizerParams:
    name: str = "AdamW"
    params: Dict[str, Any] = dict_field(
        dict(
            # learning rate
            lr=3e-4,
            # betas for adam
            betas=[0.9, 0.999],

but in the config file I write:

    optimizer:
        name: AdamW
        params:
            betas: (0.9, 0.99)

it has the same problem, the numbers in betas remains strings.

note, the default uses a list but the config uses a tuple

If I use the list instead of tuple, then it works fine:

            betas: [0.9, 0.99]

why does it matter if the definition just says it's a dict...

lebrice commented 5 months ago

Thanks for posting @stas00 , I'll take a look!