Skip to content

Question on initializing a Callable #748

@findmyway

Description

@findmyway

For example:

from jax.nn.initializers import Initializer, uniform
from jsonargparse import ArgumentParser
from dataclasses import dataclass


@dataclass
class Model:
    name: str = "default"
    init: Initializer = uniform(0.5)


parser = ArgumentParser()
parser.add_argument("--m", type=Model)
args = parser.parse_args(
    [
        "--m.name",
        "abc",
        "--m.init",
        '{"class_path": "jax.nn.initializers.constant", "init_args": {"value": -7}}',
    ]
)
error: Parser key "m.init":
  Import path jax.nn.initializers.constant does not implement protocol Initializer

Here Initializer is just a Callable Protocol.

Based on the doc:

Callable is supported by either giving a dot import path to a callable object or by giving a dict with a class_path and optionally init_args entries. The specified class must either instantiate into a callable or be a subclass of the return type of the callable.

I think my case above belongs the latter one: by giving a dict with a class_path and optionally init_args entries. However, based on the error message it seems to be checking the class_path instead of the instantiated result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions