[英]How to transform output of neural network and still train?
我有一个输出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()
我有一个转换 function 我把我的 output 通过:
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():”(已评论),但这会导致训练效果很差,而且我相信在变换 function 后梯度不会正确反向传播。
谢谢!
该错误对于问题是非常正确的 - 当您使用requires_grad = True
创建一个新张量时,您会在图中创建一个叶节点(就像模型的参数一样)并且不允许对其进行就地操作。
解决方法很简单,不需要提前创建new_tensor
。 它不应该是叶节点; 即时创建它
new_tensor = [ ]
for i in range(int(len(predictions))):
a = predictions[i]
new_tensor.append(torch.flip(torch.cumsum(torch.flip(a, ...), ...), ...))
new_tensor = torch.stack(new_tensor, 0)
这个new_tensor
将从predictions
中继承所有属性,如dtype
、 device
,并且已经具有require_grad = True
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.