Hi all, I want to parametrize two Gaussian distributions, but their parameters are related. But it seems like that the variable will be freed during the training. What can I do to fix this problem?
Here is the minimum code. Thank you!
import torch.nn as nn
import torch.distributions as D
class GaussianModel(nn.Module):
def __init__(self):
super(GaussianModel, self).__init__()
self.v1 = nn.Parameter(torch.zeros(1))
self.v2 = self.v1*2
self.g1=D.Normal(torch.tensor([4.0]), self.v1)
self.g2=D.Normal(torch.tensor([4.0]), self.v2)
def forward(self,x):
return torch.mean(self.g1.log_prob(x)+self.g2.log_prob(x))
model=GaussianModel()
opt=torch.optim.SGD(params=model.parameters(), lr=1e-3)
for i in range(10):
opt.zero_grad()
x=torch.randn(10)
loss=model(x)
loss.backward()
opt.step()
And this is the message from terminal:
RuntimeErrorTraceback (most recent call last)
<ipython-input-92-46e3175cfeff> in <module>
3 x=torch.randn(10)
4 loss=model(x)
----> 5 loss.backward()
6 opt.step()
7 # model.p1.detach()
~/.virtualenvs/ddp-pytorch/lib/python3.6/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
219 retain_graph=retain_graph,
220 create_graph=create_graph)
--> 221 torch.autograd.backward(self, gradient, retain_graph, create_graph)
222
223 def register_hook(self, hook):
~/.virtualenvs/ddp-pytorch/lib/python3.6/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
130 Variable._execution_engine.run_backward(
131 tensors, grad_tensors_, retain_graph, create_graph,
--> 132 allow_unreachable=True) # allow_unreachable flag
133
134
RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time.