我是通过这两个文章接触到Leak Integrate-and-Fire模型的:
- How the Eon Team Produced a Virtual Embodied Fly · Eon
- A connectomics milestone: Mapping the complete male fruit fly brain
LIF模型主要运用于“模拟大脑”的脉冲神经网络中,这旨在模拟神经元在受到电流刺激后,发出的神经脉冲。这有别于目前主流LLM大模型中的transformer等神经网络。
参考视频:
Leak Integrate-and-FIre Model的基本方程是:
基于这个方程,可以画出这种图像:
| 输入电流 | 输出脉冲 |
|---|---|
![]() | ![]() |
![]() | ![]() |
公式推导
在LIF模型中,一个神经元被抽象成了一个点,没有空间区域。
神经元内部可以被充电,充电到到达一定阈值后会被reset,重新回到resting potential。
生物学上,一个神经元有复杂的voltage-gated channel的控制,在LIF中,这一系列复杂的行为被抽象成了reset和refractory这种规则。
假设细胞膜是一个RC circuit(电阻-电容并联电路),那么:
- Capacitor () - lipid bilayer储存电荷的能力
- Resistor () - ion channels的leak conductance
- Battery () - 由于离子浓度差产生的resting potential
- Input current ():外部注入的电流
根据Kirchhoff’s Current Law,流入节点的总电流等于流出的总电流:
在这里流入节点的电流源自通过Capacitor的电流和通过Resistor的漏电流:

-
通过Capacitor的电流 :
前置知识:
-
电容的定义:
-
电流是电荷流动的速率:
推导:
-
-
通过Resistor的漏电流 :
前置知识:
- Ohm’s Law:
推导:
将和带入Kirchhoff’s Current Law:
定义membrane time constant:
得到LIF的core equation核心方程。
关于Membrane time constant:
- 根据观察core equation的右侧,单位为“电压”,等号左侧的单位为”电压/时间”,所以的单位必须是时间
决定了神经元膜电位对输入电流做出反应的速度;从计算机的角度来理解,就是一个“滤波器”,当越大,越对噪声不敏感。
离散化
将连续的core equation离散化有助于通过计算机编程的方式实现:
所以:
使用有限差分近似derivative导数:
带入:
此时就可以通过编程实现了:
-
是当前输入电流的值
-
是当前电压的值
-
是下一次迭代中电压的值
-
其余的全是常数
Python实现
我在这边用uv管理python项目,然后在vscode中使用jupyter notebook写代码。
安装uv:uv - Installation
创建项目:
$ uv init simple-LIF$ cd simple-LIF$ uv add jupyter matplotlib$ code LIF.ipynb选择jupyter notebook的kernel时选择当前环境中的python interpreter:

之后在jupyter notebook里添加代码块写代码即可。
引用用于画图的包:
import matplotlib.pyplot as plt定义常数(单位和默认值的选择很重要):
# ConstantsDELTA_T = 0.1 # msTAU_M = 10.0 # msV_REST = -70.0 # mVV_RESET = -75.0 # mVV_TH = -50.0 # mVR = 10.0 # MOhmREFRACTORY_PERIOD = 2.0 # ms生成input current的数据:
# Generate the input current dataTOTAL_TIME = 200 # total time: 200msCURRENT_SIZE = 2.5 # nAQUARTER_LEN = int(TOTAL_TIME / DELTA_T / 4)input_current = ( [0] * QUARTER_LEN + [CURRENT_SIZE] * QUARTER_LEN + [CURRENT_SIZE * 2] * QUARTER_LEN + [0] * QUARTER_LEN)DATA_LENGTH = len(input_current)可视化列表输入电流:
fig, input_current_ax = plt.subplots()input_current_ax.plot(range(DATA_LENGTH), input_current)plt.show()
计算membrane potential(细胞膜上的电压):
# Generate VV = V_REST # current potential on the memrbanemembrane_potential = []refractory = 0for I in input_current: # iterate the input current in each time (delta_t) if refractory > 0: # handle the refractory period refractory -= DELTA_T # refractory period's count down I = 0 # ignore the input current
# apply the core equation V = V + DELTA_T / TAU_M * ((V_REST - V) + R * I ) if V > V_TH: # enter refractory period V = V_RESET refractory = REFRACTORY_PERIOD membrane_potential.append(V) # record the membrane potential画图:
fig, membrane_potential_ax = plt.subplots()membrane_potential_ax.plot(range(DATA_LENGTH), membrane_potential)plt.show()
使用手柄输入电流
添加pygame依赖来读取手柄输入:
$ uv add pygame$ mkdir images # 用来存放输出的图片获取手柄按键映射:
$ code button_mapping.pyimport pygame, syspygame.init()pygame.joystick.init()
if pygame.joystick.get_count() == 0: sys.exit()joy = pygame.joystick.Joystick(0)joy.init()print(f"Name: {joy.get_name()}")print(f"Buttons: {joy.get_numbuttons()}, Axes: {joy.get_numaxes()}")
while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.JOYBUTTONDOWN: print(f"Button {event.button} pressed") if event.type == pygame.JOYAXISMOTION and abs(event.value) > 0.3: print(f"Axis {event.axis} moved -> {event.value:.3f}")然后连接手柄,通过uv run button_mapping.py运行程序,来确认自己手柄的物理按键对应到pygame中的几号按键。
例如:我的A键是0,用来输入的扳机是4。之后在使用的时候我就用这两个键来录制输入电流。
创建用来读取输入的脚本:
$ code add joystick_as_LIF_input.pyimport matplotlib.pyplot as pltimport pygame, time
def init_joystick(): if pygame.joystick.get_count() == 0: print("No joystick is found, QUIT") exit()
joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
for joystick in joysticks: joystick.init() print(f"{joystick.get_name()} is connected!")
joystick = joysticks[0] print(f"Using {joystick.get_name()}")
return joystick
pygame.init()pygame.joystick.init()
# ConstantsDELTA_T = 0.1 # msTAU_M = 10.0 # msV_REST = -70.0 # mVV_RESET = -75.0 # mVV_TH = -50.0 # mVR = 10.0 # MOhmREFRACTORY_PERIOD = 2.0 # ms
# Generate the input current data
# total time: 200msTOTAL_TIME = 200CURRENT_SIZE = 2.5 # nADATA_LENGTH = int(TOTAL_TIME / DELTA_T)
def generate_default_current_sample(): input_current = [] for _ in range(int(TOTAL_TIME / DELTA_T / 4)): input_current.append(0) for _ in range(int(TOTAL_TIME / DELTA_T / 4)): input_current.append(CURRENT_SIZE) for _ in range(int(TOTAL_TIME / DELTA_T / 4)): input_current.append(CURRENT_SIZE* 2) for _ in range(int(TOTAL_TIME / DELTA_T / 4)): input_current.append(0) return input_current
def generate_current_from_joystick(): def calibrate_axis(val): calibrated = (val + 1.0) / 2.0 if calibrated < 0.05: return 0 return calibrated
joystick = init_joystick() print("Press A to start the recording, and press A again to end the recording") while True: pygame.event.pump() if joystick.get_button(0): break while True: pygame.event.pump() if not joystick.get_button(0): break print("Start recording...")
input_current = [] count = 0 while True: pygame.event.pump() right_trigger = joystick.get_axis(4) right_trigger = calibrate_axis(right_trigger) I = 5 * right_trigger input_current.append(I) count += 1 if joystick.get_button(0): break time.sleep(0.005)
print(f"End recording. record length: {count * DELTA_T}")
return input_current
# input_current = generate_default_current_sample()input_current = generate_current_from_joystick()DATA_LENGTH = len(input_current)
fig, input_current_ax = plt.subplots()input_current_ax.plot(range(DATA_LENGTH), input_current)plt.savefig("images/input_current.png")
# Define the method of forwarding Vdef forward(V): return V + DELTA_T / TAU_M * ((V_REST - V) + R * I )
# Generate VV = V_REST # current potentialmembrane_potential = []refractory = 0for I in input_current: if refractory > 0: refractory -= DELTA_T I = 0 # ignore the input current
V = forward(V) if V > V_TH: V = V_RESET refractory = REFRACTORY_PERIOD membrane_potential.append(V)
fig, membrane_potential_ax = plt.subplots()membrane_potential_ax.plot(range(DATA_LENGTH), membrane_potential)plt.savefig("images/membrane_potential.png")之后通过uv run joystick_as_LIF_input.py运行脚本,按A开始录制,再按一次A结束录制。
结果参考:





