Leaf variable has been moved into the graph interior error in custom RNN implementation

username: Alpay_Tekin
cooked: <p>I am implementing custom RNN. It sounds weird but it should have separate hidden states for all inputs. I will use it inside of graph convolution. As an input, it takes edge weights one by one. I am sharing the restricted case where I have only 3 inputs as an illustration.</p>
<pre><code class="lang-auto">class RNN(nn.Module):
    def __init__(self):
        super(RNN,self).__init__()
        self.weight = Parameter(torch.rand(3,1, requires_grad=True))
        self.weight_h = Parameter(torch.rand(3,1, requires_grad=True))
        self.hidden = Parameter(torch.rand(3,1,requires_grad=True))
        self.bias = Parameter(torch.rand(3,1, requires_grad=True))
        self.tanh = Tanh()
        self.iteration = 0
    
    def forward(self, x):
        self.hidden[self.iteration] = self.tanh(x*self.weight[self.iteration] + self.hidden[self.iteration].clone()*self.weight_h[self.iteration] + self.bias[self.iteration])
        self.iteration = self.iteration + 1 
        return self.hidden[self.iteration-1]

model = RNN()
mael = nn.L1Loss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
x = torch.rand(3,1)
for i in range(2):
    optimizer.zero_grad()
    out = model(x[i])
    loss = mael(out, x[i+1])
    loss.backward()
    optimizer.step()
    print(loss.item())
</code></pre>
<p>However, the code gives these error messages. Probably, the hidden state is changed in-place operation. I could not find a way to update a hidden state.</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-11-24c2beaf081c&gt; in &lt;module&gt;()
      7     out = model(x[i])
      8     loss = mael(out, x[i+1])
----&gt; 9     loss.backward()
     10     optimizer.step()
     11     print(loss.item())
/usr/local/lib/python3.6/dist-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,
--&gt; 132         allow_unreachable=True)  # allow_unreachable flag
    133 
    134 

RuntimeError: leaf variable has been moved into the graph interior
</code></pre>
<p>Tanks in advance.</p>

username: albanD
cooked: <p>Hi,</p>
<p>Yes this line: <code>self.hidden[self.iteration] = XXX</code> is modifying the hidden state inplace which leads to the error you see.<br>
Do you actually want to learn the hidden state here? because it seems that you overwrite the value that you were supposed to learn.</p>

username: Alpay_Tekin
cooked: <p>Actually what I want is creating hidden states for each edge in the graph so that each time point corresponding hidden states will be updated.</p>

username: Alpay_Tekin
cooked: <p>Is this how ben works? We should calculate current hidden state by using previous one.</p>

username: albanD
cooked: <p>Yes, but here you are modifying the original Parameter when you compute the new one. Maybe you just want it to be a temporary Tensor in your forward function that should not modify the original Parameter?</p>