【发布时间】:2021-12-30 14:11:38
【问题描述】:
我有一个输出output 的神经网络。我想在损失和反向传播发生之前转换output。
这是我的通用代码:
with torch.set_grad_enabled(training):
outputs = net(x_batch[:, 0], x_batch[:, 1]) # the prediction of the NN
# My issue is here:
outputs = transform_torch(outputs)
loss = my_loss(outputs, y_batch)
if training:
scheduler.step()
loss.backward()
optimizer.step()
我有一个转换函数,我将输出通过:
def transform_torch(predictions):
torch_dimensions = predictions.size()
torch_grad = predictions.grad_fn
cuda0 = torch.device('cuda:0')
new_tensor = torch.ones(torch_dimensions, dtype=torch.float64, device=cuda0, requires_grad=True)
for i in range(int(len(predictions))):
a = predictions[i]
# with torch.no_grad(): # Note: no training happens if this line is kept in
new_tensor[i] = torch.flip(torch.cumsum(torch.flip(a, dims = [0]), dim = 0), dims = [0])
return new_tensor
我的问题是在倒数第二行出现错误:
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.
有什么建议吗?我已经尝试过使用“with torch.no_grad():”(已评论),但这会导致训练效果很差,而且我相信梯度在转换函数后无法正确反向传播。
谢谢!
【问题讨论】:
-
转换中的
a是什么? -
我修复了它 - a = predictions[i]。我在移除 cmets 时不小心把它遗漏了。感谢您的澄清。
标签: python deep-learning neural-network pytorch backpropagation