对应地址【https://spikingjelly.readthedocs.io/zh-cn/0.0.0.0.14/activation_based/surrogate.html#】
【在 神经元 中我们已经提到过,描述神经元放电过程的
,使用了一个Heaviside阶跃函数:
按照定义,其导数为冲激函数:
直接使用冲激函数进行梯度下降,显然会使得网络的训练及其不稳定。为了解决这一问题,各种梯度替代法(the surrogate gradient method)被相继提出,参见此综述 Surrogate Gradient Learning in Spiking Neural Networks。】
因为反向传播的时候不能导,所以用近似来代替
import torch
from spikingjelly.activation_based import surrogate
from matplotlib import pyplot as plt
sg = surrogate.Sigmoid(alpha=4.)
x = torch.rand([8]) - 0.5
x.requires_grad = True
y = sg(x)
y.sum().backward()
print(f'x={x}')
print(f'y={y}')
print(f'x.grad={x.grad}')
plt.figure(figsize=(10, 6))
plt.subplot(3, 1, 1)
plt.plot(x.detach().numpy(), 'r-', label='x')
plt.title('x values')
plt.legend()
plt.subplot(3, 1, 2)
plt.plot(y.detach().numpy(), 'g-', label='y')
plt.title('y values')
plt.legend()
plt.subplot(3, 1, 3)
plt.plot(x.grad.detach().numpy(), 'b-', label='x.grad')
plt.title('Gradient of x')
plt.legend()
plt.tight_layout()
plt.show()
将图片画出来
【SpikingJelly中替代函数的形状参数,默认情况下是使得替代函数梯度最大值为1,这在一定程度上可以避免梯度累乘导致的梯度爆炸问题。】