Containers#

SpikingJelly 的 容器 封装了常见的网络结构,并支持单步(s)和多步(m)步进模式。 欲获取更多详细信息,请参阅 包装器


SpikingJelly's containers wrap common network architectures and support both single-step (s) and multi-step (m) step modes. For more information, refer to Container .

class spikingjelly.activation_based.layer.container.MultiStepContainer(*args)[源代码]#

基类:Sequential, MultiStepModule

API Language: 中文 | English


  • 中文

  • 中文

spikingjelly.activation_based.functional.multi_step_forward() 的容器。构造方式与 torch.nn.Sequential 一致。


  • English

  • English

Container of spikingjelly.activation_based.functional.multi_step_forward(). Its constructor signature is the same as torch.nn.Sequential.

返回:

None

返回类型:

None

forward(x_seq: Tensor)[源代码]#
参数:

x_seq (torch.Tensor) -- with shape [T, batch_size, ...]

返回:

y_seq with shape [T, batch_size, ...]

返回类型:

torch.Tensor

class spikingjelly.activation_based.layer.container.SeqToANNContainer(*args)[源代码]#

基类:Sequential, MultiStepModule

API Language: 中文 | English


  • 中文

  • 中文

spikingjelly.activation_based.functional.seq_to_ann_forward() 的容器。构造方式与 torch.nn.Sequential 一致。


  • English

  • English

Container of spikingjelly.activation_based.functional.seq_to_ann_forward(). Its constructor signature is the same as torch.nn.Sequential.

返回:

None

返回类型:

None

forward(x_seq: Tensor)[源代码]#
参数:

x_seq (torch.Tensor) -- with shape [T, batch_size, ...]

返回:

y_seq with shape [T, batch_size, ...]

返回类型:

torch.Tensor

class spikingjelly.activation_based.layer.container.TLastMultiStepContainer(*args)[源代码]#

基类:Sequential, MultiStepModule

See spikingjelly.activation_based.functional.forward.t_last_multi_step_forward() .

返回:

None

返回类型:

None

forward(x_seq: Tensor)[源代码]#
参数:

x_seq (Tensor) -- shape [batch_size, ..., T]

返回:

y_seq with shape [batch_size, ..., T]

返回类型:

Tensor

class spikingjelly.activation_based.layer.container.TLastSeqToANNContainer(*args)[源代码]#

基类:Sequential, MultiStepModule

See spikingjelly.activation_based.functional.forward.t_last_seq_to_ann_forward() .

返回:

None

返回类型:

None

forward(x_seq: Tensor)[源代码]#
参数:

x_seq (Tensor) -- with shape [batch_size, ..., T]

返回:

y_seq with shape [batch_size, ..., T]

返回类型:

Tensor

class spikingjelly.activation_based.layer.container.StepModeContainer(stateful: bool, step_mode: str = 's', *args)[源代码]#

基类:Sequential, StepModule

Call single-step forward, multi-step forward or seq-to-ANN forward according to stateful and step_mode.

参数:
  • stateful (bool) -- 是否是有状态的容器

  • step_mode (str) -- 步进模式,"s""m"

  • args (tuple) -- 与 torch.nn.Sequential 相同的构造参数

  • stateful -- Whether the container is stateful

  • step_mode -- Step mode, "s" or "m"

  • args -- Same constructor arguments as torch.nn.Sequential

返回:

None

返回类型:

None

forward(x: Tensor)[源代码]#
class spikingjelly.activation_based.layer.container.ElementWiseRecurrentContainer(sub_module: Module, element_wise_function: Callable, step_mode='s')[源代码]#

基类:MemoryModule

API Language: 中文 | English


  • 中文

使用逐元素运算的自连接包装器。记 sub_module 的输入输出为 \(i[t]\)\(y[t]\) (注意 \(y[t]\) 也是整个模块的输出), 整个模块的输入为 \(x[t]\),则

\[i[t] = f(x[t], y[t-1])\]

其中 \(f\) 是用户自定义的逐元素函数。我们默认 \(y[-1] = 0\)

备注

sub_module 输入和输出的尺寸需要相同。

参数:
  • sub_module (torch.nn.Module) -- 被包含的模块

  • element_wise_function (Callable) -- 用户自定义的逐元素函数,应该形如 z=f(x, y)

  • step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)


  • English

A container that use a element-wise recurrent connection. Denote the inputs and outputs of sub_module as \(i[t]\) and \(y[t]\) (Note that \(y[t]\) is also the outputs of this module), and the inputs of this module as \(x[t]\), then

\[i[t] = f(x[t], y[t-1])\]

where \(f\) is the user-defined element-wise function. We set \(y[-1] = 0\).

Note

The shape of inputs and outputs of sub_module must be the same.

参数:
  • sub_module (torch.nn.Module) -- the contained module

  • element_wise_function (Callable) -- the user-defined element-wise function, which should have the format z=f(x, y)

  • step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)


  • 代码示例 | Example

T = 8
net = ElementWiseRecurrentContainer(
    neuron.IFNode(v_reset=None), element_wise_function=lambda x, y: x + y
)
print(net)
x = torch.zeros([T])
x[0] = 1.5
for t in range(T):
    print(t, f"x[t]={x[t]}, s[t]={net(x[t])}")

functional.reset_net(net)
返回:

None

返回类型:

None

single_step_forward(x: Tensor)[源代码]#
class spikingjelly.activation_based.layer.container.LinearRecurrentContainer(sub_module: Module, in_features: int, out_features: int, bias: bool = True, step_mode='s')[源代码]#

基类:MemoryModule

API Language: 中文 | English


  • 中文

使用线性层的自连接包装器。记 sub_module 的输入和输出为 \(i[t]\)\(y[t]\) (注意 \(y[t]\) 也是整个模块的输出), 整个模块的输入记作 \(x[t]\) ,则

\[\begin{split}i[t] = \begin{pmatrix} x[t] \\ y[t-1]\end{pmatrix} W^{T} + b\end{split}\]

其中 \(W, b\) 是线性层的权重和偏置项。默认 \(y[-1] = 0\)

\(x[t]\) 应该 shape = [N, *, in_features]\(y[t]\) 则应该 shape = [N, *, out_features]

备注

自连接是由 torch.nn.Linear(in_features + out_features, in_features, bias) 实现的。

参数:
  • sub_module (torch.nn.Module) -- 被包含的模块

  • in_features (int) -- 输入的特征数量

  • out_features (int) -- 输出的特征数量

  • bias (bool) -- 若为 False,则线性自连接不会带有可学习的偏执项

  • step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)


  • English

A container that use a linear recurrent connection. Denote the inputs and outputs of sub_module as \(i[t]\) and \(y[t]\) (Note that \(y[t]\) is also the outputs of this module), and the inputs of this module as \(x[t]\), then

\[\begin{split}i[t] = \begin{pmatrix} x[t] \\ y[t-1]\end{pmatrix} W^{T} + b\end{split}\]

where \(W, b\) are the weight and bias of the linear connection. We set \(y[-1] = 0\).

\(x[t]\) should have the shape [N, *, in_features], and \(y[t]\) has the shape [N, *, out_features].

Note

The recurrent connection is implement by torch.nn.Linear(in_features + out_features, in_features, bias).

参数:
  • sub_module (torch.nn.Module) -- the contained module

  • in_features (int) -- size of each input sample

  • out_features (int) -- size of each output sample

  • bias (bool) -- If set to False, the linear recurrent layer will not learn an additive bias

  • step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)


  • 代码示例 | Example

in_features = 4
out_features = 2
T = 8
N = 2
net = LinearRecurrentContainer(
    nn.Sequential(
        nn.Linear(in_features, out_features),
        neuron.LIFNode(),
    ),
    in_features,
    out_features,
)
print(net)
x = torch.rand([T, N, in_features])
for t in range(T):
    print(t, net(x[t]))

functional.reset_net(net)
返回:

None

返回类型:

None

single_step_forward(x: Tensor)[源代码]#