How to calculate correct batch size for LSTM?
I have a daily time series data like below.    

            CashIn  CashOut
Date        
2016-01-01  0.0     6500.0
2016-01-02  0.0     23110.0
2016-01-03  0.0     7070.0
2016-01-04  0.0     18520.0
2016-01-05  20840.0 22200.0
.
.
.
2019-03-25  59880.0 25500.0
2019-03-26  49270.0 17860.0
2019-03-27  45160.0 48600.0
2019-03-28  39480.0 22840.0
2019-03-29  70260.0 25950.0
2019-03-30  19250.0 24350.0
2019-03-31  46870.0 14400.0


My total data size is 1186. I want to forecast the CashIn and CashOut values between 2019-04-01 and 2019-04-30 by using LSTM.

I wrote a batch calculator like below.

def get_batches(arr, batch_size, seq_length):

    batch_size_total = batch_size * seq_length

    n_batches = len(arr)//batch_size_total

    arr = arr[:n_batches * batch_size_total]
    arr = arr.reshape((batch_size, -1))

    for n in range(0, arr.shape[1], seq_length):
        x = arr[:, n:n+seq_length]
        y = np.zeros_like(x)
        try:
            y[:, :-1], y[:, -1] = x[:, 1:], arr[:, n+seq_length]
        except IndexError:
            y[:, :-1], y[:, -1] = x[:, 1:], arr[:, 0]
        yield x, y


I am trying to divide this dataset by using get_batches function to batches which have 30 sequence length because I have daily time series and I want to predict the next 30 days.

batches = get_batches(np.array(data_cashIn), 40, 30)


If I write 39 instead of 40 as a parameter in get_bathces function then I am losing the last 16 daily data but I do not want to lose these data.

How can I do this correctly?  

I think that you'll always end up with a number that is not working. Because it's not the best practice. I suggest that you use the DataLoader which will easily load batches for you (and here's how you can have a custom dataset fed to the dataloder). By giving the batch_size to the Dataloader it will split your dataset into the maximum possible batches of batch_size with the last batch being &lt;=batch_size.



In regard to the LSTM, use batch_first=True and have your batch in this shape (batch, seq, feature). This will allow you to drop the headache of giving a specific size, and the input_size must be equal to feature.
