Predicting next 60 days using pytorch (LSTM)

username: Akaori
cooked: <p>I am trying to use pytorch to make predictions on time-series dataset.</p>
<p>1- First, I splitted the dataset into training and test.</p>
<p><a href="https://i.stack.imgur.com/RY8YB.png" rel="nofollow noopener"><img src="https://i.stack.imgur.com/RY8YB.png" alt="Dataset" width="495" height="339"></a></p>
<p>2- Then, I created the model.</p>
<pre><code>import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

        self.rnn = nn.LSTM(input_size=1,hidden_size=50,num_layers=3,batch_first=True,dropout=0.2)
        self.out = nn.Linear(50, 1)

    def forward(self, x):

        r_out, (h_n, h_c) = self.rnn(x, None)
        x = r_out[:,-1,:]                    #last hidden output!
        x = self.out(x)
        return x
    
model = Model()

if cuda:
    model.cuda()
</code></pre>
<p>3- After training the model, I made the validation step.</p>
<pre><code>mse = mean_squared_error(dataset['y'].iloc[60:].values,predicted)
print("RMSE:", np.sqrt(mse))

# RMSE: 0.10680269716212222
</code></pre>
<p><em>Question</em>: I would like to know, how can I use this model to predict the next 60 days ?</p>
<p>I read that is necessary to: <strong>Take the last prediction value(i.e predicted[-1]) from my predictions and add it to the last array of total_x, excluding the first timestep.</strong></p>
<p>So, I tried this:</p>
<pre><code>#array --&gt; List
today = total_x[-1].reshape(-1).tolist()

# scaling last price
last_price = scaler.transform(infered['y_pred'][-1].reshape(-1, 1))

# adding last price to list.
today.append(last_price[0])

# Exclude first(0th index) element
today = today[1:]

#reshape
today = np.array(today).reshape(-1,60,1)

today = torch.Tensor(list(today))

#predict!
tomorrow = predict_with_pytorch(model, today)

#inverse transform.
tomorrow = scaler.inverse_transform(tomorrow)[0]
</code></pre>
<p>This Variable <code>tomorrow</code> is my tomorrow prediction, however, how to do this, for the next 60 days ?</p>

username: scarecrow21
cooked: <p>After you split your dataset into train and test, you need a create a data structure that looks like this:<br>
Say, you have 10 rows in your dataset and you want to predict 1 value after looking at the previous 5 time steps.</p>
<p>1, 2, 3, 4, 5 -&gt; 6<br>
2, 3, 4, 5, 6 -&gt; 7<br>
3, 4, 5, 6, 7 -&gt; 8<br>
and so on…</p>
<p>So, X_train would be a numpy array:<br>
[ [ 1, 2, 3, 4, 5],<br>
[2 ,3, 4, 5, 6],<br>
[3, 4, 5, 6, 7],<br>
.<br>
.<br>
]</p>
<p>y_train would be a numpy array:<br>
[5, 6, 7…]</p>
<p>Pass these into your RNN after converting this to tensors. Change shape as require by the rnn.</p>
<p>You can change your y_train to contain multiple numbers to predict multiple days into the future:<br>
y_train:<br>
[[5 , 6, 7],<br>
[6, 7, 8],<br>
[7, 8, 9]]</p>
<p>You’ll need to change the <code>hidden_size</code> argument in the rnn accordingly.</p>

username: Akaori
cooked: <p>Hi <a class="mention" href="/u/scarecrow21">@scarecrow21</a>,</p>
<p>Thanks for your reply.</p>
<p>I have in mind, doing something like FbProphet, where I can make a prediction of the next 30 days looking to the whole past.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/7/7795260baadc65ee99f5646967d1b257f33e8db2.png" data-download-href="https://discuss.pytorch.org/uploads/default/7795260baadc65ee99f5646967d1b257f33e8db2" title="NCz3fVW"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/7/7795260baadc65ee99f5646967d1b257f33e8db2_2_690x414.png" alt="NCz3fVW" data-base62-sha1="h3SmQcr2rYdSsPBNiT6eIe0xYiK" width="690" height="414" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/7/7795260baadc65ee99f5646967d1b257f33e8db2_2_690x414.png, https://discuss.pytorch.org/uploads/default/original/2X/7/7795260baadc65ee99f5646967d1b257f33e8db2.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/7/7795260baadc65ee99f5646967d1b257f33e8db2.png 2x" data-dominant-color="CDD7DC"><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">NCz3fVW</span><span class="informations">720×432 73.6 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>I am trying to do something like this:</p>
<blockquote>
<pre><code>def plot_results_multiple(predicted_data, true_data,length):
   plt.plot(scaler.inverse_transform(true_data.reshape(-1, 1))[length:])
   plt.plot(scaler.inverse_transform(np.array(predicted_data).reshape(-1, 1))[length:])
   plt.show()

#predict lenght consecutive values from a real one
def predict_sequences_multiple(model, firstValue,length):
  prediction_seqs = []
  curr_frame = firstValue
 
for i in range(length): 
    predicted = []        
    
    print(predict_with_pytorch(model, curr_frame[newaxis,:,:]))
    predicted.append(predict_with_pytorch(model, curr_frame[newaxis,:,:])[0,0])
    
    curr_frame = curr_frame[0:]
    curr_frame = np.insert(curr_frame[0:], i+1, predicted[-1], axis=0)
    
    prediction_seqs.append(predicted[-1])
    
return prediction_seqs

predict_length=60
predictions = predict_sequences_multiple(model, total_x[-1], predict_length)
print(scaler.inverse_transform(np.array(predictions).reshape(-1, 1)))
plot_results_multiple(predictions, total_y, predict_length)
</code></pre>
</blockquote>