0
点赞
收藏
分享

微信扫一扫

RuntimeError: Trying to backward through the graph a second time

今天在测试conditional VAE的时候又遇到了这个问题,后来经过个人的分析发现了features embeddings时候比较坑的一个点,在此记录如下。

我通过:

celltype = embd(celltype_info).to(device)

获取embeddings之后通过:

torch.cat((a,celltype),1)

进行连接,之后进入神经网络开始训练的时候便遇到了如下error:

RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.

后来我print出来数据观察了下loss和celltype两者的差异,发现他们的grad,也就是梯度参数有所不同。前者是mulBackward0,后者是tocopyBackward0。我在没有统一梯度之后直接进行了backward()操作,导致后者celltype的梯度被释放掉了(因为backward默认的梯度是跟loss一致的),因此后面就遇到了这个错误。

解决方案如下:

celltype = embd(celltype_info).to(device).detach_()

不对conditional的信息进行梯度反向传播。

举报

相关推荐

0 条评论