Ascending CPU Memory during training of LSTM

MuMorning: <p>I am faced with memory problem when I use pytorch to implement certain network. To locate the problem, I check the memory of a very simple single LSTM network and found the memory for each epoch would increase for early turns, while for the same LSTM network in TensorFlow, the memory keeps the same (and the similar size of the first epoch’s memory by LSTM).<br>
Moreover, the problem would become more serious with the grow of hidden size.<br>
Could someone help me find out what happened? Thanks a lot!</p>
<p>The assigned pictures shows the detail.<br>
By the way, no worry for the difference in loss, cuz I didn’t average for all batches in the TensorFlow implementation.</p>
<p>Thanks for your attention again!</p>
<p>Code for training step:</p>
<pre><code class="lang-auto">for epoch in range(num_epochs):
            start = time.time()
            i = 0
            loss_sum = 0
            total_round = 0
            while (i &lt; self.train_size):
                self.rnn.train()
                self.optim.zero_grad()
                batch_end = i + batch_size
                if (batch_end &gt;= self.train_size):
                    batch_end = self.train_size
                var_x = self.to_variable(x_train[i: batch_end])
                var_y = self.to_variable(y_train[i: batch_end])
                var_y_seq = self.to_variable(y_seq_train[i: batch_end])
                if var_x.dim() == 2:
                    var_x = var_x.unsqueeze(2)
    
                y_res = self.rnn(var_x)
                var_y = var_y.view(-1,1)
                loss = self.loss_func(y_res, var_y)
                loss.backward()
                self.optim.step()
                loss_sum += loss.detach().numpy()
                i = batch_end
                total_round += 1
            end = time.time()
            print('epoch [{}] finished, the average train loss is {}, with time: {}'.format(epoch, loss_sum/total_round, end-start))
            
            memory_usage()
            
            train_loss_list.append(loss_sum/total_round)
            
            start = time.time()
            self.rnn.eval()
            
            test_y_res = self.rnn(var_x_test)
            test_loss = self.loss_func(test_y_res, var_y_test)
            test_loss_list.append(test_loss.detach().numpy())
            end = time.time()
            print('the average test loss is {}, with time: {}'.format(float(test_loss), end-start))
</code></pre>
<p>Code for my rnn model:</p>
<pre><code class="lang-auto">class my_rnn(nn.Module):

    def __init__(self,input_size,hidden_size,time_step):
        super(my_rnn,self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.T = time_step

        self.lstm = nn.LSTM(input_size=input_size,hidden_size=hidden_size,num_layers=1,batch_first = True)
        self.linear = nn.Linear(hidden_size,1)
    
    def forward(self,driving_x):
        lstm_out, hidden = self.lstm(driving_x)
        output = self.linear(lstm_out[:,-1,:])
        return output
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/d/d63e341801b7a2b1e3a3dc89c91ca63da6c1c21a.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d63e341801b7a2b1e3a3dc89c91ca63da6c1c21a" title="IMG_6284"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/d/d63e341801b7a2b1e3a3dc89c91ca63da6c1c21a_2_219x500.jpeg" alt="IMG_6284" data-base62-sha1="uzhrQ7byrk4DZUBJNuNEmnakn4C" width="219" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/d/d63e341801b7a2b1e3a3dc89c91ca63da6c1c21a_2_219x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/2X/d/d63e341801b7a2b1e3a3dc89c91ca63da6c1c21a_2_328x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/d/d63e341801b7a2b1e3a3dc89c91ca63da6c1c21a_2_438x1000.jpeg 2x" data-dominant-color="161616"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">IMG_6284</span><span class="informations">1000×2282 855 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>