Serial

class Serial(connection: Connection, neuron: Neuron, transform: OneToOne[Tensor] | None = None, connection_name: str = 'serial', neuron_name: str = 'serial')[source]

Bases: Layer

Layer with a single connection and single neuron group.

This wraps Layer to provide simplified accessors and a simplified forward() method for layers with one connection and one neuron group.

Parameters:
  • connection (Connection) – module which receives input to the layer.

  • neuron (Neuron) – module which generates output from the layer.

  • transform (OneToOne[torch.Tensor] | None, optional) – function to apply to connection output before passing into neurons. Defaults to None.

  • connection_name (str, optional) – name for the connection in the layer. Defaults to "serial".

  • neuron_name (str, optional) – name for the neuron in the layer. Defaults to "serial".

Note

When transform is not specified, the identity function is used. Keyword arguments passed into __call__, other than those captured in forward() will be passed in.

Note

The Layer object underlying a Serial object has connection and neuron registered with names "serial". Convenience properties can be used to avoid accessing manually.

add_cell(*args, **kwargs) None[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(*args, **kwargs) None[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(*args, **kwargs) None[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 cell: Cell

Registered cell.

Returns:

registered cell.

Return type:

Cell

property connection: Connection

Registered connection.

Returns:

registered connection.

Return type:

Connection

del_cell(*args, **kwargs) 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(*args, **kwargs) None[source]

Deletes an existing connection.

Parameters:

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

Raises:

AttributeErrorname does not specify a connection.

del_neuron(*args, **kwargs) None[source]

Deletes an existing neuron.

Parameters:

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

Raises:

ValueErrorname does not specify a neuron.

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

Computes a forward pass.

Parameters:
  • *inputs (torch.Tensor) – values passed to the connection.

  • connection_kwargs (dict[str, dict[str, Any]] | None, optional) – keyword arguments for the connection’s forward call. Defaults to None.

  • neuron_kwargs (dict[str, dict[str, Any]] | None, optional) – keyword arguments for the neuron’s forward call. 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:

output from the neurons, if capture_intermediate, this is the first element of a tuple, the second being the output from the connection.

Return type:

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

property neuron: Neuron

Registered neuron.

Returns:

registered neuron.

Return type:

Neuron

property synapse: Synapse

Registered synapse.

Returns:

registered connection’s synapse.

Return type:

Synapse

property updater: Updater

Registered updater.

Returns:

registered connection’s updater.

Return type:

Updater

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

Connection logic between connection outputs and neuron inputs.

This implements the forward logic of the serial topology. The transform is applied to the result of the connection before being passed to the neuron. If not specified, it is assumed to be identity.

Parameters:

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

Returns:

dictionary of output names to tensors.

Return type:

dict[str, torch.Tensor]