You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit e384cb5
Browse filesBrowse the repository at this point in the historyBrowse files
Instantiating objects with Hydra Example (ebezzam#8)
* Add hydra_instantiate example
* Instantiate Example update
* Instantiate Example Fix Docs
* Instantiate Example Fix Docs
* Instantiate Example Fix Docs and Config
* Instantiate Example Fix Docs
``instantiate`` function from ``hydra.utils`` allows you to define an object in a YAML file
127
+
without being tied to a particular class. To do this, you need to define ``_target_`` in
128
+
your config (see configs in ``configs/signal``) and object initialization arguments. Object class
129
+
can be either defined in your project (``configs/signal/ExampleZeros``, ``configs/signal/ExampleCustom``)
130
+
or taken from a package (``configs/signal/ExampleNumpy``).
131
+
132
+
Note that here we use another Hydra feature: config grouping and splitting. Instead of writing
133
+
configurations for all objects in the main config and copying configuration files, we create a sub-directory ``signal``,
134
+
where all ``signal`` configs are defined. Now we can run the main config with the ``signal`` of
135
+
our choice simply by specifying it in the command line. For example, ``python examples/real_convolve.py signal=ExampleNumpy``
136
+
or ``python examples/real_convolve.py signal=ExampleZeros``.
137
+
138
+
If we need to define some of the arguments inside the code before creating an object, we can pass them directly to the ``instantiate`` function.
139
+
For example, we did not define ``signal_len`` in the ``signal`` configuration file and passed it by hand:
140
+
``signal = instantiate(config.signal, config.signal_len)``. This is especially useful when you have positional-only arguments
141
+
like ``numpy.random.randn`` in our example. Note that we can both define arguments in the configuration file and pass new ones to ``instantiate`` like we did for
142
+
``ExampleCustom``.
143
+
144
+
Object instantiating is recursive, i.e. some of the arguments of the class can also be
145
+
defined using ``_target_`` and they will be created automatically. For example,
146
+
``python examples/real_convolve.py signal=ExampleCustom +signal/transform=power`` defines the ``transform`` argument of
147
+
the ``ExampleCustom`` class as the ``PowerTransform`` class. The ``+signal/transform=power`` in the command line
148
+
means adding the ``transform`` argument to the current ``signal`` configuration from the ``power.yaml`` config defined
149
+
in ``configs/signal/transform``. That is, you can have sub-sub-directories. The default values from sub-sub-directories
150
+
can also be changed in the command-line: ``python examples/real_convolve.py signal=ExampleCustom +signal/transform=power signal.transform.pow=3``
0 commit comments