RuntimeError of Inplace operaion during backpropagation in ENcoder Decorder model

username: Gopaluni_Rohit
cooked: <p>Hi, Can any one please help me to resolve this issue.<br>
i have created a simple <strong>CRNN-model(an encoder decoder)</strong> which is having trouble in backpropagaition.<br>
class OCR_EncoderDecoder(nn.Module):</p>
<pre><code>def __init__(self,hidden_size1,hidden_size2,output_size,verbose=False):
    super(OCR_EncoderDecoder, self).__init__()
    
    self.hidden_size1 = hidden_size1
    self.hidden_size2 = hidden_size2
    self.output_size = output_size
    
    self.encoder_cnn_cell = nn.Sequential(
    nn.Conv2d(3,28,3,padding=(0,0), stride=1),#----&gt; layer-1
    nn.BatchNorm2d(28),
    nn.ReLU(),
    nn.MaxPool2d(2,2),
    nn.Conv2d(28,32,3,padding=(0,0)),#-----&gt; Layer-2
    nn.BatchNorm2d(32),
    nn.ReLU(),
    nn.MaxPool2d(2,2),
    nn.Conv2d(32,64,3,padding=(0,0)),#-----&gt; Layer-3
    nn.Conv2d(64,128,3,padding=(1,1)),#-----&gt; Layer-4
    nn.BatchNorm2d(128),
    nn.ReLU(),
    nn.MaxPool2d(2,2),
    nn.Conv2d(128,256,3,padding=(1,1)),#-----&gt; Layer-5
    nn.BatchNorm2d(256),
    nn.ReLU()
    )
    
    self.Conv_Linear = nn.Sequential(
        nn.Linear(1536,self.hidden_size1))
    
    self.decoder_rnn_cell1 = nn.GRU(self.hidden_size1,self.hidden_size2)
    self.decoder_rnn_cell2 = nn.GRU(self.hidden_size2,self.output_size)
    
    self.Rnn_Linear = nn.Sequential(
        nn.Linear(self.output_size, self.output_size),
        nn.LogSoftmax(2)) 
    
    #self.softmax = nn.LogSoftmax(dim=2)
    
    self.verbose = verbose        
    
def forward(self,batch):
    
    # encoder
    batch = self.encoder_cnn_cell(batch)
    batch = batch.permute(0,3,2,1)
    
    

    # Decoder
    batch = batch.reshape(batch.size(0),batch.size(1),-1)
    batch = self.Conv_Linear(batch)
    batch,_ = self.decoder_rnn_cell1(batch)
    batch,_ = self.decoder_rnn_cell2(batch)
    batch = self.Rnn_Linear(batch)
    #batch = self.softmax(batch)
    

    return batch
</code></pre>
<p>U = OCR_EncoderDecoder(128,256,129,False)<br>
B = U(u)<br>
B.shape <span class="hashtag">#torch</span>.Size([2, 26, 129])<br>
LOSS = {}<br>
Loss = 0<br>
for k in range(25):<br>
opt.zero_grad()<br>
for i in range(B.size(0)):<br>
input_ = B[i].view(B.size(1),1,B.size(2))<br>
op = gt_rep(v[i],hindi_alpha2index)<br>
op = op.view(1,-1)<br>
input_length = torch.full(size=(1,), fill_value= input_.size(0), dtype=torch.long)<br>
target_length = torch.randint(low=1, high=op.size(1), size=(1,), dtype=torch.long)<br>
print(input_.shape)<br>
print(op.shape)<br>
print(input_length,target_length)<br>
loss = loss_fn(input_,op,input_length,target_length)<br>
loss.backward(retain_graph=True)<br>
Loss += loss<br>
print(k)<br>
LOSS[k] = Loss/2<br>
opt.step()</p>
<p>i have used a CTC loss and trying to backpropagate, however says this error :<br>
<strong>RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [129, 129]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).</strong></p>
<p>Kindly help in solving this issue.</p>