在使用pytorch时报错:
RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time.
之后我loss里加了retain_graph=True也没有用
后来发现,模型中利用torch.from_numpy加载了外部数值变量,作为模型的输入:
shuzhi0=torch.from_numpy(np.array(ls_shuzhi))
然后打印查看shuzhi0这个tensor,发现他本身存在梯度:
..., grad_fn=<StackBackward>
因此我们需要消除他的梯度:
shuzhi0=torch.from_numpy(np.array(ls_shuzhi)).detach()