Biclique

class Biclique(connections: Iterable[tuple[str, Connection] | tuple[str, Connection, OneToOne[Tensor]]], neurons: Iterable[tuple[str, Neuron] | tuple[str, Neuron, OneToOne[Tensor]]], combine: Callable[[dict[str, Tensor]], Tensor] | Literal['sum', 'mean', 'prod', 'min', 'max'] = 'sum')[source]

Bases: Layer

Layer structured as a complete bipartite graph.

Each input is processed by its corresponding connection, with an optional transformation applied, before being combined with the results of all other connections. These are then, for each group of neurons, optionally transformed and then passed in.

Each element of connections and c must be a tuple with at least two elements and at most three. The first of these is a string, which must be a Python identifier and unique to across the connections and neurons. The second is the module itself (Connection or Neuron respectively).

The optional third is a function which is a callable that takes and returns a Tensor. If present, this will be applied to the output tensor of the corresponding Connection or input tensor of the corresponding Neuron.

Either a function to combine the tensors from the modules in connections to be passed into inputs or a string literal may be provided. These may be “sum”, “mean”, “prod”, “min”, or “max”. All use einops to reduce. When providing a function, it must take a tuple of tensors (equal to the number of inputs) and produce a single tensor output.

Parameters:

Note

When combine is not a string, keyword arguments passed into __call__, other than those captured in forward() will be passed in.

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

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.

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 biclique topology where the tensors from the inputs are transformed, combined, and transformed again before being passed to the outputs. Transforms which were unspecified are assumed to be identity.

Parameters:

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

Returns:

dictionary of output names to tensors.

Return type:

dict[str, torch.Tensor]