The strategy discussed above is coded as follows (the code file is available as Building_a_Recurrent_Neural_Network_from_scratch-in_Python.ipynb in GitHub):
- Let's define the input and output in code, as follows:
#define documentsdocs = ['this, is','is an']# define class labelslabels = ['an','example']
- Let's preprocess our dataset so that it can be passed to an RNN:
from collections import Countercounts = Counter()for i,review in enumerate(docs+labels): counts.update(review.split())words = sorted(counts, key=counts.get, reverse=True)vocab_size=len(words)word_to_int = {word: i for i, word in enumerate(words, 1)}
In the preceding step, we are identifying all the unique words and their corresponding frequency (counts) in a ...