I’m trying to optimize flow parameters using
torch.optim, and I get the following error:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor ] is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
A minimal reproducing example:
import torch from torch import tensor as tt from pyro.distributions.transforms import Planar x = tt([[-1], ]).float() u_t = tt([, ]).float() w_t = tt([[-0.2], [1.4]]).float() y = x + u_t * torch.tanh(w_t.T@x - 1) flow = Planar(2) optimizer = torch.optim.Adam(flow.parameters(), lr=0.1) x_ = x.reshape(-1); y_ = y.reshape(-1) for i in range(10): optimizer.zero_grad() y_recon = flow(x_) loss = torch.sum((y_ - y_recon)**2) loss.backward(retain_graph=True) optimizer.step()
I believe that this is being caused by something that’s inheritance-related, as if I merge the
ConditionedPlanar class and the
Planar class (while just inheriting from
TransformModule), I do not get this error. I also do not get this error if I remove
ConditionedPlanar's inheritance from
torch.distributions.Transform and make it inherit from
Is this a bug? Is there some way for me to make the error go away? Or is this desired behaviour?