CellTrainer

class CellTrainer(**kwargs)[source]

Bases: Module

Base trainer for updating cell parameters.

Important

The Layer object “owns” the py:class:~inferno.neural.Cell objects whereas the CellTrainer owns the Monitor objects.

If applying a function to Module objects, e.g. via calling to() on the CellTrainer, ~inferno.neural.Cell objects will not be altered but ~inferno.observe.Monitor and auxiliary state objects will be.

Likewise, calling to() on the Layer will alter ~inferno.neural.Cell objects but not ~inferno.observe.Monitor and 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:
  • name (str) – name of the cell to add.

  • cell (Cell) – cell to add.

  • state (nn.Module | None, optional) – any extra state to add. Defaults to None.

  • params (Sequence[str] | None, optional) – trainable parameters the cell updater must have. Defaults to None.

Raises:
Returns:

added cell and auxiliary state.

Return type:

tuple[Cell | None, nn.Module | None]

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:

Monitor

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 unique to True will bypass any test on tags and 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. When True is 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 Layer and 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:
  • cell (str) – name of the cell to which the monitor was added.

  • name (str) – name of the monitor.

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:

NotImplementedErrorforward must be implemented by the subclass.

get_cell(name: str) tuple[Cell, Module | None][source]

Gets an added cell and any added state.

Parameters:

name (str) – name of the trainable to get.

Returns:

specified trainable, if it exists.

Return type:

tuple[Cell, nn.Module | None]

get_monitor(cell: str, name: str) Monitor | None[source]

Gets an added monitor.

Parameters:
  • cell (str) – name of the cell to which the monitor was added.

  • name (str) – name of the monitor.

Returns:

specified monitor, if it exists.

Return type:

Monitor | None

property monitors: Iterator[Monitor]

Added monitors.

Because monitors for each ~inferno.neural.CellTrainer are 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 Cell objects into training mode. Does not clear monitor state (call clear() to do so).

Parameters:

mode (bool, optional) – if the trainer should be set in training mode (True) or evaluation mode (False). Defaults to True.

Returns:

self.

Return type:

CellTrainer

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.