fangwei123456 / Spike-Element-Wise-ResNet

Deep Residual Learning in Spiking Neural Networks
Mozilla Public License 2.0
140 stars 21 forks source link

RuntimeError: Trying to backward through the graph a second time #19

Closed Dazidingo closed 1 year ago

Dazidingo commented 1 year ago

我在使用SEW-ResNet做为骨干网络进行训练时,在反向传播的过程中出现了错误,错误代码如下: File "E:\SEW-RESNet\utils\utils_fit.py", line 66, in fit_one_epoch loss.backward() File "F:\Python39\lib\site-packages\torch_tensor.py", line 396, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs) File "F:\Python39\lib\site-packages\torch\autograd__init__.py", line 173, in backward Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass File "F:\Python39\lib\site-packages\torch\autograd\function.py", line 253, in apply return user_fn(self, *args) File "F:\Python39\lib\site-packages\spikingjelly\clock_driven\neuron_kernel.py", line 351, in backward h_seq, spike_seq = ctx.saved_tensors RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward. 在添加retain_graph=True后依然出现缺失值的问题

fangwei123456 commented 1 year ago

检查一下代码,在反向传播后有没有调用reset函数重置整个网络的状态

Dazidingo commented 1 year ago

我在反向传播之后加了spikingjelly.clock_driven.functional.reset_net(model)之后就能正常训练了,感谢

Dazidingo commented 1 year ago

我想知道这个reset_net()是重置了哪些内容(权重?梯度?网络结构?)看了源api文档并没有找到

fangwei123456 commented 1 year ago

重置了神经元的电压v,避免输入新样本时还用之前的v进行计算

https://spikingjelly.readthedocs.io/zh_CN/latest/activation_based/basic_concept.html#id5

Dazidingo commented 1 year ago

理解了,感谢您的解释