fangwei123456 / spikingjelly

SpikingJelly is an open-source deep learning framework for Spiking Neural Network (SNN) based on PyTorch.
https://spikingjelly.readthedocs.io
Other
1.21k stars 233 forks source link

在用AttributeMonitor记录神经元的电压值的时候,已经设置了m.store_v_seq = True,但还是只记录一个时间的电压 #537

Open thebug-dev opened 2 months ago

thebug-dev commented 2 months ago

我的代码如下: model.eval() for m in model.fc.modules(): if isinstance(m, neuron.LIFNode): m.store_v_seq = True print(model.fc[-1].store_v_seq) monitor_v = monitor.AttributeMonitor(attribute_name='v', pre_forward=False, net=model.fc[-1], instance=neuron.LIFNode) monitor_o = monitor.OutputMonitor(net=model.fc[-1], instance=neuron.LIFNode) with torch.no_grad():
data_iter = iter(test_loader)
data, label = next(data_iter)

只取批次中的第一个样本

    data = data[0]
    label = label[0]
    print(f'Label of the first sample: {label.item()}')
    data = data.to(device)
    out_fr = model(data)
    out_spikes_counter_frequency = out_fr.cpu().numpy()
    print(f'Firing rate: {out_spikes_counter_frequency}')
    v_list = monitor_v.records[0].squeeze()
    s_list = monitor_o.records[0].squeeze()
    print(monitor_v.records, monitor_o.records, v_list.shape, s_list.shape)
    numpy_array_v_list = v_list.detach().cpu().numpy()
    numpy_array_s_list = s_list.detach().cpu().numpy()
    visualizing.plot_1d_spikes(spikes=np.asarray(numpy_array_s_list), title='Membrane Potentials', xlabel='Simulating Step',ylabel='Neuron Index', dpi=300)
    numpy_array_s_list = numpy_array_s_list.flatten()
    visualizing.plot_one_neuron_v_s(numpy_array_v_list, numpy_array_s_list, v_threshold=1, v_reset=0, figsize=(12, 8), dpi=300)
    plt.show()
    functional.reset_net(model)
    del monitor_v,monitor_o

请问作者大大,已经设置了m.store_v_seq = True,但v监视器输出如下: [tensor([[0.6333, 0.0000, 0.7034, 0.6480]], device='cuda:0')],只有一个时间步的,而s监视器正常有32个时间步

thebug-dev commented 2 months ago

之前的代码使用了文字文本,现改成代码文本方便查看: image 输出如下: image 形状为torch.Size([4]),并不是预期的(32,4)

thebug-dev commented 2 months ago

model.fc如下: image

thebug-dev commented 2 months ago

已经修复了,我把monitor_v和monitor_o的两行代码改成: image 就好了,很神奇,但原理未知