[英]How to transform output of NN, while still being able to 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()
遵循如何轉換神經網絡的 output 中的建議並繼續訓練? ,我有一個轉換 function 我把我的 output 通過:
def transform_torch(predictions):
new_tensor = []
for i in range(int(len(predictions))):
arr = predictions[i]
a = arr.clone().detach()
# My transformation, which results in a positive first element, and the other elements represent decrements of the first positive element.
b = torch.negative(a)
b[0] = abs(b[0])
new_tensor.append(torch.cumsum(b, dim = 0))
# new_tensor[i].requires_grad = True
new_tensor = torch.stack(new_tensor, 0)
return new_tensor
注意:除了clone().detach()
之外,我還嘗試了Pytorch 首選方法中描述的方法來復制張量,得到類似的結果。
我的問題是,這個被轉換的張量實際上並沒有發生任何訓練。
如果我嘗試就地修改張量(例如直接修改arr
),那么 Torch 會抱怨我無法就地修改帶有漸變的張量。
有什么建議么?
對predictions
調用detach
會停止向 model 的梯度傳播。 之后您所做的任何事情都不會改變您的參數。
如何修改代碼以避免這種情況:
def transform_torch(predictions):
b = torch.cat([predictions[:, :1, ...].abs(), -predictions[:, 1:, ...]], dim=1)
new_tensor = torch.cumsum(b, dim=1)
return new_tensor
用這樣的東西從張量中提取 grad 怎么樣
grad = output.grad
並在轉換后將相同的梯度分配給新張量
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.