Miscellaneous#

其他辅助 工具函数


Other auxiliary tool functions .

spikingjelly.activation_based.functional.misc.set_threshold_margin(output_layer: BaseNode, label_one_hot: Tensor, eval_threshold=1.0, threshold0=0.9, threshold1=1.1)[源代码]#

API Language: 中文 | English


  • 中文

对于用来分类的网络,为输出层神经元的电压阈值设置一定的裕量,以获得更好的分类性能。

类别总数为C,网络的输出层共有C个神经元。网络在训练时,当输入真实类别为i的数据, 输出层中第i个神经元的电压阈值会被设置成 threshold1 ,而其他神经元的电压阈值会被设置成 threshold0 。而在测试(推理)时,输出层中神经元的电压阈值被统一设置成 eval_threshold

参数:
  • output_layer (neuron.BaseNode) -- 用于分类的网络的输出层,输出层输出shape=[batch_size, C]

  • label_one_hot (torch.Tensor) -- one hot格式的样本标签,shape=[batch_size, C]

  • eval_threshold (float) -- 输出层神经元在测试(推理)时使用的电压阈值

  • threshold0 (float) -- 输出层神经元在训练时,负样本的电压阈值

  • threshold1 (float) -- 输出层神经元在训练时,正样本的电压阈值

返回:

None

返回类型:

None

抛出:

RuntimeError -- 若 output_layer.v_threshold 不支持被赋值为标量或与 label_one_hot 同形状的张量,则底层赋值异常会原样向上传播


  • English

Set voltage threshold margin for neurons in the output layer to reach better performance in classification task.

When there are C different classes, the output layer contains C neurons. During training, when the input with groundtruth label i are sent into the network, the voltage threshold of the i-th neurons in the output layer will be set to threshold1 and the remaining will be set to threshold0.

During inference, the voltage thresholds of ALL neurons in the output layer will be set to eval_threshold.

参数:
  • output_layer (neuron.BaseNode) -- The output layer of classification network, where the shape of output should be [batch_size, C]

  • label_one_hot (torch.Tensor) -- Labels in one-hot format, shape=[batch_size, C]

  • eval_threshold (float) -- Voltage threshold of neurons in output layer when evaluating (inference)

  • threshold0 (float) -- Voltage threshold of the corresponding neurons of negative samples in output layer when training

  • threshold1 (float) -- Voltage threshold of the corresponding neurons of positive samples in output layer when training

返回:

None

返回类型:

None

抛出:

RuntimeError -- Any assignment error raised while updating output_layer.v_threshold is propagated unchanged

spikingjelly.activation_based.functional.misc.redundant_one_hot(labels: Tensor, num_classes: int, n: int)[源代码]#

API Language: 中文 | English


  • 中文

对数据进行冗余的one-hot编码,每一类用 n 个1和 (num_classes - 1) * n 个0来编码。

参数:
  • labels (torch.Tensor) -- shape=[batch_size]的tensor,表示 batch_size 个标签

  • num_classes (int) -- 类别总数

  • n (int) -- 表示每个类别所用的编码数量

返回:

形状为 [batch_size, num_classes * n] 的tensor

返回类型:

torch.Tensor

抛出:

RuntimeError -- 若 labels 中存在不在 [0, num_classes - 1] 范围内的值,则 F.one_hot 会抛出异常


  • English

Redundant one-hot encoding for data. Each class is encoded to n 1's and (num_classes - 1) * n 0's

参数:
  • labels (torch.Tensor) -- Tensor of shape=[batch_size], batch_size labels

  • num_classes (int) -- The total number of classes.

  • n (int) -- The encoding length for each class.

返回:

Tensor of shape [batch_size, num_classes * n]

返回类型:

torch.Tensor

抛出:

RuntimeError -- F.one_hot raises if labels contains values outside [0, num_classes - 1]


  • 代码示例 | Example

>>> num_classes = 3
>>> n = 2
>>> labels = torch.randint(0, num_classes, [4])
>>> labels
tensor([0, 1, 1, 0])
>>> codes = functional.redundant_one_hot(labels, num_classes, n)
>>> codes
tensor([[1., 1., 0., 0., 0., 0.],
        [0., 0., 1., 1., 0., 0.],
        [0., 0., 1., 1., 0., 0.],
        [1., 1., 0., 0., 0., 0.]])
spikingjelly.activation_based.functional.misc.first_spike_index(spikes: Tensor)[源代码]#

API Language: 中文 | English


  • 中文

输入若干个神经元的输出脉冲,返回一个与输入相同shape的 bool 类型的index。 index为 True 的位置,表示该神经元首次释放脉冲的时刻。

参数:

spikes (torch.Tensor) -- shape=[*, T] ,表示任意个神经元在 \(t=0, 1, ..., T-1\) , 共T个时刻的输出脉冲

返回:

indexshape=[*, T] ,为 True 的位置表示该神经元首次释放脉冲的时刻

返回类型:

torch.Tensor

抛出:

RuntimeError -- 若 spikes 不是可进行 cumsum 的张量类型,则底层张量操作异常会原样向上传播


  • English

Return an index tensor of the same shape of input tensor, which is the output spike of some neurons. The location of True represents the moment of first spike.

参数:

spikes (torch.Tensor) -- shape=[*, T], indicates the output spikes of some neurons when \(t=0, 1, ..., T-1\).

返回:

index, shape=[*, T], the index of True represents the moment of first spike.

返回类型:

torch.Tensor

抛出:

RuntimeError -- Any tensor operation error raised by cumsum is propagated unchanged


  • 代码示例 | Example

>>> spikes = (torch.rand(size=[2, 3, 8]) >= 0.8).float()
>>> spikes
tensor([[[0., 0., 0., 0., 0., 0., 0., 0.],
 [1., 0., 0., 0., 0., 0., 1., 0.],
 [0., 1., 0., 0., 0., 1., 0., 1.]],

[[0., 0., 1., 1., 0., 0., 0., 1.],
 [1., 1., 0., 0., 1., 0., 0., 0.],
 [0., 0., 0., 1., 0., 0., 0., 0.]]])
>>> first_spike_index(spikes)
tensor([[[False, False, False, False, False, False, False, False],
 [ True, False, False, False, False, False, False, False],
 [False,  True, False, False, False, False, False, False]],

[[False, False,  True, False, False, False, False, False],
 [ True, False, False, False, False, False, False, False],
 [False, False, False,  True, False, False, False, False]]])
spikingjelly.activation_based.functional.misc.kaiming_normal_conv_linear_weight(net: Module)[源代码]#

API Language: 中文 | English


  • 中文

使用kaiming normal初始化 net 中的所有 torch.nn._ConvNdtorch.nn.Linear 的权重(不包括偏置项)。 参见 torch.nn.init.kaiming_normal_

参数:

net (torch.nn.Module) -- 任何属于 nn.Module 子类的网络

返回:

None

返回类型:

None

抛出:

RuntimeError -- 若某个权重张量不支持 Kaiming 初始化,则底层初始化异常会原样向上传播


  • English

Initialize all weights (not including bias) of torch.nn._ConvNd and torch.nn.Linear in net by the kaiming normal. See torch.nn.init.kaiming_normal_ for more details.

参数:

net (torch.nn.Module) -- Any network inherits from nn.Module

返回:

None

返回类型:

None

抛出:

RuntimeError -- Any initialization error raised by torch.nn.init.kaiming_normal_ is propagated unchanged

spikingjelly.activation_based.functional.misc.delay(x_seq: Tensor, delay_steps: int)[源代码]#

API Language: 中文 | English


  • 中文

延迟函数,可以用来延迟输入,使得 y[t] = x[t - delay_steps]。缺失的数据用0填充。

参数:
  • x_seq (torch.Tensor) -- 输入的序列,shape = [T, *]

  • delay_steps (int) -- 延迟的时间步数

返回:

延迟后的序列

返回类型:

torch.Tensor

抛出:

ValueError -- 当 delay_steps 小于 0 时,底层切片与拼接行为不再满足延迟语义,调用方应保证 delay_steps >= 0


  • English

A delay function that can delay inputs and makes y[t] = x[t - delay_steps]. The nonexistent data will be regarded as 0.

参数:
  • x_seq (torch.Tensor) -- the input sequence with shape = [T, *]

  • delay_steps (int) -- the number of delayed time-steps

返回:

the delayed sequence

返回类型:

torch.Tensor

抛出:

ValueError -- Callers are expected to provide delay_steps >= 0 to preserve the intended delay semantics


  • 代码示例 | Example

x = torch.rand([5, 2])
x[3:].zero_()
x.requires_grad = True
y = delay(x, 1)
print("x=")
print(x)
print("y=")
print(y)
y.sum().backward()
print("x.grad=")
print(x.grad)

Output:

x=
tensor([[0.1084, 0.5698],
        [0.4563, 0.3623],
        [0.0556, 0.4704],
        [0.0000, 0.0000],
        [0.0000, 0.0000]], requires_grad=True)
y=
tensor([[0.0000, 0.0000],
        [0.1084, 0.5698],
        [0.4563, 0.3623],
        [0.0556, 0.4704],
        [0.0000, 0.0000]], grad_fn=<CatBackward0>)
x.grad=
tensor([[1., 1.],
        [1., 1.],
        [1., 1.],
        [1., 1.],
        [0., 0.]])