LSTM: Sine Wave Prediction using Adam and batches

rohit901: <p>Hey, all</p>
<p>I have been trying to understand the PyTorch sine wave example given here: <a href="https://github.com/pytorch/examples/tree/master/time_sequence_prediction" rel="noopener nofollow ugc">example</a></p>
<p>It took me some time to digest what actually is happening and how the input/output pair is made in this.<br>
They have used LBFGS and have fed all the batches at once which might not be feasible in every case, thus I was trying to implement the same example using batched way and using Adam Optimizer.</p>
<p>I have also tweaked the Model structure a bit, now my model is like this:</p>
<pre><code class="lang-auto">class Sequence(nn.Module):
    def __init__(self):
        super(Sequence, self).__init__()
        self.lstm1 = nn.LSTMCell(1, 50) #only 1 feature in input, 51 is no of features in hidden_state
        self.lstm2 = nn.LSTMCell(50, 50) #prev cell outputs 51, so this input is 51, and it outputs 51 (hidden_dim)
        self.linear = nn.Linear(50, 36) #takes 51 dim to predict 1 value
        self.relu = nn.ReLU()
        self.linear2 = nn.Linear(36, 24)
        self.linear3 = nn.Linear(24, 1)
        
    def forward(self, input, future = 0):
        outputs = []
        h_t = torch.zeros(input.size(0), 50, dtype = torch.double) #shape is 97x51 (batch,hidden_size)
        c_t = torch.zeros(input.size(0), 50, dtype = torch.double) #shape is 97x51 (batch,hidden_size)
        h_t2 = torch.zeros(input.size(0), 50, dtype = torch.double)
        c_t2 = torch.zeros(input.size(0), 50, dtype = torch.double)
        
        #hidden state and cell state are set to 0 in every new "batch" of examples
        
        for input_t in input.split(1, dim = 1): # input_t is of shape [batch_size,1], loop always runs for 999 times
            h_t, c_t = self.lstm1(input_t, (h_t, c_t))
            h_t2, c_t2 = self.lstm2(h_t, (h_t2, c_t2))
            output = self.linear3(self.relu(self.linear2(self.relu(self.linear(h_t2))))) #output is of shape [batch_size, 1], for every time step t, output has (t+1)th value prediction for all sine waves
            outputs += [output]
        for i in range(future):
            h_t, c_t = self.lstm1(output, (h_t, c_t)) #
            h_t2, c_t2 = self.lstm2(h_t, (h_t2, c_t2))
            output = self.linear3(self.relu(self.linear2(self.relu(self.linear(h_t2)))))
            #output = self.linear(h_t2)
            outputs += [output]
        outputs = torch.cat(outputs, dim = 1) #concat predictions across all timesteps from t1 to t999, and future
        return outputs
</code></pre>
<p>Optimizer:</p>
<p><code>optim = torch.optim.Adam(seq.parameters(), lr = 0.08)</code></p>
<p>Dataset:</p>
<pre><code class="lang-auto">class seqDS(Dataset):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.len = x.shape[0]
    def __getitem__(self, idx):
        return self.x[idx], self.y[idx]
    def __len__(self):
        return self.len
    
ds = seqDS(input, target)
</code></pre>
<p>Loader:</p>
<p><code>train_loader = DataLoader(ds, shuffle = False, batch_size = 16)</code></p>
<p>and finally, the training Loop:</p>
<pre><code class="lang-auto">for i in range(50):
    loss = 0.0
    num_len = 0
    seq.train()
    for x,y in train_loader:
        batch_size = x.shape[0]
        y_pred = seq(x)
        optim.zero_grad()
        loss = criterion(y_pred, y)
        loss.backward()
        optim.step()
        num_len += batch_size
        loss += (batch_size * loss.item())
    loss = loss / (num_len)
    print(f"Epoch {i+1}, Loss: {loss}")
    
    with torch.no_grad():
        future = 1000
        pred = seq(test_input, future=future)
        loss = criterion(pred[:, :-future], test_target)
        print('test loss:', loss.item())
        print("==========================")
        y = pred.detach().numpy()
    
    plt.figure(figsize=(30,10))
    plt.title('Predict future values for time sequences\n(Dashlines are predicted values)', fontsize=30)
    plt.xlabel('x', fontsize=20)
    plt.ylabel('y', fontsize=20)
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    def draw(yi, color):
        plt.plot(np.arange(input.size(1)), yi[:input.size(1)], color, linewidth = 2.0)
        plt.plot(np.arange(input.size(1), input.size(1) + future), yi[input.size(1):], color + ':', linewidth = 2.0)
    draw(y[0], 'r')
    draw(y[1], 'g')
    draw(y[2], 'b')
    plt.savefig('predict%d.pdf'%i)
    plt.close()
</code></pre>
<p>Now, as you can see from this plot below, the results are very disappointing and the model is not able to predict future values properly and even is not very accurate on predicting test set values that have the real ground truth labels.</p>
<p>I really want to go deep into this and understand what is actually happening and how can I fix this, LSTM is supposed to have a memory element due to cell state that’s what I know, and since the cell state used at time t = 0 is propagated to time t = 999, why its still failing to predict the sine wave values for future times (after t = 1000) on the test set data?</p>
<p>Thank you so much for your time.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/b/1bf58d2e22d5f55aeda6d9456330b290c244fab8.png" data-download-href="https://discuss.pytorch.org/uploads/default/1bf58d2e22d5f55aeda6d9456330b290c244fab8" title="lstm_results"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1bf58d2e22d5f55aeda6d9456330b290c244fab8_2_690x230.png" alt="lstm_results" data-base62-sha1="3ZkYXVLF9S68pPkdngFZP2OW4OY" width="690" height="230" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1bf58d2e22d5f55aeda6d9456330b290c244fab8_2_690x230.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1bf58d2e22d5f55aeda6d9456330b290c244fab8_2_1035x345.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1bf58d2e22d5f55aeda6d9456330b290c244fab8_2_1380x460.png 2x" data-dominant-color="E7E5E7"><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">lstm_results</span><span class="informations">1918×642 87.1 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>

rohit901: <p>Well, i was just tweaking the model a bit and saw prediction result for 12th epoch, its like this:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/7/57636e1dd704d451dc4a007224881c99b46ccd66.png" data-download-href="https://discuss.pytorch.org/uploads/default/57636e1dd704d451dc4a007224881c99b46ccd66" title="12th_epoch_pred"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/57636e1dd704d451dc4a007224881c99b46ccd66_2_690x232.png" alt="12th_epoch_pred" data-base62-sha1="ct4zduStwF035gac5GvUmZRhcr4" width="690" height="232" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/57636e1dd704d451dc4a007224881c99b46ccd66_2_690x232.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/57636e1dd704d451dc4a007224881c99b46ccd66_2_1035x348.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/57636e1dd704d451dc4a007224881c99b46ccd66_2_1380x464.png 2x" data-dominant-color="E6E4E6"><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">12th_epoch_pred</span><span class="informations">1709×575 390 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>
<p>Now, I’m confused, why does the later epochs (epoch &gt; 12) might not be having this good prediction as this one? Shouldn’t the prediction (on future time) increase if the test loss/train loss is decreasing?</p>
<p>This is the result at 50th epoch:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/a/4a0bdf95dde79b05744f13884978113ffda7ae51.png" data-download-href="https://discuss.pytorch.org/uploads/default/4a0bdf95dde79b05744f13884978113ffda7ae51" title="predict49"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/a/4a0bdf95dde79b05744f13884978113ffda7ae51_2_690x229.png" alt="predict49" data-base62-sha1="az2LPju81wYZcr4l8AdmeGxKzol" width="690" height="229" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/a/4a0bdf95dde79b05744f13884978113ffda7ae51_2_690x229.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/a/4a0bdf95dde79b05744f13884978113ffda7ae51_2_1035x343.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/a/4a0bdf95dde79b05744f13884978113ffda7ae51_2_1380x458.png 2x" data-dominant-color="E7E5E7"><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">predict49</span><span class="informations">2160×720 303 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>

googlebot: <p>This task is hard for RNNs, correct rule would actually only need one step lookback to distinguish half-cycles and otherwise would just memorize step differentials by last position in post-RNN layers.</p>
<p>But that rule is elusive, especially with an oversized network and unlimited lookback. So, the L-BFGS optimizer manages to find a better solution, by being more thorough and not using mini-batches. Training with usual optimizers is more fragile, easily picking step transition rules that don’t generalize to &gt;1000 events (nb: LSTM’s update&amp;forget gates don’t sum to 1, so “amount of information” is not auto-balanced, unlike GRU)</p>
<p>Switching to an optimizer without momentum, tuning it and adding lr scheduler may help, but perhaps it would be better to start from some other example instead.</p>