spikingjelly.activation_based.encoding module#

class spikingjelly.activation_based.encoding.StatelessEncoder(step_mode='s')[源代码]#

基类:Module, StepModule

API Language: 中文 | English


  • 中文

无状态编码器的基类。 无状态编码器 encoder = StatelessEncoder(),直接调用 encoder(x) 即可将 x 编码为 spike

参数:

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


  • English

The base class of stateless encoder. The stateless encoder encoder = StatelessEncoder() can encode x to spike by encoder(x).

参数:

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

abstractmethod forward(x: Tensor)[源代码]#

API Language: 中文 | English


  • 中文

编码函数,将输入 x 编码为脉冲 spike

参数:

x (torch.Tensor) -- 输入数据

返回:

脉冲张量,形状与 x.shape 相同

返回类型:

torch.Tensor


  • English

Encode function that converts input x to spike spike .

参数:

x (torch.Tensor) -- input data

返回:

spike, whose shape is same with x.shape

返回类型:

torch.Tensor

class spikingjelly.activation_based.encoding.StatefulEncoder(T: int, step_mode='s')[源代码]#

基类:MemoryModule

API Language: 中文 | English


  • 中文

有状态编码器的基类。 有状态编码器 encoder = StatefulEncoder(T) ,编码器会在首次调用 encoder(x) 时对 x 进行编码。 在第 t 次调用 encoder(x) 时会输出 spike[t % T]

参数:
  • T (int) -- 编码周期。通常情况下,与SNN的仿真周期(总步长一致)

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


  • English

The base class of stateful encoder. The stateful encoder encoder = StatefulEncoder(T) will encode x to spike at the first time of calling encoder(x). It will output spike[t % T] at the t -th calling

参数:
  • T (int) -- the encoding period. It is usually same with the total simulation time-steps of SNN

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


  • 代码示例 | 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)[源代码]#

API Language: 中文 | English


  • 中文

编码函数,返回第 t 次调用的编码结果 spike[t % T]

参数:

x (torch.Tensor) -- 输入数据

返回:

脉冲,shape 与 x.shape 相同

返回类型:

torch.Tensor


  • 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

返回类型:

torch.Tensor

abstractmethod single_step_encode(x: Tensor)[源代码]#

API Language: 中文 | English


  • 中文

参数:

x (torch.Tensor) -- 输入数据

返回:

None

返回类型:

None


  • English

参数:

x (torch.Tensor) -- input data

返回:

None

返回类型:

None

class spikingjelly.activation_based.encoding.PeriodicEncoder(spike: Tensor, step_mode='s')[源代码]#

基类:StatefulEncoder

API Language: 中文 | English


  • 中文

周期性编码器,在第 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] at t -th calling, where T = 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)[源代码]#

API Language: 中文 | English


  • 中文

设置周期性编码器的脉冲序列。将输入的 spike 作为编码器的脉冲序列,并将编码周期 T 设置为 spike.shape[0]

参数:

spike (torch.Tensor) -- 脉冲张量,其第0维为编码周期

返回:

None

返回类型:

None


  • English

Sets the spike sequence for the periodic encoder. The input spike is used as the encoder's spike sequence, and the encoding period T is set to spike.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')[源代码]#

基类:StatefulEncoder

API Language: 中文 | English


  • 中文

延迟编码器,将 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,因为这个编码器是有状态的。

参数:
  • T (int) -- 最大(最晚)脉冲发放时刻

  • enc_function (str) -- 定义使用哪个函数将输入强度转化为脉冲发放时刻,可以为 linearlog

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


  • English

The latency encoder will encode 0 <= x <= 1 to spike whose firing time is 0 <= t_f <= T-1. A larger x will 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!

参数:
  • T (int) -- the maximum (latest) firing time

  • enc_function (str) -- how to convert intensity to firing time. linear or log

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


  • 代码示例 | 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)[源代码]#

API Language: 中文 | English


  • 中文

单步编码函数。根据输入的 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 x and 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')[源代码]#

基类:StatelessEncoder

API Language: 中文 | English


  • 中文

无状态的泊松编码器。输出脉冲的发放概率与输入 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)[源代码]#

API Language: 中文 | English


  • 中文

前向传播。根据输入 x 中的概率值生成泊松脉冲序列。每个元素以概率 x 独立地发放脉冲。

参数:

x (torch.Tensor) -- 输入数据,取值范围应为 [0, 1],表示脉冲发放概率

返回:

脉冲张量,形状与 x 相同

返回类型:

torch.Tensor


  • English

Forward pass. Generates Poisson spike trains based on the probability values in x. Each element independently fires a spike with probability x.

参数:

x (torch.Tensor) -- input data in the range [0, 1], representing firing probabilities

返回:

spike tensor with the same shape as x

返回类型:

torch.Tensor

class spikingjelly.activation_based.encoding.WeightedPhaseEncoder(K: int, step_mode='s')[源代码]#

基类:StatefulEncoder

API Language: 中文 | English


  • 中文

带权的相位编码,一种基于二进制表示的编码方法。

将输入按照二进制各位展开,从高位到低位遍历输入进行脉冲编码。相比于频率编码,每一位携带的信息量更多。编码相位数为 \(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,因为这个编码器是有状态的。

参数:
  • K (int) -- 编码周期。通常情况下,与SNN的仿真周期(总步长一致)

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


  • English

The weighted phase encoder, which is based on binary system. It will flatten x as a binary number. When T=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!

参数:
  • K (int) -- the encoding period. It is usually same with the total simulation time-steps of SNN

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

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

API Language: 中文 | English


  • 中文

单步编码函数。将输入 x 按照二进制加权相位编码方式展开为脉冲序列。

参数:

x (torch.Tensor) -- 输入数据,取值范围应为 [0, 1 - 2^{-T}]

抛出:

AssertionError -- 当 x 不在 [0, 1 - 2^{-T}] 范围内时


  • English

Single-step encoding function. Encodes the input x into 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 x is not in the range [0, 1 - 2^{-T}]

class spikingjelly.activation_based.encoding.PopSpikeEncoderDeterministic(obs_dim, pop_dim, spike_ts, mean_range, std)[源代码]#

基类:Module

API Language: 中文 | English


  • 中文

可学习的群体编码脉冲编码器,使用确定性脉冲序列。编码器使用高斯函数作为感受野,将输入观测映射到群体输出。

参数:
  • obs_dim (int) -- 观测空间的维度

  • pop_dim (int) -- 每个观测维度的编码器数量

  • spike_ts (int) -- 脉冲时间步数

  • mean_range (tuple) -- 均值范围 [min, max]

  • std (float) -- 标准差


  • 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.

参数:
  • obs_dim (int) -- dimension of observation space

  • pop_dim (int) -- number of encoders per observation dimension

  • spike_ts (int) -- number of spike time steps

  • mean_range (tuple) -- mean range [min, max]

  • std (float) -- standard deviation

forward(obs)[源代码]#

API Language: 中文 | English


  • 中文

前向传播。将输入观测映射为高斯群体激活,并在 spike_ts 个时间步上重复后输入脉冲神经元,输出确定性脉冲序列。

参数:

obs (torch.Tensor) -- 输入观测张量,最后一维大小应与 obs_dim 一致

返回:

编码后的脉冲序列,形状为 [spike_ts, batch_size, obs_dim * pop_dim]

返回类型:

torch.Tensor


  • English

Forward pass. The input observations are mapped to Gaussian population activations, repeated for spike_ts time 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]

返回类型:

torch.Tensor

class spikingjelly.activation_based.encoding.PopSpikeEncoderRandom(obs_dim, pop_dim, spike_ts, mean_range, std)[源代码]#

基类:Module

API Language: 中文 | English


  • 中文

可学习的群体编码脉冲编码器,使用随机脉冲序列。编码器使用高斯函数作为感受野,将输入观测映射到群体输出。

参数:
  • obs_dim (int) -- 观测空间的维度

  • pop_dim (int) -- 每个观测维度的编码器数量

  • spike_ts (int) -- 脉冲时间步数

  • mean_range (tuple) -- 均值范围 [min, max]

  • std (float) -- 标准差


  • 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.

参数:
  • obs_dim (int) -- dimension of observation space

  • pop_dim (int) -- number of encoders per observation dimension

  • spike_ts (int) -- number of spike time steps

  • mean_range (tuple) -- mean range [min, max]

  • std (float) -- standard deviation

forward(obs)[源代码]#

API Language: 中文 | English


  • 中文

前向传播。将输入观测映射为高斯群体激活,并在每个时间步通过随机采样生成脉冲序列。

参数:

obs (torch.Tensor) -- 输入观测张量,最后一维大小应与 obs_dim 一致

返回:

随机脉冲序列,形状为 [spike_ts, batch_size, obs_dim * pop_dim]

返回类型:

torch.Tensor


  • 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]

返回类型:

torch.Tensor

class spikingjelly.activation_based.encoding.PopEncoder(obs_dim, pop_dim, spike_ts, mean_range, std)[源代码]#

基类:Module

API Language: 中文 | English


  • 中文

可学习的群体编码器,输出编码后的脉冲输入序列,用于SNN训练。

参数:
  • obs_dim (int) -- 观测空间的维度

  • pop_dim (int) -- 每个观测维度的编码器数量

  • spike_ts (int) -- 脉冲时间步数

  • mean_range (tuple) -- 均值范围 [min, max]

  • std (float) -- 标准差


  • English

Learnable population coding encoder that outputs encoded spike input sequences for SNN training.

参数:
  • obs_dim (int) -- dimension of observation space

  • pop_dim (int) -- number of encoders per observation dimension

  • spike_ts (int) -- number of spike time steps

  • mean_range (tuple) -- mean range [min, max]

  • std (float) -- standard deviation

forward(obs)[源代码]#

API Language: 中文 | English


  • 中文

前向传播。将输入观测映射为高斯群体激活,并在 spike_ts 个时间步上复制该激活,返回连续输入序列。

参数:

obs (torch.Tensor) -- 输入观测张量,最后一维大小应与 obs_dim 一致

返回:

编码输入序列,形状为 [spike_ts, batch_size, obs_dim * pop_dim]

返回类型:

torch.Tensor


  • English

Forward pass. The input observations are mapped to Gaussian population activations and repeated for spike_ts time 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]

返回类型:

torch.Tensor