spikingjelly学习-梯度替代

对应地址【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,这在一定程度上可以避免梯度累乘导致的梯度爆炸问题。】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_44781508

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值