CellTrainer¶
- class CellTrainer(**kwargs)[source]¶
Bases:
ModuleBase trainer for updating cell parameters.
Important
The
Layerobject “owns” the py:class:~inferno.neural.Cell objects whereas theCellTrainerowns theMonitorobjects.If applying a function to
Moduleobjects, e.g. via callingto()on theCellTrainer,~inferno.neural.Cellobjects will not be altered but~inferno.observe.Monitorand auxiliary state objects will be.Likewise, calling
to()on theLayerwill alter~inferno.neural.Cellobjects but not~inferno.observe.Monitorand auxiliary state objects.- add_cell(name: str, cell: Cell, state: Module | None = None, params: Sequence[str] | None = None) tuple[Cell, Module | None][source]¶
Adds a cell and any auxiliary state.
- Parameters:
- Raises:
ValueError – a cell with the specified name already exists.
RuntimeError – given cell has no updater.
- Returns:
added cell and auxiliary state.
- Return type:
- add_monitor(cell: str, name: str, attr: str, monitor: MonitorConstructor, unique: bool = False, /, **tags: Any) Monitor[source]¶
Adds a monitor to a trainable.
- Parameters:
cell (str) – name of the cell to which the monitor will be added.
name (str) – name of the monitor to add (unique to the cell).
attr (str) – dot-separated attribute to monitor, relative to the cell.
monitor (MonitorConstructor) – partial constructor for the monitor.
unique (bool) – if the monitor should not be aliased from the pool regardless. Defaults to
False.**tags (Any) – tags to determine if the monitor is unique amongst monitors with the same name, class, and reducer class.
- Raises:
AttributeError – specified cell does not exist.
- Returns:
added or retrieved monitor.
- Return type:
Important
Monitors are aliased based off of their name, resolved attribute path (i.e. that which is used by the layer), and any added tags. This attribute is added as a tag
"attr".Tip
Setting
uniquetoTruewill bypass any test ontagsand guarantee the monitor will be unique. This is less efficient, but if, for instance, monitor properties may change over the course of training, this should be used. WhenTrueis also guaranteed to delete an existing monitor and return the new one.
- property cells: Iterator[tuple[Cell, Module | None]]¶
Added cells and their auxiliary states.
- Yields:
Cell – added cells and their auxiliary states.
- clear(**kwargs) None[source]¶
Clears all of the monitors for the trainer.
Note
Keyword arguments are passed to
clear()call.Note
If a subclassed trainer has additional state, this should be overridden to delete that state as well. This however doesn’t delete updater state as it may be shared across trainers.
- del_cell(name: str) None[source]¶
Deletes an added cell.
- Parameters:
name (str) – name of the cell to delete.
- Raises:
AttributeError – specified cell does not exist.
Important
This does not strictly delete the cell, it is still owned by its
Layerand can be added again. It is only removed from the trainer. However its associated state and monitors are deleted.
- del_monitor(cell: str, name: str) None[source]¶
Deletes an added monitor.
- Parameters:
- Raises:
AttributeError – specified cell does not exist, or does not have a monitor with the specified name added to it.
- forward(*inputs, **kwargs)[source]¶
Processes a training step.
- Raises:
NotImplementedError –
forwardmust be implemented by the subclass.
- property monitors: Iterator[Monitor]¶
Added monitors.
Because monitors for each
~inferno.neural.CellTrainerare pooled together for each trainer, duplicate monitors are not created where possible. The number of monitors here may be less than the number of added monitors.- Yields:
Monitor – added monitors.
- property named_cells: Iterator[tuple[str, tuple[Cell, Module | None]]]¶
Added cell, their auxiliary states, and their names.
- Yields:
tuple[str, Cell] – tuple of an added cell, their auxiliary states, and its name.
- property named_monitors: Iterator[tuple[tuple[str, str], Monitor]]¶
Iterable of added monitors and tuples of the cell and monitor name.
- Yields:
tuple[tuple[str, str], Monitor] – tuple of an added monitor and a tuple of the cell name and monitor name corresponding to it.
- named_monitors_of(cell: str) Iterator[tuple[str, Monitor]][source]¶
Monitors associated with a given cell.
- Parameters:
cell (str) – name of the cell to get associated monitors of.
- Yields:
tuple[str, Monitor] – associated monitors and their names.
- train(mode: bool = True) CellTrainer[source]¶
Override of module’s train method.
Automatically registers and deregisters monitors. Does not put
Cellobjects into training mode. Does not clear monitor state (callclear()to do so).- Parameters:
mode (bool, optional) – if the trainer should be set in training mode (
True) or evaluation mode (False). Defaults toTrue.- Returns:
self.
- Return type:
- update(**kwargs) None[source]¶
Applies all cumulative updates.
This calls every updater which applies cumulative updates and any updater hooks are automatically called (e.g. parameter clamping). The updaters will each be called once, even if present in multiple cells.
This will apply all updates, not only those created by this trainer.