In a former article, we seemed on the concept and math behind Bidirectional RNNs: Vanilla RNNs, LSTMs, and GRUs, whether or not they be deep or not. On this article, we are going to see how we are able to implement a Bidirectional LSTM in Python. We are going to each construct one which’s deep, and one which’s not. On this article, we won’t be trying on the maths — we now have already seen it in varied different articles, so we are going to solely concentrate on the implementation.
We are going to begin by implementing the Bidirectional LSTM, which isn’t deep. Allow us to get began.
Importing Libraries
We import the wanted libraries. We will likely be utilizing scikit-learn to judge our mannequin.
import numpy as np
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
import matplotlib.pyplot as plt
Defining Activation Perform
We will then outline the activation operate:
def dtanh(x):
return 1.0 - np.tanh(x) ** 2
Implementing Bidirectional LSTM
We will now, lastly, implement the Bidirectional LSTM itself. We divide it into two elements:
- LSTMCell: handles the low-level math for the ahead step and gradient computations for backward step — for…