spikingjelly.activation_based.encoding module#
- class spikingjelly.activation_based.encoding.StatelessEncoder(step_mode='s')[源代码]#
基类:
Module,StepModule
中文
无状态编码器的基类。 无状态编码器
encoder = StatelessEncoder(),直接调用encoder(x)即可将x编码为spike。- 参数:
step_mode (str) -- 步进模式,可以为
's'(单步) 或'm'(多步)
English
The base class of stateless encoder. The stateless encoder
encoder = StatelessEncoder()can encodextospikebyencoder(x).- 参数:
step_mode (str) -- the step mode, which can be
's'(single-step) or'm'(multi-step)
- abstractmethod forward(x: Tensor)[源代码]#
-
中文
编码函数,将输入
x编码为脉冲spike。- 参数:
x (torch.Tensor) -- 输入数据
- 返回:
脉冲张量,形状与
x.shape相同- 返回类型:
English
Encode function that converts input
xto spikespike.- 参数:
x (torch.Tensor) -- input data
- 返回:
spike, whose shape is same with
x.shape- 返回类型:
- class spikingjelly.activation_based.encoding.StatefulEncoder(T: int, step_mode='s')[源代码]#
基类:
MemoryModule
中文
有状态编码器的基类。 有状态编码器
encoder = StatefulEncoder(T),编码器会在首次调用encoder(x)时对x进行编码。 在第t次调用encoder(x)时会输出spike[t % T]。
English
The base class of stateful encoder. The stateful encoder
encoder = StatefulEncoder(T)will encodextospikeat the first time of callingencoder(x). It will outputspike[t % T]at thet-th calling- 参数:
代码示例 | Example
encoder = StatefulEncoder(T) s_list = [] for t in range(T): s_list.append(encoder(x)) # s_list[t] == spike[t]
- single_step_forward(x: Tensor = None)[源代码]#
-
中文
编码函数,返回第
t次调用的编码结果spike[t % T]。- 参数:
x (torch.Tensor) -- 输入数据
- 返回:
脉冲,shape 与
x.shape相同- 返回类型:
English
Encode function that returns the encoding result at the
t-th call,spike[t % T].- 参数:
x (torch.Tensor) -- input data
- 返回:
spike, whose shape is same with
x.shape- 返回类型:
- abstractmethod single_step_encode(x: Tensor)[源代码]#
-
中文
- 参数:
x (torch.Tensor) -- 输入数据
- 返回:
None
- 返回类型:
None
English
- 参数:
x (torch.Tensor) -- input data
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.encoding.PeriodicEncoder(spike: Tensor, step_mode='s')[源代码]#
-
中文
周期性编码器,在第
t次调用时输出spike[t % T],其中T = spike.shape[0]警告
不要忘记调用
reset(),因为这个编码器是有状态的。- 参数:
spike (torch.Tensor) -- 输入脉冲
step_mode (str) -- 步进模式,可以为
's'(单步) 或'm'(多步)
English
The periodic encoder that outputs
spike[t % T]att-th calling, whereT = spike.shape[0]Warning
Do not forget to reset the encoder because the encoder is stateful!
- 参数:
spike (torch.Tensor) -- the input spike
step_mode (str) -- the step mode, which can be
's'(single-step) or'm'(multi-step)
- single_step_encode(spike: Tensor)[源代码]#
-
中文
设置周期性编码器的脉冲序列。将输入的
spike作为编码器的脉冲序列,并将编码周期T设置为spike.shape[0]。- 参数:
spike (torch.Tensor) -- 脉冲张量,其第0维为编码周期
- 返回:
None
- 返回类型:
None
English
Sets the spike sequence for the periodic encoder. The input
spikeis used as the encoder's spike sequence, and the encoding periodTis set tospike.shape[0].- 参数:
spike (torch.Tensor) -- the spike tensor, whose 0-th dimension is the encoding period
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.encoding.LatencyEncoder(T: int, enc_function='linear', step_mode='s')[源代码]#
-
中文
延迟编码器,将
0 <= x <= 1的输入转化为在0 <= t_f <= T-1时刻发放的脉冲。输入的强度越大,发放越早。- 当
enc_function == 'linear': - \[t_f(x) = (T - 1)(1 - x)\]
- 当
enc_function == 'log': - \[t_f(x) = (T - 1) - \text{ln}(\alpha * x + 1)\]
其中 \(\alpha\) 满足 \(t_f(1) = T - 1\)
警告
必须确保
0 <= x <= 1。警告
不要忘记调用reset,因为这个编码器是有状态的。
- 参数:
English
The latency encoder will encode
0 <= x <= 1to spike whose firing time is0 <= t_f <= T-1. A largerxwill cause a earlier firing time.- If
enc_function == 'linear': - \[t_f(x) = (T - 1)(1 - x)\]
- If
enc_function == 'log': - \[t_f(x) = (T - 1) - \text{ln}(\alpha * x + 1)\]
where \(\alpha\) satisfies \(t_f(1) = T - 1\)
Warning
The user must assert
0 <= x <= 1.Warning
Do not forget to reset the encoder because the encoder is stateful!
- 参数:
代码示例 | Example
x = torch.rand(size=[8, 2]) print("x", x) T = 20 encoder = LatencyEncoder(T) for t in range(T): print(encoder(x))
- single_step_encode(x: Tensor)[源代码]#
-
中文
单步编码函数。根据输入的
x计算脉冲发放时刻 \(t_f\),并生成对应的 one-hot 脉冲张量。- 参数:
x (torch.Tensor) -- 输入数据,取值范围应为
[0, 1]- 返回:
None
- 返回类型:
None
English
Single-step encoding function. Computes the firing time \(t_f\) from input
xand generates the corresponding one-hot spike tensor.- 参数:
x (torch.Tensor) -- input data, which should be in the range
[0, 1]- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.encoding.PoissonEncoder(step_mode='s')[源代码]#
-
中文
无状态的泊松编码器。输出脉冲的发放概率与输入
x相同。警告
必须确保
0 <= x <= 1。- 参数:
step_mode (str) -- 步进模式,可以为
's'(单步) 或'm'(多步)
English
The poisson encoder will output spike whose firing probability is
x。Warning
The user must assert
0 <= x <= 1.- 参数:
step_mode (str) -- the step mode, which can be
's'(single-step) or'm'(multi-step)
- forward(x: Tensor)[源代码]#
-
中文
前向传播。根据输入
x中的概率值生成泊松脉冲序列。每个元素以概率x独立地发放脉冲。- 参数:
x (torch.Tensor) -- 输入数据,取值范围应为
[0, 1],表示脉冲发放概率- 返回:
脉冲张量,形状与
x相同- 返回类型:
English
Forward pass. Generates Poisson spike trains based on the probability values in
x. Each element independently fires a spike with probabilityx.- 参数:
x (torch.Tensor) -- input data in the range
[0, 1], representing firing probabilities- 返回:
spike tensor with the same shape as
x- 返回类型:
- class spikingjelly.activation_based.encoding.WeightedPhaseEncoder(K: int, step_mode='s')[源代码]#
-
中文
带权的相位编码,一种基于二进制表示的编码方法。
将输入按照二进制各位展开,从高位到低位遍历输入进行脉冲编码。相比于频率编码,每一位携带的信息量更多。编码相位数为 \(K\) 时, 可以对于处于区间 \([0, 1-2^{-K}]\) 的数进行编码。以下为原始论文中的示例:
Phase (K=8)
1
2
3
4
5
6
7
8
Spike weight \(\omega(t)\)
2-1
2-2
2-3
2-4
2-5
2-6
2-7
2-8
192/256
1
1
0
0
0
0
0
0
1/256
0
0
0
0
0
0
0
1
128/256
1
0
0
0
0
0
0
0
255/256
1
1
1
1
1
1
1
1
参考文献: Kim J, Kim H, Huh S, et al. Deep neural networks with weighted spikes[J]. Neurocomputing, 2018, 311: 373-386.
警告
不要忘记调用reset,因为这个编码器是有状态的。
English
The weighted phase encoder, which is based on binary system. It will flatten
xas a binary number. WhenT=K, it can encode \(x \in [0, 1-2^{-K}]\) to different spikes. Here is the example from the origin paper:Phase (K=8)
1
2
3
4
5
6
7
8
Spike weight \(\omega(t)\)
2-1
2-2
2-3
2-4
2-5
2-6
2-7
2-8
192/256
1
1
0
0
0
0
0
0
1/256
0
0
0
0
0
0
0
1
128/256
1
0
0
0
0
0
0
0
255/256
1
1
1
1
1
1
1
1
Reference: Kim J, Kim H, Huh S, et al. Deep neural networks with weighted spikes[J]. Neurocomputing, 2018, 311: 373-386.
Warning
Do not forget to reset the encoder because the encoder is stateful!
- 参数:
- single_step_encode(x: Tensor)[源代码]#
-
中文
单步编码函数。将输入
x按照二进制加权相位编码方式展开为脉冲序列。- 参数:
x (torch.Tensor) -- 输入数据,取值范围应为
[0, 1 - 2^{-T}]- 抛出:
AssertionError -- 当
x不在[0, 1 - 2^{-T}]范围内时
English
Single-step encoding function. Encodes the input
xinto spike trains using the weighted phase encoding scheme based on binary decomposition.- 参数:
x (torch.Tensor) -- input data, which should be in the range
[0, 1 - 2^{-T}]- 抛出:
AssertionError -- if
xis not in the range[0, 1 - 2^{-T}]
- class spikingjelly.activation_based.encoding.PopSpikeEncoderDeterministic(obs_dim, pop_dim, spike_ts, mean_range, std)[源代码]#
基类:
Module
中文
可学习的群体编码脉冲编码器,使用确定性脉冲序列。编码器使用高斯函数作为感受野,将输入观测映射到群体输出。
- 参数:
English
Learnable population coding spike encoder with deterministic spike trains. The encoder uses Gaussian functions as receptive fields to map input observations to population outputs.
- 参数:
- forward(obs)[源代码]#
-
中文
前向传播。将输入观测映射为高斯群体激活,并在
spike_ts个时间步上重复后输入脉冲神经元,输出确定性脉冲序列。- 参数:
obs (torch.Tensor) -- 输入观测张量,最后一维大小应与
obs_dim一致- 返回:
编码后的脉冲序列,形状为
[spike_ts, batch_size, obs_dim * pop_dim]- 返回类型:
English
Forward pass. The input observations are mapped to Gaussian population activations, repeated for
spike_tstime steps, and fed into spiking neurons to produce deterministic spike trains.- 参数:
obs (torch.Tensor) -- input observation tensor whose last dimension should match
obs_dim- 返回:
encoded spike sequence with shape
[spike_ts, batch_size, obs_dim * pop_dim]- 返回类型:
- class spikingjelly.activation_based.encoding.PopSpikeEncoderRandom(obs_dim, pop_dim, spike_ts, mean_range, std)[源代码]#
基类:
Module
中文
可学习的群体编码脉冲编码器,使用随机脉冲序列。编码器使用高斯函数作为感受野,将输入观测映射到群体输出。
- 参数:
English
Learnable population coding spike encoder with random spike trains. The encoder uses Gaussian functions as receptive fields to map input observations to population outputs.
- 参数:
- forward(obs)[源代码]#
-
中文
前向传播。将输入观测映射为高斯群体激活,并在每个时间步通过随机采样生成脉冲序列。
- 参数:
obs (torch.Tensor) -- 输入观测张量,最后一维大小应与
obs_dim一致- 返回:
随机脉冲序列,形状为
[spike_ts, batch_size, obs_dim * pop_dim]- 返回类型:
English
Forward pass. The input observations are mapped to Gaussian population activations, and random spikes are sampled at each time step.
- 参数:
obs (torch.Tensor) -- input observation tensor whose last dimension should match
obs_dim- 返回:
random spike sequence with shape
[spike_ts, batch_size, obs_dim * pop_dim]- 返回类型:
- class spikingjelly.activation_based.encoding.PopEncoder(obs_dim, pop_dim, spike_ts, mean_range, std)[源代码]#
基类:
Module
中文
可学习的群体编码器,输出编码后的脉冲输入序列,用于SNN训练。
- 参数:
English
Learnable population coding encoder that outputs encoded spike input sequences for SNN training.
- 参数:
- forward(obs)[源代码]#
-
中文
前向传播。将输入观测映射为高斯群体激活,并在
spike_ts个时间步上复制该激活,返回连续输入序列。- 参数:
obs (torch.Tensor) -- 输入观测张量,最后一维大小应与
obs_dim一致- 返回:
编码输入序列,形状为
[spike_ts, batch_size, obs_dim * pop_dim]- 返回类型:
English
Forward pass. The input observations are mapped to Gaussian population activations and repeated for
spike_tstime steps as a continuous input sequence.- 参数:
obs (torch.Tensor) -- input observation tensor whose last dimension should match
obs_dim- 返回:
encoded input sequence with shape
[spike_ts, batch_size, obs_dim * pop_dim]- 返回类型: