Conv2D

class Conv2D(height: int, width: int, channels: int, filters: int, step_time: float, kernel: int | tuple[int, int], *, stride: int | tuple[int, int] = 1, padding: int | tuple[int, int] = 0, dilation: int | tuple[int, int] = 1, synapse: SynapseConstructor, bias: bool = False, delay: float | None = None, batch_size: int = 1, weight_init: OneToOne[Tensor] | None = None, bias_init: OneToOne[Tensor] | None = None, delay_init: OneToOne[Tensor] | None = None)[source]

Bases: WeightBiasDelayMixin, Connection

Convolutional connection along two spatial dimensions with separate input planes.

Parameters:
  • height (int) – height of the expected inputs.

  • width (int) – width of the expected inputs.

  • channels (int) – number of channels in the input tensor.

  • filters (int) – number of convolutional filters (channels of the output tensor).

  • step_time (float) – length of a simulation time step, in \(\text{ms}\).

  • kernel (int | tuple[int, int]) – size of the convolution kernel.

  • stride (int | tuple[int, int], optional) – stride of the convolution. Defaults to 1.

  • padding (int | tuple[int, int], optional) – amount of zero padding added to height and width. Defaults to 0.

  • dilation (int | tuple[int, int], optional) – dilation of the convolution. Defaults to 1.

  • synapse (SynapseConstructor) – partial constructor for inner Synapse.

  • bias (bool, optional) – if the connection should support learnable additive bias. Defaults to False.

  • delay (float | None, optional) – maximum supported delay length, in \(\text{ms}\), excludes delays when None. Defaults to None.

  • batch_size (int, optional) – size of input batches for simulation. Defaults to 1.

  • weight_init (OneToOne[torch.Tensor] | None, optional) – initializer for weights. Defaults to None.

  • bias_init (OneToOne[torch.Tensor] | None, optional) – initializer for biases. Defaults to None.

  • delay_init (OneToOne[torch.Tensor] | None, optional) – initializer for delays. Defaults to None.

Shape

Conv2D.weight, Conv2D.delay:

\(F \times C \times H \times W\)

Conv2D.bias:

\(F\)

Where:
  • \(F\) is the number of filters (output channels).

  • \(C\) is the number of input channels.

  • \(kH\) is the kernel height.

  • \(kW\) is the kernel width.

Note

When delay is None, no delay_ parameter is created and altering the maximum delay of synapse will have no effect. Setting to 0 will create and register a delay_ parameter but not use delays unless it is later changed.

Note

If weight_init or bias_init are None, weight and bias are, respectively, initialized as uniform random values over the interval \([0, 1)\) using torch.rand().

If delay_init is None, delay is initialized as zeros using torch.rand().

Tip

The added padding is applied after the synapse. Inputs must still be of uniform size. Only zero padding is supported, if another type of padding is required, it should be performed before being inputted to the connection.

forward(*inputs: Tensor, **kwargs) Tensor[source]

Generates connection output from inputs, after passing through the synapse.

Outputs are determined as the learned two-dimensional convolution applied to synaptic currents, after new input is applied to the synapse.

Parameters:

*inputs (torch.Tensor) – inputs to the connection.

Returns:

outputs from the connection.

Return type:

torch.Tensor

Shape

*inputs:

\(B \times C \times H \times W\)

return:

\(B \times F \times H_\text{out} \times W_\text{out}\)

Where:
  • \(B\) is the batch size.

  • \(C\) is the number of input channels.

  • \(H\) is the input height.

  • \(W\) is the input width.

  • \(F\) is the number of filters (output channels).

  • \(H_\text{out}\) is the output height.

  • \(W_\text{out}\) is the output width.

Note

*inputs are reshaped using like_synaptic() then passed to py:meth:Synapse.forward of synapse. Keyword arguments are also passed through.

See also

The formulae for output height and width are detailed in the documentation for outshape.

property inshape: tuple[int, int, int]

Shape of inputs to the connection, excluding the batch dimension.

Returns:

shape of inputs to the connection.

Return type:

tuple[int]

Note

Resulting tuple will be:

\((C, H, W)\)

Where:
  • \(C\) is the number of input channels.

  • \(H\) is the input height.

  • \(W\) is the input width.

like_bias(data: Tensor) Tensor[source]

Reshapes data like reduced postsynaptic receptive spikes to connection bias.

Parameters:

data (torch.Tensor) – data shaped like reduced postsynaptic receptive spikes.

Returns:

reshaped data.

Return type:

torch.Tensor

Shape

data:

\(F \times 1 \times 1 \times 1\)

return:

\(F\)

Where:
  • \(F\) is the number of filters (output channels).

like_input(data: Tensor) Tensor[source]

Reshapes data like synapse input to connection input.

Parameters:

data (torch.Tensor) – data shaped like synapse input.

Returns:

reshaped data.

Return type:

torch.Tensor

Shape

data:

\(B \times (C \cdot kH \cdot kW) \times (H_\text{out} \cdot W_\text{out})\)

return:

\(B \times C \times H \times W\)

Where:
  • \(B\) is the batch size.

  • \(C\) is the number of input channels.

  • \(kH\) is the kernel height.

  • \(kW\) is the kernel width.

  • \(H_\text{out}\) is the output height.

  • \(W_\text{out}\) is the output width.

  • \(H\) is the input height.

  • \(W\) is the input width.

Note

PyTorch’s fold() and unfold() are only implemented for floating point values. Intermediate casting to the same datatype as connection weights will be performed if required.

like_synaptic(data: Tensor) Tensor[source]

Reshapes data like connection input to synapse input.

Parameters:

data (torch.Tensor) – data shaped like connection input.

Returns:

reshaped data.

Return type:

torch.Tensor

Shape

data:

\(B \times C \times H \times W\)

return:

\(B \times (C \cdot kH \cdot kW) \times (H_\text{out} \cdot W_\text{out})\)

Where:
  • \(B\) is the batch size.

  • \(C\) is the number of input channels.

  • \(H\) is the input height.

  • \(W\) is the input width.

  • \(kH\) is the kernel height.

  • \(kW\) is the kernel width.

  • \(H_\text{out}\) is the output height.

  • \(W_\text{out}\) is the output width.

Note

PyTorch’s fold() and unfold() are only implemented for floating point values. Intermediate casting to the same datatype as connection weights will be performed if required.

property outshape: tuple[int, int, int]

Shape of outputs from the connection, excluding the batch dimension.

Returns:

shape of outputs from the connection.

Return type:

tuple[int]

Note

Resulting tuple will be:

\((F, H_\text{out}, W_\text{out})\)

Where:

\[\begin{split}\begin{align*} H_\text{out} &= \left\lfloor \frac{H + 2 \times p_H - d_H \times (k_H - 1) - 1}{s_H} + 1 \right\rfloor \\ W_\text{out} &= \left\lfloor \frac{W + 2 \times p_W - d_W \times (k_W - 1) - 1}{s_W} + 1 \right\rfloor \end{align*}\end{split}\]
And:
  • \(F\) is the number of filters (output channels).

  • \((H, W)\) are the input height and width.

  • \((pH, pW)\) are the per-side padding height and width.

  • \((dH, dW)\) are the dilation height and width.

  • \((kH, kW)\) are the kernel height and width.

  • \((sH, sW)\) are the stride height and width.

postsyn_receptive(data: Tensor) Tensor[source]

Reshapes data like connection output for pre-post learning methods.

Parameters:

data (torch.Tensor) – data shaped like output of forward().

Returns:

reshaped data.

Return type:

torch.Tensor

Shape

data:

\(B \times F \times H_\text{out} \times W_\text{out}\)

return:

\(B \times F \times 1 \times 1 \times 1 \times (H_\text{out} \cdot W_\text{out})\)

Where:
  • \(B\) is the batch size.

  • \(F\) is the number of filters (output channels).

  • \(H_\text{out}\) is the output height.

  • \(W_\text{out}\) is the output width.

presyn_receptive(data: Tensor) Tensor[source]

Reshapes data like the synapse state for pre-post learning methods.

Parameters:

data (torch.Tensor) – data shaped like output of like_synaptic().

Returns:

reshaped data.

Return type:

torch.Tensor

Shape

data:

\(B \times (C \cdot kH \cdot kW) \times (H_\text{out} \cdot W_\text{out}) \times [F]\)

return:

\(B \times F \times C \times kH \times kW \times (H_\text{out} \cdot W_\text{out})\)

or

\(B \times 1 \times C \times kH \times kW \times (H_\text{out} \cdot W_\text{out})\)

Where:
  • \(B\) is the batch size.

  • \(C\) is the number of input channels.

  • \(kH\) is the kernel height.

  • \(kW\) is the kernel width.

  • \(H_\text{out}\) is the output height.

  • \(W_\text{out}\) is the output width.

  • \(F\) is the number of filters (output channels).

property selector: Tensor | None

Learned delays as a selector for synaptic currents and delays.

Returns:

delay selector if the connection has learnable delays.

Return type:

torch.Tensor | None

Shape

\(B \times (C \cdot kH \cdot kW) \times (H_\text{out} \cdot W_\text{out}) \times F\)

Where:
  • \(B\) is the batch size.

  • \(C\) is the number of input channels.

  • \(kH\) is the kernel height.

  • \(kW\) is the kernel width.

  • \(H_\text{out}\) is the output height.

  • \(W_\text{out}\) is the output width.

  • \(F\) is the number of filters (output channels).

Caution

This operation relies upon torch.Tensor.expand(), and consequentially multiple elements may reference the same underlying memory.