RuntimeError: Trying to backward through the graph a second time, but the buffers have already been

报错信息

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.

方法一

网上大部分的方法是这样的,在backward()函数中添加参数retain_graph=True:

loss.backward(retain_graph=True)

但是我试过后还是会报出其他的问题。

方法二

我出现这个问题是发生在自定义循环神经网络时发生的,即将网络输出作为下一时刻的输入时出现的问题。

报错代码:

for idx, (u, x1, x2, x1_next, x2_next) in enumerate(train_loader):
	if idx == 0:
    	x1_input = x1
        x2_input = x2
	output = net(u.to(device),
	             x1_input.to(device),
	             x2_input.to(device))
	x1_input = output[:, 0]
	x2_input = output[:, 1]

解决方法是将numpy.array类型作为中间变量,解决方法如下:

for idx, (u, x1, x2, x1_next, x2_next) in enumerate(train_loader):
    if idx == 0:
        x1_input = x1.data.numpy()
        x2_input = x2.data.numpy()
    output = net(u.to(device),
                 torch.from_numpy(x1_input).float().to(device),
                 torch.from_numpy(x2_input).float().to(device))
	x1_input = output[:, 0].data.cpu().numpy()
	x2_input = output[:, 1].data.cpu().numpy()

循环之前还应该有个x1_input 、x2_input的初始化,否则会报错(numpy.zeros)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值