出现错误,请优化。错误描述:ERROR Brian 2 encountered an unexpected error. If you think this is a bug in Brian 2, please report this issue either to the discourse forum at <http://brian.discourse.group/>, or to the issue tracker at <https://github.com/brian-team/brian2/issues>. Please include this file with debug information in your report: C:\Users\mana\AppData\Local\Temp\brian_debug_pgl7l4v3.log Additionally, you can also include a copy of the script that was run, available at: C:\Users\mana\AppData\Local\Temp\brian_script_ixr169pb.py Thanks! [brian2] Traceback (most recent call last): File "E:/SNN/python/project/测试.py", line 36, in <module> synapses = Synapses(neurons, neurons, stdp_eqs, on_pre=stdp_on_pre, on_post=stdp_on_post) File "C:\Users\mana\AppData\Local\Programs\Python\Python37\lib\site-packages\brian2\synapses\synapses.py", line 822, in __init__ self._create_variables(model, user_dtype=dtype) File "C:\Users\mana\AppData\Local\Programs\Python\Python37\lib\site-packages\brian2\synapses\synapses.py", line 1175, in _create_variables check_identifier_pre_post(eq.varname) File "C:\Users\mana\AppData\Local\Programs\Python\Python37\lib\site-packages\brian2\groups\neurongroup.py", line 89, in check_identifier_pre_post raise ValueError(f"'{identifier}' cannot be used as a variable name, the " ValueError: 'A_post' cannot be used as a variable name, the '_pre' and '_post' suffixes are used to refer to pre- and post-synaptic variables in synapses.
时间: 2023-08-06 07:02:07 浏览: 285
Keil v5 Error: Encountered an improper argument 个人实测解决办法
非常抱歉遇到了错误。根据错误信息,问题出在使用了`A_post`作为变量名。这是因为在Brian2中,`_pre`和`_post`后缀用于表示突触前后的变量。
为了解决这个问题,我们可以修改STDP规则的变量名,以避免和预定义的后缀冲突。
以下是修改后的代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from brian2 import *
# 设置参数
N = 100 # 神经元数量
duration = 500 * ms # 模拟时间
dt = 0.1 * ms # 时间步长
# 定义方程和STDP规则
eqs = '''
dv/dt = (I - v) / (10*ms) : 1 (unless refractory)
I : 1
'''
# 定义STDP规则
stdp_eqs = '''
w : 1
dApre/dt = -Apre / tau_pre : 1 (event-driven)
dApost/dt = -Apost / tau_post : 1 (event-driven)
'''
stdp_on_pre = '''
v_post += w
Apre += dApre
w = clip(w + Apost, 0, w_max)
'''
stdp_on_post = '''
Apost += dApost
w = clip(w + Apre, 0, w_max)
'''
# 创建神经元组和连接权重
neurons = NeuronGroup(N, eqs, threshold='v>1', reset='v=0', refractory=5*ms)
synapses = Synapses(neurons, neurons, stdp_eqs, on_pre=stdp_on_pre, on_post=stdp_on_post)
synapses.connect()
# 随机初始化神经元电流和连接权重
neurons.I = 'rand()'
synapses.w = 'rand()'
# 记录神经元脉冲
spikes = SpikeMonitor(neurons)
# 运行仿真
run(duration)
# 提取脉冲时间和神经元索引
spike_times = spikes.t
spike_indices = spikes.i
# 将脉冲分类为5个类别
num_classes = 5
classes = [[] for _ in range(num_classes)]
for t, idx in zip(spike_times, spike_indices):
class_idx = int(idx / (N / num_classes))
classes[class_idx].append(t)
# 绘制脉冲响应结果
plt.figure(figsize=(10, 5))
for i, class_spikes in enumerate(classes):
plt.eventplot(class_spikes, color='C{}'.format(i), linewidths=0.5)
plt.xlabel('Time (ms)')
plt.ylabel('Class Index')
plt.title('Spike Response')
plt.xlim(0, duration/ms)
plt.ylim(0, num_classes-1)
plt.show()
```
请注意,我们将`dA_pre`和`dA_post`改为`dApre`和`dApost`,以避免与后缀冲突。同时,我们也将相应的变量名在STDP规则中进行了修改。
如果您还遇到任何问题,请随时向我提问。非常抱歉给您带来的不便。
阅读全文