How to do it...

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

  1. Let's define the input and output in code, as follows:
#define documentsdocs = ['this, is','is an']# define class labelslabels = ['an','example']
  1. 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 ...

Get Neural Networks with Keras Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.