spikingjelly.activation_based.rnn module#
- spikingjelly.activation_based.rnn.directional_rnn_cell_forward(cell: Module, x: Tensor, states: Tensor)[源代码]#
-
中文
沿时间维对单个 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相同- 返回类型:
- 抛出:
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
xis the first dimension. The function repeatedly evaluatescell(x[t], ss)wheressis the current state. Ifstatesis a 2-D tensor, the cell is treated as having a single state tensor. Ifstatesis 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]; otherwiseshape = [states_num, batch_size, hidden_size]
- 返回:
(output, final_states), whereoutputhas shape[T, batch_size, hidden_size]andfinal_stateshas the same shape asstates- 返回类型:
- 抛出:
ValueError -- Callers are expected to provide
stateswith rank 2 or 3
- spikingjelly.activation_based.rnn.bidirectional_rnn_cell_forward(cell: Module, cell_reverse: Module, x: Tensor, states: Tensor, states_reverse: Tensor)[源代码]#
-
中文
沿时间维同时执行正向与反向 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],由正向与反向输出拼接而成; 其余两个返回值的形状分别与states、states_reverse相同- 返回类型:
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 consumesx[t]and the reverse cell consumesx[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]; otherwiseshape = [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]; otherwiseshape = [states_num, batch_size, hidden_size]
- 返回:
(output, final_states, final_states_reverse).outputhas shape[T, batch_size, 2 * hidden_size]and concatenates the forward and reverse outputs. The other two return values have the same shapes asstatesandstates_reverse, respectively- 返回类型:
- class spikingjelly.activation_based.rnn.SpikingRNNCellBase(input_size: int, hidden_size: int, bias=True)[源代码]#
基类:
Module
中文
Spiking RNN cell 的基类。
- 参数:
备注
所有权重和偏置项都会按照 \(\\mathcal{U}(-\\sqrt{k}, \\sqrt{k})\) 进行初始化。 其中 \(k = \\frac{1}{\\text{hidden_size}}\).
English
The base class of Spiking RNN Cell.
- 参数:
Note
All the weights and biases are initialized from \(\\mathcal{U}(-\\sqrt{k}, \\sqrt{k})\) where \(k = \\frac{1}{\\text{hidden_size}}\).
- reset_parameters()[源代码]#
-
中文
初始化所有可学习参数。
- 返回:
None- 返回类型:
None
English
Initialize all learnable parameters.
- 返回:
None- 返回类型:
None
- 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
中文
多层(可选双向)脉冲 RNN 的基类。
- 参数:
input_size (int) -- 输入
x的特征数hidden_size (int) -- 隐藏状态
h的特征数num_layers (int) -- 内部RNN的层数,例如
num_layers = 2将会创建堆栈式的两层RNN,第1层接收第0层的输出作为输入, 并计算最终输出bias (bool) -- 若为
False, 则内部的隐藏层不会带有偏置项b_ih和b_hh。 默认为Truedropout_p (float) -- 若非
0,则除了最后一层,每个RNN层后会增加一个丢弃概率为dropout_p的 Dropout 层。 默认为0invariant_dropout_mask (bool) -- 若为
False,则使用普通的 Dropout;若为True,则使用SNN中特有的,mask 不 随着时间变化的 Dropout`,参见Dropout。默认为Falsebidirectional (bool) -- 若为
True,则使用双向RNN。默认为Falseargs -- 子类使用的额外参数
kwargs -- 子类使用的额外参数
English
The base-class of a multi-layer spiking RNN.
- 参数:
input_size (int) -- The number of expected features in the input
xhidden_size (int) -- The number of features in the hidden state
hnum_layers (int) -- Number of recurrent layers. E.g., setting
num_layers=2would 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 resultsbias (bool) -- If
False, then the layer does not use bias weights b_ih and b_hh. Default:Truedropout_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: 0invariant_dropout_mask (bool) -- If
False,use the naive Dropout;IfTrue,use the dropout in SNN that mask doesn't change in different time steps, seeDropoutfor more information. Default:Falsebidirectional (bool) -- If
True, becomes a bidirectional LSTM. Default:Falseargs -- additional arguments for sub-class
kwargs -- additional arguments for sub-class
- create_cells(*args, **kwargs)[源代码]#
-
中文
- 参数:
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()[源代码]#
-
中文
- 返回:
构成该RNN的基本RNN Cell。例如对于
SpikingLSTM, 返回的是SpikingLSTMCell- 返回类型:
nn.Module
English
- 返回:
The base cell of this RNN. E.g., in
SpikingLSTMthis function will returnSpikingLSTMCell- 返回类型:
nn.Module
- static states_num()[源代码]#
-
中文
- 返回:
状态变量的数量。例如对于
SpikingLSTM,由于其输出是h和c, 因此返回2;而对于SpikingGRU,由于其输出是h,因此返回1- 返回类型:
English
- 返回:
The states number. E.g., for
SpikingLSTMthe output arehandc, this function will return2; forSpikingGRUthe output ish, this function will return1- 返回类型:
- forward(x: Tensor, states=None)[源代码]#
-
中文
执行多层(可选双向)脉冲 RNN 的前向传播。
输入
x的时间维位于第 0 维。若states为None,则会在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_directions为2, 否则为1
- 返回:
(output, output_states)。output的形状为[T, batch, num_directions * hidden_size];output_states在self.states_num() == 1时为单个张量,否则为包含多个状态张量的 tuple- 返回类型:
tuple[torch.Tensor, Union[torch.Tensor, tuple]]
- 抛出:
TypeError -- 若
states既不是None、torch.Tensor也不是tupleValueError -- 若
states的层数/方向数维度与当前 RNN 配置不匹配
English
Run the forward pass of a stacked spiking RNN, optionally bidirectional.
The time axis of
xis the first dimension. IfstatesisNone, zero initial states are created onx.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 sequencestates (Union[torch.Tensor, tuple]) -- a single tensor when
self.states_num()is1, otherwise a tuple withself.states_num()tensors.shape = [num_layers * num_directions, batch, hidden_size]for all tensors, containing theself.states_num()initial states for each element in the batch. If the RNN is bidirectional,num_directionsshould be2, else it should be1
- 返回:
(output, output_states).outputhas shape[T, batch, num_directions * hidden_size].output_statesis a single tensor whenself.states_num() == 1; otherwise it is a tuple of state tensors- 返回类型:
tuple[torch.Tensor, Union[torch.Tensor, tuple]]
- 抛出:
TypeError -- If
statesis neitherNone,torch.TensornortupleValueError -- If the layer/direction dimension of
statesdoes 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)[源代码]#
-
中文
脉冲 长短时记忆 (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点积,即逐元素相乘。
- 参数:
input_size (int) -- 输入
x的特征数hidden_size (int) -- 隐藏状态
h的特征数bias (bool) -- 若为
False, 则内部的隐藏层不会带有偏置项b_ih和b_hh。 默认为Truesurrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数, 计算
i,f,o反向传播时使用surrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- 反向传播时用来计算脉冲函数梯度的替代函数, 计算
g反向传播时使用。 若为None, 则设置成surrogate_function1。默认为None
备注
所有权重和偏置项都会按照 \(\\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
xhidden_size (int) -- The number of features in the hidden state
hbias (bool) -- If
False, then the layer does not use bias weightsb_ihandb_hh. Default:Truesurrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- surrogate function for replacing gradient of spiking functions during back-propagation, which is used for generating
i,f,osurrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- surrogate function for replacing gradient of spiking functions during back-propagation, which is used for generating
g. IfNone, the surrogate function for generatinggwill be set assurrogate_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)[源代码]#
-
中文
执行单步 Spiking LSTM cell 前向传播。
若
hc为None,则会在x.device上构造零初始状态h_0和c_0。返回值中的h_1、c_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_0和c_0默认为零张量
- 返回:
(h_1, c_1),两个张量的形状均为[batch_size, hidden_size]- 返回类型:
English
Run the forward pass of a single-step Spiking LSTM cell.
If
hcisNone, zero initial statesh_0andc_0are created onx.device. The returnedh_1andc_1are 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]. IfNone, both states default to zeros
- 返回:
(h_1, c_1), both tensors have shape[batch_size, hidden_size]- 返回类型:
- 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)[源代码]#
-
中文
多层`脉冲` 长短时记忆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_ih和b_hh。 默认为Truedropout_p (float) -- 若非
0,则除了最后一层,每个RNN层后会增加一个丢弃概率为dropout_p的 Dropout 层。 默认为0invariant_dropout_mask (bool) -- 若为
False,则使用普通的 Dropout;若为True,则使用SNN中特有的,mask 不 随着时间变化的 Dropout`,参见Dropout。默认为Falsebidirectional (bool) -- 若为
True,则使用双向RNN。默认为Falsesurrogate_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
xhidden_size (int) -- The number of features in the hidden state
hnum_layers (int) -- Number of recurrent layers. E.g., setting
num_layers=2would 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 resultsbias (bool) -- If
False, then the layer does not use bias weights b_ih and b_hh. Default:Truedropout_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: 0invariant_dropout_mask (bool) -- If
False,use the naive Dropout;IfTrue,use the dropout in SNN that mask doesn't change in different time steps, seeDropoutfor more information. Default:Falsebidirectional (bool) -- If
True, becomes a bidirectional LSTM. Default:Falsesurrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- surrogate function for replacing gradient of spiking functions during back-propagation, which is used for generating
i,f,osurrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- surrogate function for replacing gradient of spiking functions during back-propagation, which is used for generating
g. IfNone, the surrogate function for generatinggwill be set assurrogate_function1. Default:None
- class spikingjelly.activation_based.rnn.SpikingVanillaRNNCell(input_size: int, hidden_size: int, bias=True, surrogate_function=Erf(alpha=2.0, spiking=True))[源代码]#
-
中文
单步脉冲 Vanilla RNN cell。
- 参数:
input_size (int) -- 输入
x的特征数hidden_size (int) -- 隐藏状态
h的特征数bias (bool) -- 若为
False,则内部线性层不使用偏置项。默认为Truesurrogate_function (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- 反向传播时用于替代脉冲函数梯度的替代函数
English
Single-step spiking Vanilla RNN cell.
- 参数:
input_size (int) -- Number of input features in
xhidden_size (int) -- Number of features in the hidden state
hbias (bool) -- If
False, the internal linear layers do not use bias. Default:Truesurrogate_function (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- Surrogate function used to replace the spike gradient during back-propagation
- forward(x: Tensor, h=None)[源代码]#
-
中文
执行单步 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]的下一时刻隐藏状态- 返回类型:
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]. IfNone, a zero state is used
- 返回:
Next hidden state with
shape = [batch_size, hidden_size]- 返回类型:
- 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))[源代码]#
-
中文
多层(可选双向)脉冲 Vanilla RNN。
- 参数:
input_size (int) -- 输入
x的特征数hidden_size (int) -- 隐藏状态
h的特征数num_layers (int) -- 循环层数
bias (bool) -- 若为
False,内部线性层不使用偏置项。默认为Truedropout_p (float) -- 除最后一层外的层间 dropout 概率。默认为
0invariant_dropout_mask (bool) -- 若为
True,则训练时在时间维共享 dropout maskbidirectional (bool) -- 若为
True,则构造双向 RNN。默认为Falsesurrogate_function (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- 反向传播时用于替代脉冲函数梯度的替代函数
English
Stacked spiking Vanilla RNN, optionally bidirectional.
- 参数:
input_size (int) -- Number of input features in
xhidden_size (int) -- Number of features in the hidden state
hnum_layers (int) -- Number of recurrent layers
bias (bool) -- If
False, the internal linear layers do not use bias. Default:Truedropout_p (float) -- Inter-layer dropout probability except for the last layer. Default:
0invariant_dropout_mask (bool) -- If
True, the dropout mask is shared across time steps during trainingbidirectional (bool) -- If
True, build a bidirectional RNN. Default:Falsesurrogate_function (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- Surrogate function used to replace the spike gradient during back-propagation
- 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)[源代码]#
-
中文
单步脉冲 GRU cell。
- 参数:
input_size (int) -- 输入
x的特征数hidden_size (int) -- 隐藏状态
h的特征数bias (bool) -- 若为
False,则内部线性层不使用偏置项。默认为Truesurrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- 用于
r、z门及默认候选态梯度近似的替代函数surrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- 候选态
n的替代函数。若为None,则复用surrogate_function1
English
Single-step spiking GRU cell.
- 参数:
input_size (int) -- Number of input features in
xhidden_size (int) -- Number of features in the hidden state
hbias (bool) -- If
False, the internal linear layers do not use bias. Default:Truesurrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- Surrogate function used for the
randzgates and, by default, the candidate statesurrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- Surrogate function for the candidate state
n. IfNone,surrogate_function1is reused
- forward(x: Tensor, h=None)[源代码]#
-
中文
执行单步 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]的下一时刻隐藏状态- 返回类型:
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]. IfNone, a zero state is used
- 返回:
Next hidden state with
shape = [batch_size, hidden_size]- 返回类型:
- 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)[源代码]#
-
中文
多层(可选双向)脉冲 GRU。
- 参数:
input_size (int) -- 输入
x的特征数hidden_size (int) -- 隐藏状态
h的特征数num_layers (int) -- 循环层数
bias (bool) -- 若为
False,内部线性层不使用偏置项。默认为Truedropout_p (float) -- 除最后一层外的层间 dropout 概率。默认为
0invariant_dropout_mask (bool) -- 若为
True,则训练时在时间维共享 dropout maskbidirectional (bool) -- 若为
True,则构造双向 RNN。默认为Falsesurrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- 用于
r、z门及默认候选态梯度近似的替代函数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
xhidden_size (int) -- Number of features in the hidden state
hnum_layers (int) -- Number of recurrent layers
bias (bool) -- If
False, the internal linear layers do not use bias. Default:Truedropout_p (float) -- Inter-layer dropout probability except for the last layer. Default:
0invariant_dropout_mask (bool) -- If
True, the dropout mask is shared across time steps during trainingbidirectional (bool) -- If
True, build a bidirectional RNN. Default:Falsesurrogate_function1 (spikingjelly.activation_based.surrogate.SurrogateFunctionBase) -- Surrogate function used for the
randzgates and, by default, the candidate statesurrogate_function2 (Optional[spikingjelly.activation_based.surrogate.SurrogateFunctionBase]) -- Surrogate function for the candidate state
n. IfNone,surrogate_function1is reused