spikingjelly.activation_based.rnn module#

spikingjelly.activation_based.rnn.directional_rnn_cell_forward(cell: Module, x: Tensor, states: Tensor)[源代码]#

API Language: 中文 | English


  • 中文

沿时间维对单个 RNN cell 执行循环,返回整段输出序列和最终状态。

x 的时间维位于第 0 维。该函数会逐个时间步调用 cell(x[t], ss), 其中 ss 是当前状态。若 states 是二维张量,则 cell 被视为只有一个 状态张量;若 states 是三维张量,则其第 0 维表示状态数量,且默认将第 0 个 状态视为当前时间步的输出。

参数:
  • cell (nn.Module) -- RNN cell

  • x (torch.Tensor) -- shape = [T, batch_size, input_size] 的输入

  • states (torch.Tensor) -- RNN cell的起始状态。 若RNN cell只有单个隐藏状态,则 shape = [batch_size, hidden_size] ; 否则 shape = [states_num, batch_size, hidden_size]

返回:

(output, final_states) ,其中 output 的形状为 [T, batch_size, hidden_size]final_states 的形状与 states 相同

返回类型:

tuple[torch.Tensor, torch.Tensor]

抛出:

ValueError -- 若 states.dim() 既不是 2 也不是 3,则行为未定义,调用方应保证输入合法


  • English

Run a single RNN cell along the time dimension and return the full output sequence together with the final state.

The time axis of x is the first dimension. The function repeatedly evaluates cell(x[t], ss) where ss is the current state. If states is a 2-D tensor, the cell is treated as having a single state tensor. If states is a 3-D tensor, its first dimension is interpreted as the number of states, and the first state is treated as the output at each time step.

参数:
  • cell (nn.Module) -- the RNN cell

  • x (torch.Tensor) -- input with shape = [T, batch_size, input_size]

  • states (torch.Tensor) -- initial state of the RNN cell. If the RNN cell has a single hidden state, shape = [batch_size, hidden_size]; otherwise shape = [states_num, batch_size, hidden_size]

返回:

(output, final_states), where output has shape [T, batch_size, hidden_size] and final_states has the same shape as states

返回类型:

tuple[torch.Tensor, torch.Tensor]

抛出:

ValueError -- Callers are expected to provide states with rank 2 or 3

spikingjelly.activation_based.rnn.bidirectional_rnn_cell_forward(cell: Module, cell_reverse: Module, x: Tensor, states: Tensor, states_reverse: Tensor)[源代码]#

API Language: 中文 | English


  • 中文

沿时间维同时执行正向与反向 RNN cell,返回拼接后的输出序列以及两个方向的最终状态。

对于每个时间步 t,正向 cell 接收 x[t],反向 cell 接收 x[T - t - 1]。若状态张量为三维,则默认将第 0 个状态视为该时间步输出。

参数:
  • cell (nn.Module) -- 正向RNN cell,输入是正向序列

  • cell_reverse (nn.Module) -- 反向的RNN cell,输入是反向序列

  • x (torch.Tensor) -- shape = [T, batch_size, input_size] 的输入

  • states (torch.Tensor) -- 正向RNN cell的起始状态 若RNN cell只有单个隐藏状态,则 shape = [batch_size, hidden_size] ; 否则 shape = [states_num, batch_size, hidden_size]

  • states_reverse (torch.Tensor) -- 反向RNN cell的起始状态 若RNN cell只有单个隐藏状态,则 shape = [batch_size, hidden_size] ; 否则 shape = [states_num, batch_size, hidden_size]

返回:

(output, final_states, final_states_reverse)。其中 output 的形状为 [T, batch_size, 2 * hidden_size],由正向与反向输出拼接而成; 其余两个返回值的形状分别与 statesstates_reverse 相同

返回类型:

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


  • English

Run a forward RNN cell and a reverse RNN cell along the time dimension, returning the concatenated output sequence and the final states of both directions.

At each time step t, the forward cell consumes x[t] and the reverse cell consumes x[T - t - 1]. If the state tensor is 3-D, its first state is treated as the output at that step.

参数:
  • cell (nn.Module) -- forward RNN cell that takes the forward sequence as input

  • cell_reverse (nn.Module) -- reverse RNN cell that takes the reverse sequence as input

  • x (torch.Tensor) -- input with shape = [T, batch_size, input_size]

  • states (torch.Tensor) -- initial state of the forward RNN cell. If the RNN cell has a single hidden state, shape = [batch_size, hidden_size]; otherwise shape = [states_num, batch_size, hidden_size]

  • states_reverse (torch.Tensor) -- initial state of the reverse RNN cell. If the RNN cell has a single hidden state, shape = [batch_size, hidden_size]; otherwise shape = [states_num, batch_size, hidden_size]

返回:

(output, final_states, final_states_reverse). output has shape [T, batch_size, 2 * hidden_size] and concatenates the forward and reverse outputs. The other two return values have the same shapes as states and states_reverse, respectively

返回类型:

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

class spikingjelly.activation_based.rnn.SpikingRNNCellBase(input_size: int, hidden_size: int, bias=True)[源代码]#

基类:Module

API Language: 中文 | English


  • 中文

Spiking RNN cell 的基类。

参数:
  • input_size (int) -- 输入 x 的特征数

  • hidden_size (int) -- 隐藏状态 h 的特征数

  • bias (bool) -- 若为 False, 则内部的隐藏层不会带有偏置项 b_ihb_hh。 默认为 True

备注

所有权重和偏置项都会按照 \(\\mathcal{U}(-\\sqrt{k}, \\sqrt{k})\) 进行初始化。 其中 \(k = \\frac{1}{\\text{hidden_size}}\).


  • English

The base class of Spiking RNN Cell.

参数:
  • input_size (int) -- The number of expected features in the input x

  • hidden_size (int) -- The number of features in the hidden state h

  • bias (bool) -- If False, then the layer does not use bias weights b_ih and b_hh. Default: True

Note

All the weights and biases are initialized from \(\\mathcal{U}(-\\sqrt{k}, \\sqrt{k})\) where \(k = \\frac{1}{\\text{hidden_size}}\).

reset_parameters()[源代码]#

API Language: 中文 | English


  • 中文

初始化所有可学习参数。

返回:

None

返回类型:

None


  • English

Initialize all learnable parameters.

返回:

None

返回类型:

None

weight_ih()[源代码]#

API Language: 中文 | English


  • 中文

返回:

输入到隐藏状态的连接权重

返回类型:

torch.Tensor


  • English

返回:

the learnable input-hidden weights

返回类型:

torch.Tensor

weight_hh()[源代码]#

API Language: 中文 | English


  • 中文

返回:

隐藏状态到隐藏状态的连接权重

返回类型:

torch.Tensor


  • English

返回:

the learnable hidden-hidden weights

返回类型:

torch.Tensor

bias_ih()[源代码]#

API Language: 中文 | English


  • 中文

返回:

输入到隐藏状态的连接偏置项

返回类型:

torch.Tensor


  • English

返回:

the learnable input-hidden bias

返回类型:

torch.Tensor

bias_hh()[源代码]#

API Language: 中文 | English


  • 中文

返回:

隐藏状态到隐藏状态的连接偏置项

返回类型:

torch.Tensor


  • English

返回:

the learnable hidden-hidden bias

返回类型:

torch.Tensor

class spikingjelly.activation_based.rnn.SpikingRNNBase(input_size, hidden_size, num_layers, bias=True, dropout_p=0, invariant_dropout_mask=False, bidirectional=False, *args, **kwargs)[源代码]#

基类:Module

API Language: 中文 | English


  • 中文

多层(可选双向)脉冲 RNN 的基类。

参数:
  • input_size (int) -- 输入 x 的特征数

  • hidden_size (int) -- 隐藏状态 h 的特征数

  • num_layers (int) -- 内部RNN的层数,例如 num_layers = 2 将会创建堆栈式的两层RNN,第1层接收第0层的输出作为输入, 并计算最终输出

  • bias (bool) -- 若为 False, 则内部的隐藏层不会带有偏置项 b_ihb_hh。 默认为 True

  • dropout_p (float) -- 若非 0,则除了最后一层,每个RNN层后会增加一个丢弃概率为 dropout_pDropout 层。 默认为 0

  • invariant_dropout_mask (bool) -- 若为 False,则使用普通的 Dropout;若为 True,则使用SNN中特有的,mask 不 随着时间变化的 Dropout`,参见 Dropout。默认为 False

  • bidirectional (bool) -- 若为 True,则使用双向RNN。默认为 False

  • args -- 子类使用的额外参数

  • kwargs -- 子类使用的额外参数


  • English

The base-class of a multi-layer spiking RNN.

参数:
  • input_size (int) -- The number of expected features in the input x

  • hidden_size (int) -- The number of features in the hidden state h

  • num_layers (int) -- Number of recurrent layers. E.g., setting num_layers=2 would mean stacking two LSTMs together to form a stacked RNN, with the second RNN taking in outputs of the first RNN and computing the final results

  • bias (bool) -- If False, then the layer does not use bias weights b_ih and b_hh. Default: True

  • dropout_p (float) -- If non-zero, introduces a Dropout layer on the outputs of each RNN layer except the last layer, with dropout probability equal to dropout. Default: 0

  • invariant_dropout_mask (bool) -- If False,use the naive Dropout;If True,use the dropout in SNN that mask doesn't change in different time steps, see Dropout for more information. Default: False

  • bidirectional (bool) -- If True, becomes a bidirectional LSTM. Default: False

  • args -- additional arguments for sub-class

  • kwargs -- additional arguments for sub-class

create_cells(*args, **kwargs)[源代码]#

API Language: 中文 | English


  • 中文

参数:
  • args -- 子类使用的额外参数

  • kwargs -- 子类使用的额外参数

返回:

self.bidirectional == True 则会返回正反两个堆栈式RNN;否则返回单个堆栈式RNN

返回类型:

nn.Sequential | tuple[nn.Sequential, nn.Sequential]


  • English

参数:
  • args -- additional arguments for sub-class

  • kwargs -- additional arguments for sub-class

返回:

If self.bidirectional == True, return a RNN for forward direction and a RNN for reverse direction; else, return a single stacking RNN

返回类型:

nn.Sequential | tuple[nn.Sequential, nn.Sequential]

static base_cell()[源代码]#

API Language: 中文 | English


  • 中文

返回:

构成该RNN的基本RNN Cell。例如对于 SpikingLSTM, 返回的是 SpikingLSTMCell

返回类型:

nn.Module


  • English

返回:

The base cell of this RNN. E.g., in SpikingLSTM this function will return SpikingLSTMCell

返回类型:

nn.Module

static states_num()[源代码]#

API Language: 中文 | English


  • 中文

返回:

状态变量的数量。例如对于 SpikingLSTM,由于其输出是 hc, 因此返回 2;而对于 SpikingGRU,由于其输出是 h,因此返回 1

返回类型:

int


  • English

返回:

The states number. E.g., for SpikingLSTM the output are h and c, this function will return 2; for SpikingGRU the output is h, this function will return 1

返回类型:

int

forward(x: Tensor, states=None)[源代码]#

API Language: 中文 | English


  • 中文

执行多层(可选双向)脉冲 RNN 的前向传播。

输入 x 的时间维位于第 0 维。若 statesNone,则会在 x.device 上自动创建零初始状态。对双向 RNN,状态张量的第 0 维按照 [layer_0_forward, ..., layer_{L-1}_forward, layer_0_reverse, ..., layer_{L-1}_reverse] 排列;对单向 RNN,则仅包含各层正向状态。

参数:
  • x (torch.Tensor) -- shape = [T, batch_size, input_size],输入序列

  • states (Union[torch.Tensor, tuple]) -- self.states_num()1 时是单个tensor, 否则是一个tuple,包含 self.states_num() 个tensors。 所有的tensor的尺寸均为 shape = [num_layers * num_directions, batch, hidden_size], 包含 self.states_num() 个初始状态 如果RNN是双向的, num_directions2, 否则为 1

返回:

(output, output_states)output 的形状为 [T, batch, num_directions * hidden_size]output_statesself.states_num() == 1 时为单个张量,否则为包含多个状态张量的 tuple

返回类型:

tuple[torch.Tensor, Union[torch.Tensor, tuple]]

抛出:
  • TypeError -- 若 states 既不是 Nonetorch.Tensor 也不是 tuple

  • ValueError -- 若 states 的层数/方向数维度与当前 RNN 配置不匹配


  • English

Run the forward pass of a stacked spiking RNN, optionally bidirectional.

The time axis of x is the first dimension. If states is None, zero initial states are created on x.device. For bidirectional RNNs, the first state dimension is ordered as [layer_0_forward, ..., layer_{L-1}_forward, layer_0_reverse, ..., layer_{L-1}_reverse].

参数:
  • x (torch.Tensor) -- shape = [T, batch_size, input_size], tensor containing the features of the input sequence

  • states (Union[torch.Tensor, tuple]) -- a single tensor when self.states_num() is 1, otherwise a tuple with self.states_num() tensors. shape = [num_layers * num_directions, batch, hidden_size] for all tensors, containing the self.states_num() initial states for each element in the batch. If the RNN is bidirectional, num_directions should be 2, else it should be 1

返回:

(output, output_states). output has shape [T, batch, num_directions * hidden_size]. output_states is a single tensor when self.states_num() == 1; otherwise it is a tuple of state tensors

返回类型:

tuple[torch.Tensor, Union[torch.Tensor, tuple]]

抛出:
  • TypeError -- If states is neither None, torch.Tensor nor tuple

  • ValueError -- If the layer/direction dimension of states does not match this RNN configuration

class spikingjelly.activation_based.rnn.SpikingLSTMCell(input_size: int, hidden_size: int, bias=True, surrogate_function1=Erf(alpha=2.0, spiking=True), surrogate_function2=None)[源代码]#

基类:SpikingRNNCellBase

API Language: 中文 | English


  • 中文

脉冲 长短时记忆 (LSTM) cell, 最先由 Long Short-Term Memory Spiking Networks and Their Applications 一文提出。

\[\begin{split}i &= \\Theta(W_{ii} x + b_{ii} + W_{hi} h + b_{hi}) \\\\ f &= \\Theta(W_{if} x + b_{if} + W_{hf} h + b_{hf}) \\\\ g &= \\Theta(W_{ig} x + b_{ig} + W_{hg} h + b_{hg}) \\\\ o &= \\Theta(W_{io} x + b_{io} + W_{ho} h + b_{ho}) \\\\ c' &= f * c + i * g \\\\ h' &= o * c'\end{split}\]

其中 \(\\Theta\) 是heaviside阶跃函数(脉冲函数), and \(*\) 是Hadamard点积,即逐元素相乘。

参数:

备注

所有权重和偏置项都会按照 \(\\mathcal{U}(-\\sqrt{k}, \\sqrt{k})\) 进行初始化。 其中 \(k = \\frac{1}{\\text{hidden_size}}\).

示例代码:

T = 6
batch_size = 2
input_size = 3
hidden_size = 4
rnn = rnn.SpikingLSTMCell(input_size, hidden_size)
input = torch.randn(T, batch_size, input_size) * 50
h = torch.randn(batch_size, hidden_size)
c = torch.randn(batch_size, hidden_size)

output = []
for t in range(T):
    h, c = rnn(input[t], (h, c))
    output.append(h)
print(output)

  • English

A spiking long short-term memory (LSTM) cell, which is firstly proposed in Long Short-Term Memory Spiking Networks and Their Applications.

\[\begin{split}i &= \\Theta(W_{ii} x + b_{ii} + W_{hi} h + b_{hi}) \\\\ f &= \\Theta(W_{if} x + b_{if} + W_{hf} h + b_{hf}) \\\\ g &= \\Theta(W_{ig} x + b_{ig} + W_{hg} h + b_{hg}) \\\\ o &= \\Theta(W_{io} x + b_{io} + W_{ho} h + b_{ho}) \\\\ c' &= f * c + i * g \\\\ h' &= o * c'\end{split}\]

where \(\\Theta\) is the heaviside function, and \(*\) is the Hadamard product.

参数:
  • input_size (int) -- The number of expected features in the input x

  • hidden_size (int) -- The number of features in the hidden state h

  • bias (bool) -- If False, then the layer does not use bias weights b_ih and b_hh. Default: True

  • surrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- surrogate function for replacing gradient of spiking functions during back-propagation, which is used for generating i, f, o

  • surrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- surrogate function for replacing gradient of spiking functions during back-propagation, which is used for generating g. If None, the surrogate function for generating g will be set as surrogate_function1. Default: None

Note

All the weights and biases are initialized from \(\\mathcal{U}(-\\sqrt{k}, \\sqrt{k})\) where \(k = \\frac{1}{\\text{hidden_size}}\).

Examples:

T = 6
batch_size = 2
input_size = 3
hidden_size = 4
rnn = rnn.SpikingLSTMCell(input_size, hidden_size)
input = torch.randn(T, batch_size, input_size) * 50
h = torch.randn(batch_size, hidden_size)
c = torch.randn(batch_size, hidden_size)

output = []
for t in range(T):
    h, c = rnn(input[t], (h, c))
    output.append(h)
print(output)
forward(x: Tensor, hc=None)[源代码]#

API Language: 中文 | English


  • 中文

执行单步 Spiking LSTM cell 前向传播。

hcNone,则会在 x.device 上构造零初始状态 h_0c_0。返回值中的 h_1c_1 分别表示当前时间步的隐藏状态与细胞状态。

参数:
  • x (torch.Tensor) -- shape = [batch_size, input_size] 的输入

  • hc (Optional[tuple[torch.Tensor, torch.Tensor]]) -- (h_0, c_0),其中两个张量的形状均为 [batch_size, hidden_size]。若为 None,则 h_0c_0 默认为零张量

返回:

(h_1, c_1),两个张量的形状均为 [batch_size, hidden_size]

返回类型:

tuple[torch.Tensor, torch.Tensor]


  • English

Run the forward pass of a single-step Spiking LSTM cell.

If hc is None, zero initial states h_0 and c_0 are created on x.device. The returned h_1 and c_1 are the hidden state and cell state of the current time step.

参数:
  • x (torch.Tensor) -- the input tensor with shape = [batch_size, input_size]

  • hc (Optional[tuple[torch.Tensor, torch.Tensor]]) -- (h_0, c_0), where both tensors have shape [batch_size, hidden_size]. If None, both states default to zeros

返回:

(h_1, c_1), both tensors have shape [batch_size, hidden_size]

返回类型:

tuple[torch.Tensor, torch.Tensor]

class spikingjelly.activation_based.rnn.SpikingLSTM(input_size, hidden_size, num_layers, bias=True, dropout_p=0, invariant_dropout_mask=False, bidirectional=False, surrogate_function1=Erf(alpha=2.0, spiking=True), surrogate_function2=None)[源代码]#

基类:SpikingRNNBase

API Language: 中文 | English


  • 中文

多层`脉冲` 长短时记忆LSTM, 最先由 Long Short-Term Memory Spiking Networks and Their Applications 一文提出。

每一层的计算按照

\[\begin{split}i_{t} &= \\Theta(W_{ii} x_{t} + b_{ii} + W_{hi} h_{t-1} + b_{hi}) \\\\ f_{t} &= \\Theta(W_{if} x_{t} + b_{if} + W_{hf} h_{t-1} + b_{hf}) \\\\ g_{t} &= \\Theta(W_{ig} x_{t} + b_{ig} + W_{hg} h_{t-1} + b_{hg}) \\\\ o_{t} &= \\Theta(W_{io} x_{t} + b_{io} + W_{ho} h_{t-1} + b_{ho}) \\\\ c_{t} &= f_{t} * c_{t-1} + i_{t} * g_{t} \\\\ h_{t} &= o_{t} * c_{t-1}'\end{split}\]

其中 \(h_{t}\)\(t\) 时刻的隐藏状态,\(c_{t}\)\(t\) 时刻的细胞状态,\(h_{t-1}\) 是该层 \(t-1\) 时刻的隐藏状态或起始状态,\(i_{t}\)\(f_{t}\)\(g_{t}\)\(o_{t}\) 分别是输入,遗忘,细胞,输出门, \(\\Theta\) 是heaviside阶跃函数(脉冲函数), and \(*\) 是Hadamard点积,即逐元素相乘。

参数:
  • input_size (int) -- 输入 x 的特征数

  • hidden_size (int) -- 隐藏状态 h 的特征数

  • num_layers (int) -- 内部RNN的层数,例如 num_layers = 2 将会创建堆栈式的两层RNN,第1层接收第0层的输出作为输入, 并计算最终输出

  • bias (bool) -- 若为 False, 则内部的隐藏层不会带有偏置项 b_ihb_hh。 默认为 True

  • dropout_p (float) -- 若非 0,则除了最后一层,每个RNN层后会增加一个丢弃概率为 dropout_pDropout 层。 默认为 0

  • invariant_dropout_mask (bool) -- 若为 False,则使用普通的 Dropout;若为 True,则使用SNN中特有的,mask 不 随着时间变化的 Dropout`,参见 Dropout。默认为 False

  • bidirectional (bool) -- 若为 True,则使用双向RNN。默认为 False

  • surrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数, 计算 i, f, o 反向传播时使用

  • surrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- 反向传播时用来计算脉冲函数梯度的替代函数, 计算 g 反向传播时使用。 若为 None, 则设置成 surrogate_function1。默认为 None


  • English

The spiking multi-layer long short-term memory (LSTM), which is firstly proposed in Long Short-Term Memory Spiking Networks and Their Applications.

For each element in the input sequence, each layer computes the following function:

\[\begin{split}i_{t} &= \\Theta(W_{ii} x_{t} + b_{ii} + W_{hi} h_{t-1} + b_{hi}) \\\\ f_{t} &= \\Theta(W_{if} x_{t} + b_{if} + W_{hf} h_{t-1} + b_{hf}) \\\\ g_{t} &= \\Theta(W_{ig} x_{t} + b_{ig} + W_{hg} h_{t-1} + b_{hg}) \\\\ o_{t} &= \\Theta(W_{io} x_{t} + b_{io} + W_{ho} h_{t-1} + b_{ho}) \\\\ c_{t} &= f_{t} * c_{t-1} + i_{t} * g_{t} \\\\ h_{t} &= o_{t} * c_{t-1}'\end{split}\]

where \(h_t\) is the hidden state at time t, \(c_t\) is the cell state at time t, \(x_t\) is the input at time t, \(h_{t-1}\) is the hidden state of the layer at time t-1 or the initial hidden state at time 0, and \(i_t\), \(f_t\), \(g_t\), \(o_t\) are the input, forget, cell, and output gates, respectively. \(\\Theta\) is the heaviside function, and \(*\) is the Hadamard product.

参数:
  • input_size (int) -- The number of expected features in the input x

  • hidden_size (int) -- The number of features in the hidden state h

  • num_layers (int) -- Number of recurrent layers. E.g., setting num_layers=2 would mean stacking two LSTMs together to form a stacked RNN, with the second RNN taking in outputs of the first RNN and computing the final results

  • bias (bool) -- If False, then the layer does not use bias weights b_ih and b_hh. Default: True

  • dropout_p (float) -- If non-zero, introduces a Dropout layer on the outputs of each RNN layer except the last layer, with dropout probability equal to dropout. Default: 0

  • invariant_dropout_mask (bool) -- If False,use the naive Dropout;If True,use the dropout in SNN that mask doesn't change in different time steps, see Dropout for more information. Default: False

  • bidirectional (bool) -- If True, becomes a bidirectional LSTM. Default: False

  • surrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- surrogate function for replacing gradient of spiking functions during back-propagation, which is used for generating i, f, o

  • surrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- surrogate function for replacing gradient of spiking functions during back-propagation, which is used for generating g. If None, the surrogate function for generating g will be set as surrogate_function1. Default: None

static base_cell()[源代码]#

API Language: 中文 | English


  • 中文

返回:

SpikingLSTMCell

返回类型:

nn.Module


  • English

返回:

SpikingLSTMCell

返回类型:

nn.Module

static states_num()[源代码]#

API Language: 中文 | English


  • 中文

返回:

2 (隐藏状态 h 和细胞状态 c

返回类型:

int


  • English

返回:

2 (hidden state h and cell state c)

返回类型:

int

class spikingjelly.activation_based.rnn.SpikingVanillaRNNCell(input_size: int, hidden_size: int, bias=True, surrogate_function=Erf(alpha=2.0, spiking=True))[源代码]#

基类:SpikingRNNCellBase

API Language: 中文 | English


  • 中文

单步脉冲 Vanilla RNN cell。

参数:

  • English

Single-step spiking Vanilla RNN cell.

参数:
  • input_size (int) -- Number of input features in x

  • hidden_size (int) -- Number of features in the hidden state h

  • bias (bool) -- If False, the internal linear layers do not use bias. Default: True

  • surrogate_function (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- Surrogate function used to replace the spike gradient during back-propagation

forward(x: Tensor, h=None)[源代码]#

API Language: 中文 | English


  • 中文

执行单步 Spiking Vanilla RNN cell 前向传播。

参数:
  • x (torch.Tensor) -- shape = [batch_size, input_size] 的输入

  • h (Optional[torch.Tensor]) -- shape = [batch_size, hidden_size] 的起始隐藏状态。若为 None,则使用零初始状态

返回:

shape = [batch_size, hidden_size] 的下一时刻隐藏状态

返回类型:

torch.Tensor


  • English

Run the forward pass of a single-step Spiking Vanilla RNN cell.

参数:
  • x (torch.Tensor) -- Input tensor with shape = [batch_size, input_size]

  • h (Optional[torch.Tensor]) -- Initial hidden state with shape = [batch_size, hidden_size]. If None, a zero state is used

返回:

Next hidden state with shape = [batch_size, hidden_size]

返回类型:

torch.Tensor

class spikingjelly.activation_based.rnn.SpikingVanillaRNN(input_size, hidden_size, num_layers, bias=True, dropout_p=0, invariant_dropout_mask=False, bidirectional=False, surrogate_function=Erf(alpha=2.0, spiking=True))[源代码]#

基类:SpikingRNNBase

API Language: 中文 | English


  • 中文

多层(可选双向)脉冲 Vanilla RNN。

参数:
  • input_size (int) -- 输入 x 的特征数

  • hidden_size (int) -- 隐藏状态 h 的特征数

  • num_layers (int) -- 循环层数

  • bias (bool) -- 若为 False,内部线性层不使用偏置项。默认为 True

  • dropout_p (float) -- 除最后一层外的层间 dropout 概率。默认为 0

  • invariant_dropout_mask (bool) -- 若为 True,则训练时在时间维共享 dropout mask

  • bidirectional (bool) -- 若为 True,则构造双向 RNN。默认为 False

  • surrogate_function (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- 反向传播时用于替代脉冲函数梯度的替代函数


  • English

Stacked spiking Vanilla RNN, optionally bidirectional.

参数:
  • input_size (int) -- Number of input features in x

  • hidden_size (int) -- Number of features in the hidden state h

  • num_layers (int) -- Number of recurrent layers

  • bias (bool) -- If False, the internal linear layers do not use bias. Default: True

  • dropout_p (float) -- Inter-layer dropout probability except for the last layer. Default: 0

  • invariant_dropout_mask (bool) -- If True, the dropout mask is shared across time steps during training

  • bidirectional (bool) -- If True, build a bidirectional RNN. Default: False

  • surrogate_function (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- Surrogate function used to replace the spike gradient during back-propagation

static base_cell()[源代码]#

API Language: 中文 | English


  • 中文

返回:

SpikingVanillaRNNCell

返回类型:

nn.Module


  • English

返回:

SpikingVanillaRNNCell

返回类型:

nn.Module

static states_num()[源代码]#

API Language: 中文 | English


  • 中文

返回:

1 (仅隐藏状态 h

返回类型:

int


  • English

返回:

1 (hidden state h only)

返回类型:

int

class spikingjelly.activation_based.rnn.SpikingGRUCell(input_size: int, hidden_size: int, bias=True, surrogate_function1=Erf(alpha=2.0, spiking=True), surrogate_function2=None)[源代码]#

基类:SpikingRNNCellBase

API Language: 中文 | English


  • 中文

单步脉冲 GRU cell。

参数:

  • English

Single-step spiking GRU cell.

参数:
forward(x: Tensor, h=None)[源代码]#

API Language: 中文 | English


  • 中文

执行单步 Spiking GRU cell 前向传播。

参数:
  • x (torch.Tensor) -- shape = [batch_size, input_size] 的输入

  • h (Optional[torch.Tensor]) -- shape = [batch_size, hidden_size] 的起始隐藏状态。若为 None,则使用零初始状态

返回:

shape = [batch_size, hidden_size] 的下一时刻隐藏状态

返回类型:

torch.Tensor


  • English

Run the forward pass of a single-step Spiking GRU cell.

参数:
  • x (torch.Tensor) -- Input tensor with shape = [batch_size, input_size]

  • h (Optional[torch.Tensor]) -- Initial hidden state with shape = [batch_size, hidden_size]. If None, a zero state is used

返回:

Next hidden state with shape = [batch_size, hidden_size]

返回类型:

torch.Tensor

class spikingjelly.activation_based.rnn.SpikingGRU(input_size, hidden_size, num_layers, bias=True, dropout_p=0, invariant_dropout_mask=False, bidirectional=False, surrogate_function1=Erf(alpha=2.0, spiking=True), surrogate_function2=None)[源代码]#

基类:SpikingRNNBase

API Language: 中文 | English


  • 中文

多层(可选双向)脉冲 GRU。

参数:
  • input_size (int) -- 输入 x 的特征数

  • hidden_size (int) -- 隐藏状态 h 的特征数

  • num_layers (int) -- 循环层数

  • bias (bool) -- 若为 False,内部线性层不使用偏置项。默认为 True

  • dropout_p (float) -- 除最后一层外的层间 dropout 概率。默认为 0

  • invariant_dropout_mask (bool) -- 若为 True,则训练时在时间维共享 dropout mask

  • bidirectional (bool) -- 若为 True,则构造双向 RNN。默认为 False

  • surrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- 用于 rz 门及默认候选态梯度近似的替代函数

  • surrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- 候选态 n 的替代函数。若为 None,则复用 surrogate_function1


  • English

Stacked spiking GRU, optionally bidirectional.

参数:
  • input_size (int) -- Number of input features in x

  • hidden_size (int) -- Number of features in the hidden state h

  • num_layers (int) -- Number of recurrent layers

  • bias (bool) -- If False, the internal linear layers do not use bias. Default: True

  • dropout_p (float) -- Inter-layer dropout probability except for the last layer. Default: 0

  • invariant_dropout_mask (bool) -- If True, the dropout mask is shared across time steps during training

  • bidirectional (bool) -- If True, build a bidirectional RNN. Default: False

  • surrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- Surrogate function used for the r and z gates and, by default, the candidate state

  • surrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- Surrogate function for the candidate state n. If None, surrogate_function1 is reused

static base_cell()[源代码]#

API Language: 中文 | English


  • 中文

返回:

SpikingGRUCell

返回类型:

nn.Module


  • English

返回:

SpikingGRUCell

返回类型:

nn.Module

static states_num()[源代码]#

API Language: 中文 | English


  • 中文

返回:

1 (仅隐藏状态 h

返回类型:

int


  • English

返回:

1 (hidden state h only)

返回类型:

int