Please help, got this RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

username: Stihl
cooked: <p>Can’t find the problem place.I cant find inplace problem operations. RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1, 5]], which is output 0 of UnsqueezeBackward0, is at version 321; expected version 316 instead<br>
here is the forward code:<br>
def forward(self, x):</p>
<pre><code>    if len(x.shape) == 3:

        x1 = x.view(1, self.channels, self.bars, self.input_dim) 

        x2 = self.norm(x1)

    else:

        x2 = self.norm(x)

    save = torch.zeros(x2.size()[0], self.act_size).to(device)

    out = torch.zeros(self.channels).to(device)

    for b in range(x2.size()[0]):

        y0 = x2[b][0].view(1, self.bars, self.input_dim)

        y1 = x2[b][1].view(1, self.bars, self.input_dim)

        y2 = x2[b][2].view(1, self.bars, self.input_dim)

        y3 = x2[b][3].view(1, self.bars, self.input_dim)

        y4 = x2[b][4].view(1, self.bars, self.input_dim)

        lstm_out_0, hid_0 = self.lstm_0(y0)

        lstm_out_1, hid_1 = self.lstm_1(y1)

        lstm_out_2, hid_2 = self.lstm_2(y2)

        lstm_out_3, hid_3 = self.lstm_3(y3)

        lstm_out_4, hid_4 = self.lstm_4(y4)

        out[0] = hid_0[0][-1]

        out[1] = hid_1[0][-1]

        out[2] = hid_2[0][-1]

        out[3] = hid_3[0][-1]

        out[4] = hid_4[0][-1]

        save[b] = self.dense(out)

    return save</code></pre>

username: Stihl
cooked: <p>/pytorch/torch/csrc/autograd/python_anomaly_mode.cpp:57: UserWarning: Traceback of forward call that caused the error:<br>
File “/usr/lib/python3.6/runpy.py”, line 193, in _run_module_as_main<br>
“<strong>main</strong>”, mod_spec)<br>
File “/usr/lib/python3.6/runpy.py”, line 85, in _run_code<br>
exec(code, run_globals)<br>
File “/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py”, line 16, in <br>
app.launch_new_instance()<br>
File “/usr/local/lib/python3.6/dist-packages/traitlets/config/application.py”, line 664, in launch_instance<br>
app.start()<br>
File “/usr/local/lib/python3.6/dist-packages/ipykernel/kernelapp.py”, line 499, in start<br>
self.io_loop.start()<br>
File “/usr/local/lib/python3.6/dist-packages/tornado/platform/asyncio.py”, line 132, in start<br>
self.asyncio_loop.run_forever()<br>
File “/usr/lib/python3.6/asyncio/base_events.py”, line 438, in run_forever<br>
self._run_once()<br>
File “/usr/lib/python3.6/asyncio/base_events.py”, line 1451, in _run_once<br>
handle._run()<br>
File “/usr/lib/python3.6/asyncio/events.py”, line 145, in _run<br>
self._callback(*self._args)<br>
File “/usr/local/lib/python3.6/dist-packages/tornado/platform/asyncio.py”, line 122, in _handle_events<br>
handler_func(fileobj, events)<br>
File “/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py”, line 300, in null_wrapper<br>
return fn(*args, **kwargs)<br>
File “/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py”, line 462, in _handle_events<br>
self._handle_recv()<br>
File “/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py”, line 492, in _handle_recv<br>
self._run_callback(callback, msg)<br>
File “/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py”, line 444, in _run_callback<br>
callback(*args, **kwargs)<br>
File “/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py”, line 300, in null_wrapper<br>
return fn(*args, **kwargs)<br>
File “/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py”, line 283, in dispatcher<br>
return self.dispatch_shell(stream, msg)<br>
File “/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py”, line 233, in dispatch_shell<br>
handler(stream, idents, msg)<br>
File “/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py”, line 399, in execute_request<br>
user_expressions, allow_stdin)<br>
File “/usr/local/lib/python3.6/dist-packages/ipykernel/ipkernel.py”, line 208, in do_execute<br>
res = shell.run_cell(code, store_history=store_history, silent=silent)<br>
File “/usr/local/lib/python3.6/dist-packages/ipykernel/zmqshell.py”, line 537, in run_cell<br>
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)<br>
File “/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py”, line 2718, in run_cell<br>
interactivity=interactivity, compiler=compiler, result=result)<br>
File “/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py”, line 2822, in run_ast_nodes<br>
if self.run_code(code, result):<br>
File “/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py”, line 2882, in run_code<br>
exec(code_obj, self.user_global_ns, self.user_ns)<br>
File “”, line 1223, in <br>
value_v = net_crt(states_v)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py”, line 541, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “”, line 998, in forward<br>
save[b] = self.dense(out)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py”, line 541, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py”, line 92, in forward<br>
input = module(input)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py”, line 541, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/linear.py”, line 87, in forward<br>
return F.linear(input, self.weight, self.bias)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py”, line 1372, in linear<br>
output = input.matmul(weight.t())</p>

username: albanD
cooked: <aside class="quote no-group" data-username="Stihl" data-post="1" data-topic="94216">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/4af34b/48.png" class="avatar"> Stihl:</div>
<blockquote>
<p><code>out[4] = hid_4[0][-1]</code></p>
</blockquote>
</aside>
<p>I guess each of these calls are actually modifying <code>out</code> inplace. But it’s value is needed by the <code>dense(out)</code> call?<br>
Can you try not to change out inplace and just <code>torch.cat()</code> your hidden layers together?</p>

username: Stihl
cooked: <p>Thank you <img src="https://discuss.pytorch.org/images/emoji/apple/v.png?v=9" title=":v:" class="emoji" alt=":v:"><br>
It works now</p>