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

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

Open
stas00 opened this issue Jan 4, 2024 · 2 comments
Open

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

stas00 opened this issue Jan 4, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@stas00
Copy link
Contributor

stas00 commented Jan 4, 2024

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
Copy link
Contributor Author

stas00 commented Jan 4, 2024

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
Copy link
Owner

lebrice commented Jan 18, 2024

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

@lebrice lebrice self-assigned this Jan 18, 2024
@lebrice lebrice added the bug Something isn't working label Jan 18, 2024
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