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)[源代码]#
-
中文
对于用来分类的网络,为输出层神经元的电压阈值设置一定的裕量,以获得更好的分类性能。
类别总数为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
threshold1and the remaining will be set tothreshold0.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_thresholdis propagated unchanged
- spikingjelly.activation_based.functional.misc.redundant_one_hot(labels: Tensor, num_classes: int, n: int)[源代码]#
-
中文
对数据进行冗余的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- 返回类型:
- 抛出:
RuntimeError -- 若
labels中存在不在[0, num_classes - 1]范围内的值,则F.one_hot会抛出异常
English
Redundant one-hot encoding for data. Each class is encoded to
n1's and(num_classes - 1) * n0's- 参数:
labels (torch.Tensor) -- Tensor of shape=[batch_size],
batch_sizelabelsnum_classes (int) -- The total number of classes.
n (int) -- The encoding length for each class.
- 返回:
Tensor of shape
[batch_size, num_classes * n]- 返回类型:
- 抛出:
RuntimeError --
F.one_hotraises iflabelscontains 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)[源代码]#
-
中文
输入若干个神经元的输出脉冲,返回一个与输入相同shape的
bool类型的index。 index为True的位置,表示该神经元首次释放脉冲的时刻。- 参数:
spikes (torch.Tensor) --
shape=[*, T],表示任意个神经元在 \(t=0, 1, ..., T-1\) , 共T个时刻的输出脉冲- 返回:
index,shape=[*, T],为True的位置表示该神经元首次释放脉冲的时刻- 返回类型:
- 抛出:
RuntimeError -- 若
spikes不是可进行cumsum的张量类型,则底层张量操作异常会原样向上传播
English
Return an
indextensor of the same shape of input tensor, which is the output spike of some neurons. The location ofTruerepresents 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 ofTruerepresents the moment of first spike.- 返回类型:
- 抛出:
RuntimeError -- Any tensor operation error raised by
cumsumis 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)[源代码]#
-
中文
使用kaiming normal初始化
net中的所有torch.nn._ConvNd和torch.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._ConvNdandtorch.nn.Linearinnetby the kaiming normal. Seetorch.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)[源代码]#
-
中文
延迟函数,可以用来延迟输入,使得
y[t] = x[t - delay_steps]。缺失的数据用0填充。- 参数:
x_seq (torch.Tensor) -- 输入的序列,
shape = [T, *]delay_steps (int) -- 延迟的时间步数
- 返回:
延迟后的序列
- 返回类型:
- 抛出:
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
- 返回类型:
- 抛出:
ValueError -- Callers are expected to provide
delay_steps >= 0to 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.]])