m = train_i[np.random.randint(low=0, high=450)] # 随机抽取一个数据 real_class = y_train[m].item() # 真实的类别 for i in range(class_num * per_class_neuron_num): # 分别送入dec_layer的每一个class neural_class = i // per_class_neuron_num # 表示这个神经元应该对哪一类响应1 neural_seq = i % per_class_neuron_num Gi = F.sigmoid(B[i]) dec_layer[i].calculate_membrane_potentials(W[i] * Gi, t_spike[m]) t_max = dec_layer[i].v.argmax() # 训练第i个分类器 if neural_class == real_class: # 应该放电 if dec_layer[i].v[t_max] < dec_layer[i].v_thr: v_error = node.psp_kernel(t_max - t_spike[m], dec_layer[i].v0, dec_layer[i].tau, dec_layer[i].tau_s) dW = Gi * v_error dB = W[i] * v_error * Gi * (1 - Gi) W[i] += learn_rate * dW B[i] += learn_rate * dB else: # 不应该放电 if dec_layer[i].v[t_max] > dec_layer[i].v_thr: v_error = node.psp_kernel(t_max - t_spike[m], dec_layer[i].v0, dec_layer[i].tau, dec_layer[i].tau_s) dW = Gi * v_error dB = W[i] * v_error * Gi * (1 - Gi) W[i] -= learn_rate * dW
while 1: m = train_i[np.random.randint(low=0, high=450)] # 随机抽取一个数据 real_class = y_train[m].item() # 真实的类别 for i in range(class_num * per_class_neuron_num): # 分别送入dec_layer的每一个class neural_class = i // per_class_neuron_num # 表示这个神经元应该对哪一类响应1 neural_seq = i % per_class_neuron_num dec_layer[i].calculate_membrane_potentials(W[i], t_spike[m]) t_max = dec_layer[i].v.argmax() # 训练第i个分类器 if neural_class == real_class: # 应该放电 if dec_layer[i].v[t_max] < dec_layer[i].v_thr: W[i] += learn_rate * node.psp_kernel( t_max - t_spike[m], dec_layer[i].v0, dec_layer[i].tau, dec_layer[i].tau_s) else: # 不应该放电 if dec_layer[i].v[t_max] > dec_layer[i].v_thr: W[i] -= learn_rate * node.psp_kernel( t_max - t_spike[m], dec_layer[i].v0, dec_layer[i].tau, dec_layer[i].tau_s) # 计算第i个分类器的错误率 if train_times % 128 == 0: # 测试一次 error_times = 0 for m in test_i: real_class = y_train[m].item() # 真实的类别 # 比较每一个分类器的输出结果