Layer¶
- class Layer[source]¶
-
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:
- Raises:
AttributeError –
connectiondoes not specify a connection.AttributeError –
neurondoes not specify a neuron.
- Returns:
cell specified by the connection and neuron.
- Return type:
- 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:
RuntimeError –
namealready specifies a connection- Returns:
added connection.
- Return type:
- add_neuron(name: str, neuron: Neuron) Neuron[source]¶
Adds a new neuron.
- Parameters:
- Raises:
RuntimeError –
namealready specifies a neuron- Returns:
added neuron.
- Return type:
- property cells: Proxy¶
Registered cells.
For a given
connection_nameandneuron_name, theCellconstructed vialayer.add_cell(connection_name, neuron_name)can be accessed aslayer.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
Cellobjects 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
clearmethods, ifsubmodulesisTrue.
- property connections: Proxy¶
Registered connections.
For a given
nameof aConnectionset vialayer.add_connection(name), it can be accessed aslayer.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
Connectionobjects 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:
- Raises:
AttributeError –
connectiondoes not specify a connection.AttributeError –
neurondoes not specify a neuron.
- del_connection(name: str) None[source]¶
Deletes an existing connection.
- Parameters:
name (str) – name of the connection to delete.
- Raises:
AttributeError –
namedoes not specify a connection.
- del_neuron(name: str) None[source]¶
Deletes an existing neuron.
- Parameters:
name (str) – name of the neuron to delete.
- Raises:
ValueError –
namedoes 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
inputsandconnection_kwargsare the names of registeredConnectionobjects.The keys for
neuron_kwargsare the names of the registeredNeuronobjects.Underlying
ConnectionandNeuronobjects are called using__call__, which in turn callConnection.forward()andNeuron.forward()respectively. The keyword argument dictionaries will be unpacked for each call automatically, and the inputs will be unpacked as positional arguments for eachConnectioncall.Only input modules that have keys in
inputswill be run and added to the positional argument ofwiring().- 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:
- Raises:
AttributeError – no cell has been created with the specified connection and neuron.
- Returns:
cell specified by the connection and neuron.
- Return type:
- get_connection(name: str) Connection[source]¶
Gets an existing connection.
- Parameters:
name (str) – name of the connection to get.
- Raises:
AttributeError –
namedoes not specify a connection.- Returns:
connection with specified name.
- Return type:
- get_neuron(name: str) Neuron[source]¶
Gets an existing neuron.
- Parameters:
name (str) – name of the neuron to get.
- Raises:
AttributeError –
namedoes not specify a neuron.- Returns:
neuron with specified name
- Return type:
- 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
nameof aNeuronset vialayer.add_neuron(name, neuron), it can be accessed aslayer.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
Neuronobjects 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:
NotImplementedError –
wiringmust be implemented by the subclass.- Returns:
dictionary of output names to tensors.
- Return type: