Skip to content

Commit

Permalink
Updated instantiate docs to reflect positional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Mar 11, 2021
1 parent 2ae8791 commit 6da7da2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion hydra/_internal/instantiate/_instantiate2.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def instantiate(config: Any, *args: Any, **kwargs: Any) -> Any:
In addition to the parameters, the config must contain:
_target_ : target class or callable name (str)
And may contain:
_args_: List-like of positional arguments to pass to the target
_recursive_: Construct nested objects as well (bool).
True by default.
may be overridden via a _recursive_ key in
Expand All @@ -121,7 +122,6 @@ def instantiate(config: Any, *args: Any, **kwargs: Any) -> Any:
the exception of Structured Configs (and their fields).
all : Passed objects are dicts, lists and primitives without
a trace of OmegaConf containers
_args_: List-like of positional arguments
:param args: Optional positional parameters pass-through
:param kwargs: Optional named parameters to override
parameters in the config object. Parameters not present
Expand Down
24 changes: 22 additions & 2 deletions website/docs/advanced/instantiate_objects/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def instantiate(config: Any, *args: Any, **kwargs: Any) -> Any:
In addition to the parameters, the config must contain:
_target_ : target class or callable name (str)
And may contain:
_args_: List-like of positional arguments to pass to the target
_recursive_: Construct nested objects as well (bool).
True by default.
may be overridden via a _recursive_ key in
Expand All @@ -46,16 +47,24 @@ def instantiate(config: Any, *args: Any, **kwargs: Any) -> Any:
:return: if _target_ is a class name: the instantiated object
if _target_ is a callable: the return value of the call
"""

# Alias for instantiate
call = instantiate
```

</details><br/>

The config passed to these functions must have a key called `_target_`, with the value of a fully qualified class name, class method, static method or callable.
Any additional parameters are passed as keyword arguments to the target.
The config passed to these functions must have a key called `_target_`, with the value of a fully qualified class name, class method, static method or callable.
For convenience, `None` config results in a `None` object.

**Named arguments** : Config fields (except reserved fields like `_target_`) are passed as named arguments to the target.
Named arguments in the config can be overridden by passing named argument with the same name in the `instantiate()` call-site.

**Positional arguments** : The config may contain a `_args_` field representing positional arguments to pass to the target.
The positional arguments can be overridden together by passing positional arguments in the `instantiate()` call-site.



### Simple usage
Your application might have an Optimizer class:
```python title="Example class"
Expand Down Expand Up @@ -158,6 +167,17 @@ print(trainer)
# )
```

Similarly, positional arguments of nested objects can be overridden:
```python
obj = instantiate(
cfg.object,
# pass 1 and 2 as positional arguments to the target object
1, 2,
# pass 3 and 4 as positional arguments to a nested child object
child={"_args_": [3, 4]},
)
```

### Disable recursive instantiation
You can disable recursive instantiation by setting `_recursive_` to `False` in the config node or in the call-site
In that case the Trainer object will receive an OmegaConf DictConfig for nested dataset and optimizer instead of the instantiated objects.
Expand Down

0 comments on commit 6da7da2

Please sign in to comment.