I writed a net about seq2seq .It have two class, one is encoder,the other is decoder
The loss backpropagation Two times , the net is ok, but third time,the loss backpropagation  occur a error
This is the error:
RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
This is my code:
for epoch in range(20):
for k in range(6250):
    if (k+1)%5==0:
        en_input=to_cuda(total_in[:,k-4:k+1,:])
        target_out=to_cuda(total_out[:,k-4:k+1,:])
        
        #==========encoder============================
        output,hidden_out=encoder(en_input) #### this is my encoder
        de_input=output[:,-1,:]
        
        #==========decoder============================    
        for i in range(5):
          
            de_input=to_cuda((de_input.view(3,1,25)))
        
            output,hidden_out=decoder(de_input)#####  This is my decoder
            
            out=torch.cat((out,output),1)
            
        out=out[:,1:6,:]
        
        #==========loss backward======================
        loss=criterion(out,target_out)
        optimizer_en.zero_grad()
        optimizer_de.zero_grad()
        print(loss)    ##  first time:loss 0.4317    second time loss 0.4834
        loss.backward()
        optimizer_en.step()
        optimizer_de.step()