TabNetRegressorをRMSEで学習

torch.autograd.backward()時に、
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor []], which is output 0 of SqrtBackward, is at version 1; expected version 0 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

のエラーが生じた。これはinplace操作が行われたときに出る。

 

discuss.pytorch.org


これらのバージョンは、Tensorがインプレースで変更された回数を追跡します。変更するたびにバージョンが上がります。
手動でバージョンを変更することはできません。間違ったバージョンでエラーが発生した場合は、バージョンがぶつからないようにインプレース操作を削除する必要があります。

 

lossをいったんcopyして、それを返すとエラーが生じなくなった。

 

class RMSELoss(torch.nn.Module):

    def __init__(self):
        super(RMSELoss, self).__init__()

    def forward(self, x, y):
        criterion = nn.MSELoss()
        loss = torch.sqrt(criterion(x, y))
        return loss