Skip to content

How to pass a reference to a function in yaml config file #13613

Discussion options

You must be logged in to vote

This is supported in jsonargparse. Just use Callable as type hint, see its mention in the docs. Basically you would have:

class MyData:
    def __init__(self, data_dir: str, filter: Callable):
        ...

Then the config could be as you had it:

class_path: foo.datasets.my_data
init_args:
  data_dir: data
  filter: foo.datasets.filter1

Or filter could be a class that once instantiated becomes callable, like:

class_path: foo.datasets.my_data
init_args:
  data_dir: data
  filter:
    class_path: path.to.callable.class
    init_args:
      param1: val1
      ...

Replies: 3 comments 11 replies

Comment options

You must be logged in to vote
3 replies
@kenenbek
Comment options

@tshu-w
Comment options

@mauvilsa
Comment options

Comment options

You must be logged in to vote
7 replies
@chenjoya
Comment options

@mauvilsa
Comment options

@chenjoya
Comment options

@mauvilsa
Comment options

@klieret
Comment options

Answer selected by akihironitta
Comment options

You must be logged in to vote
1 reply
@chenjoya
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lightningcli pl.cli.LightningCLI pl Generic label for PyTorch Lightning package
6 participants