Layer

class Layer[source]

Bases: Module, ABC

Representation of simultaneously processed connections and neurons.

add_cell(connection: str, neuron: str) Cell[source]

Creates and adds a cell if it doesn’t exist.

If a cell already exists with the given connection and neuron, this will return the existing cell rather than create a new one.

Parameters:
  • connection (str) – name of the connection for the cell to add.

  • neuron (str) – name of the neuron for the cell to add.

Raises:
Returns:

cell specified by the connection and neuron.

Return type:

Cell

add_connection(name: str, connection: Connection) Connection[source]

Adds a new connection.

Parameters:
  • name (str) – name of the connection to add.

  • connection (Connection) – connection to add.

Raises:

RuntimeErrorname already specifies a connection

Returns:

added connection.

Return type:

Connection

add_neuron(name: str, neuron: Neuron) Neuron[source]

Adds a new neuron.

Parameters:
  • name (str) – name of the neuron to add.

  • neuron (Neuron) – neuron to add.

Raises:

RuntimeErrorname already specifies a neuron

Returns:

added neuron.

Return type:

Neuron

property cells: Proxy

Registered cells.

For a given connection_name and neuron_name, the Cell constructed via layer.add_cell(connection_name, neuron_name) can be accessed as layer.cells.connection_name.neuron_name.

It can be modified in-place (including setting other attributes, adding monitors, etc), but it can neither be deleted nor reassigned.

This is primarily used when targeting Cell objects with a monitor.

Returns:

safe access to registered cells.

Return type:

Proxy

clear(submodules: bool = True, **kwargs) None[source]

Clears the state of the layer.

Parameters:
  • submodules (bool, optional) – if the state of connections and neurons should also be cleared. Defaults to True.

  • **kwargs (Any) – keyword arguments passed to connection and neuron submodule clear methods, if submodules is True.

property connections: Proxy

Registered connections.

For a given name of a Connection set via layer.add_connection(name), it can be accessed as layer.connections.name.

It can be modified in-place (including setting other attributes, adding monitors, etc), but it can neither be deleted nor reassigned.

This is primarily used when targeting Connection objects with a monitor.

Returns:

safe access to registered connections.

Return type:

Proxy

del_cell(connection: str, neuron: str) None[source]

Deletes a created cell if it exists.

Even if a cell hasn’t been created with the given pair, if the pair is valid, this will not raise an error.

Parameters:
  • connection (str) – name of the connection for the cell to delete.

  • neuron (str) – name of the neuron for the cell to delete.

Raises:
del_connection(name: str) None[source]

Deletes an existing connection.

Parameters:

name (str) – name of the connection to delete.

Raises:

AttributeErrorname does not specify a connection.

del_neuron(name: str) None[source]

Deletes an existing neuron.

Parameters:

name (str) – name of the neuron to delete.

Raises:

ValueErrorname does not specify a neuron.

forward(inputs: dict[str, tuple[Tensor, ...]], connection_kwargs: dict[str, dict[str, Any]] | None = None, neuron_kwargs: dict[str, dict[str, Any]] | None = None, capture_intermediate: bool = False, **kwargs: Any) dict[str, Tensor] | tuple[dict[str, Tensor], dict[str, Tensor]][source]

Computes a forward pass.

The keys for inputs and connection_kwargs are the names of registered Connection objects.

The keys for neuron_kwargs are the names of the registered Neuron objects.

Underlying Connection and Neuron objects are called using __call__, which in turn call Connection.forward() and Neuron.forward() respectively. The keyword argument dictionaries will be unpacked for each call automatically, and the inputs will be unpacked as positional arguments for each Connection call.

Only input modules that have keys in inputs will be run and added to the positional argument of wiring().

Parameters:
  • inputs (dict[str, tuple[torch.Tensor, ...]]) – inputs passed to the registered connections’ forward calls.

  • connection_kwargs (dict[str, dict[str, Any]] | None, optional) – keyword arguments passed to registered connections’ forward calls. Defaults to None.

  • neuron_kwargs (dict[str, dict[str, Any]] | None, optional) – keyword arguments passed to registered neurons’ forward calls. Defaults to None.

  • capture_intermediate (bool, optional) – if output from the connections should also be returned. Defaults to False.

  • **kwargs (Any) – keyword arguments passed to wiring().

Returns:

tensors from neurons and the associated neuron names, if capture_intermediate, this is the first element of a tuple, the second being a tuple of tensors from connections and the associated connection names.

Return type:

dict[str, torch.Tensor] | tuple[dict[str, torch.Tensor], dict[str, torch.Tensor]]

get_cell(connection: str, neuron: str) Cell[source]

Gets a created cell if it exists.

Parameters:
  • connection (str) – name of the connection for the cell to get.

  • neuron (str) – name of the neuron for the cell to get.

Raises:

AttributeError – no cell has been created with the specified connection and neuron.

Returns:

cell specified by the connection and neuron.

Return type:

Cell

get_connection(name: str) Connection[source]

Gets an existing connection.

Parameters:

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

Raises:

AttributeErrorname does not specify a connection.

Returns:

connection with specified name.

Return type:

Connection

get_neuron(name: str) Neuron[source]

Gets an existing neuron.

Parameters:

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

Raises:

AttributeErrorname does not specify a neuron.

Returns:

neuron with specified name

Return type:

Neuron

property named_cells: Iterator[tuple[tuple[str, str], Cell]]

Iterable of registered cells and tuples of the connection and neuron names.

Yields:

tuple[tuple[str, str], torch.Tensor] – tuple of a registered cell and a tuple of the connection name and neuron name corresponding to it.

property named_connections: Iterator[tuple[str, Connection]]

Iterable of registered connections and their names.

Yields:

tuple[str, Connection] – tuple of a registered connection and its name.

property named_neurons: Iterator[tuple[str, Neuron]]

Iterable of registered neurons and their names.

Yields:

tuple[str, Neuron] – tuple of a registered neuron and its name.

property named_synapses: Iterator[tuple[str, Synapse]]

Iterable of registered connection’s synapses and their names.

Yields:

tuple[str, Synapse] – tuple of a registered synapse and its name.

property neurons: Proxy

Registered neurons.

For a given name of a Neuron set via layer.add_neuron(name, neuron), it can be accessed as layer.neurons.name.

It can be modified in-place (including setting other attributes, adding monitors, etc), but it can neither be deleted nor reassigned.

This is primarily used when targeting Neuron objects with a monitor.

Returns:

safe access to registered neurons.

Return type:

Proxy

update(clear: bool = True, **kwargs) None[source]

Applies all cumulative updates.

This calls every updated which applies cumulative updates and any updater hooks are automatically called (e.g. parameter clamping).

Parameters:

clear (bool, optional) – if accumulators should be cleared after updating. Defaults to True.

abstract wiring(inputs: dict[str, Tensor], **kwargs) dict[str, Tensor][source]

Connection logic between connection outputs and neuron inputs.

The inputs are given as a dictionary where each key is a registered input name and the value is the tensor output from that connection. This is expected to return a dictionary where each key is the name of a registered output and the value is the tensor to be passed to its __call__ method.

Parameters:

inputs (dict[str, torch.Tensor]) – dictionary of input names to tensors.

Raises:

NotImplementedErrorwiring must be implemented by the subclass.

Returns:

dictionary of output names to tensors.

Return type:

dict[str, torch.Tensor]