LSTMs in time series forecasting
In the NLP sections, we discussed the capabilities of LSTMs and their improvements over RNNs by mitigating issues such as the vanishing gradient problem, enabling the model to learn longer sequences. In the context of time series forecasting, LSTM networks can be quite powerful. Let’s see how we can apply LSTMs to our sales dataset:
- Let’s begin by preparing our data:
# Create sequences
seq_len = 20
X = []
y = []
for i in range(seq_len, len(data)):
X.append(data[i-seq_len:i])
y.append(data[i])
X = np.array(X)
X = X.reshape(X.shape[0], X.shape[1], 1)
y = np.array(y)
# Train/val split
split = int(0.8*len(X))
X_train, X_val = X[:split], X[split:]
y_train, y_val = y[:split], y[split:]
# Set batch size and buffer size
batch_size = 64
buffer_size = 1000
# Create dataset
dataset = tf.data.Dataset.from_tensor_slices(
(X_train, y_train))
dataset = dataset.shuffle(
...