# Predicting next 60 days using pytorch (LSTM)

**URL:** <https://discuss.pytorch.org/t/predicting-next-60-days-using-pytorch-lstm/46476>\
**Category:** Uncategorized\
**Created:** [May 28, 2019, 9:59pm UTC](https://discuss.pytorch.org/t/predicting-next-60-days-using-pytorch-lstm/46476 "2019-05-28T21:59:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akaori](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/e480ec/32.png) [@Akaori](https://discuss.pytorch.org/u/Akaori)\
**Post date:** [May 28, 2019, 9:59pm UTC](https://discuss.pytorch.org/t/predicting-next-60-days-using-pytorch-lstm/46476/1 "2019-05-28T21:59:15Z")

</div>

I am trying to use pytorch to make predictions on time-series dataset.

1- First, I splitted the dataset into training and test.

[![Dataset](https://i.stack.imgur.com/RY8YB.png)](https://i.stack.imgur.com/RY8YB.png)

2- Then, I created the model.

```
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()

```

3- After training the model, I made the validation step.

```
mse = mean_squared_error(dataset['y'].iloc[60:].values,predicted)
print("RMSE:", np.sqrt(mse))

# RMSE: 0.10680269716212222

```

_Question_: I would like to know, how can I use this model to predict the next 60 days ?

I read that is necessary to: **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.**

So, I tried this:

```
#array --> 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]

```

This Variable `tomorrow` is my tomorrow prediction, however, how to do this, for the next 60 days ?

---

<div class="post-metadata">

**Author:** ![scarecrow21](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scarecrow21](https://discuss.pytorch.org/u/scarecrow21)\
**Post date:** [May 29, 2019, 11:19am UTC](https://discuss.pytorch.org/t/predicting-next-60-days-using-pytorch-lstm/46476/2 "2019-05-29T11:19:48Z")

</div>

After you split your dataset into train and test, you need a create a data structure that looks like this:  
Say, you have 10 rows in your dataset and you want to predict 1 value after looking at the previous 5 time steps.

1, 2, 3, 4, 5 -\> 6  
2, 3, 4, 5, 6 -\> 7  
3, 4, 5, 6, 7 -\> 8  
and so on…

So, X\_train would be a numpy array:  
[[ 1, 2, 3, 4, 5],  
[2 ,3, 4, 5, 6],  
[3, 4, 5, 6, 7],  
.  
.  
]

y\_train would be a numpy array:  
[5, 6, 7…]

Pass these into your RNN after converting this to tensors. Change shape as require by the rnn.

You can change your y\_train to contain multiple numbers to predict multiple days into the future:  
y\_train:  
[[5 , 6, 7],  
[6, 7, 8],  
[7, 8, 9]]

You’ll need to change the `hidden_size` argument in the rnn accordingly.

---

<div class="post-metadata">

**Author:** ![Akaori](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/e480ec/32.png) [@Akaori](https://discuss.pytorch.org/u/Akaori)\
**Post date:** [May 29, 2019, 5:32pm UTC](https://discuss.pytorch.org/t/predicting-next-60-days-using-pytorch-lstm/46476/3 "2019-05-29T17:32:11Z")

</div>

Hi @scarecrow21,

Thanks for your reply.

I have in mind, doing something like FbProphet, where I can make a prediction of the next 30 days looking to the whole past.

 ![NCz3fVW](https://discuss.pytorch.org/uploads/default/original/2X/7/7795260baadc65ee99f5646967d1b257f33e8db2.png)

I am trying to do something like this:

> ```
> 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)
> 
> ```
