本文讲解了基于snntorch库搭建脉冲神经网络的过程,解释了前向模型的原理,并进行了代码仿真(文末有完整代码可以直接运行),基于梯度下降法的脉冲神经网络训练过程则在第四节讲解。
1. LIF 神经元模型
关于LIF神经元模型的详细解释可以参考官网文档snntorch 0.7.0 --- Tutorial 2 - The Leaky Integrate-and-Fire Neuron — snntorch 0.7.0 documentation
2. 简化的LIF 神经元模型
LIF 神经元模型相当复杂,需要调整一系列超参数,这导致需要跟踪的参数非常多,如果扩展到完整的 SNN,就会变得更加繁琐。因此,我们不妨做一些简化。
在前面的教程中,我们使用欧拉法推导出了被动膜模型的以下解法:
假设
如果我们假设 t 代表序列中的时间步长而不是连续时间,那么我们可以设置 Δt=1 。为了进一步减少超参数的数量,可以假设 R=1 ,则有
在深度学习中,输入的权重因子通常是一个可学习的参数。因此引入输入X[t]:
最后可以得到:
考虑膜的复位机制,如果膜超过阈值,神经元就会发出输出尖峰,如果触发了尖峰,膜电位应该复位。重置-减弱机制的模型是:
W 是一个可学习的参数,而 Uthr 通常只是设置为 1 (尽管可以调整),因此衰减率β是唯一需要指定的超参数。
输入阶跃信号,神经元响应使用代码实现如下:
# imports
import snntorch as snn
from snntorch import spikeplot as splt
from snntorch import spikegen
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
def leaky_integrate_and_fire(mem, x, w, beta, threshold=1):
spk = (mem > threshold) # if membrane exceeds threshold, spk=1, else, 0
mem = beta * mem + w*x - spk*threshold
return spk, mem
#@title Plotting Settings
def plot_cur_mem_spk(cur, mem, spk, thr_line=False, vline=False, title=False, ylim_max1=1.25, ylim_max2=1.25):
# Generate Plots
fig, ax = plt.subplots(3, figsize=(8,6), sharex=True,
gridspec_kw = {'height_ratios': [1, 1, 0.4]})
# Plot input current
ax[0].plot(cur, c="tab:orange")
ax[0].set_ylim([0, ylim_max1])
ax[0].set_xlim([0, 200])
ax[0].set_ylabel("Input Current ($I_{in}$)")
if title:
ax[0].set_title(title)
# Plot membrane potential
ax[1].plot(mem)
ax[1].set_ylim([0, ylim_max2])
ax[1].set_ylabel("Membrane Potential ($U_{mem}$)")
if thr_line:
ax[1].axhline(y=thr_line, alpha=