In this Python Deep Learning tutorial, an implementation and explanation is given for an Elman RNN. The implementation is done in Tensorflow, which is one of the many Python Deep Learning libraries.
A more modern RNN is the GRU. A GRU has less parameters to train and is therefore quite fast. An implementation in Tensorflow of the GRU can be found here.
What is an Elman RNN?
Suppose you have many documents and you would like to find all names in the documents. Then one option is to use an Elman RNN! This RNN is originally invented by Jeffrey Elman . The Elman RNN reads word (and context) by word (and context) and tries to predict the label (either O if the word is not a name, B-NAME if the word is the beginning of a name and I-NAME if the word is the continuation of a name). One example of such a labeling is the following:
Barack Obama is a former president of the USA.
The labelling (or annotation) would then be as follows:
- Barack [B-NAME]
- Obama [I-NAME]
- is [O]
- a [O]
- president [O]
- of [O]
- the [O]
- USA [O]
- . [O]
In the next section, we will look at the architecture of the network.
The Elman Recurrent Neural Network is a neural network with a variable number of recursions. In this article, we will implement one including a Word Embedding layer which converts words to a semantic representation. Then, these embeddings are fed to the recursion layer which takes the previous hidden layer () and current token () as input and produces probabilities for each of the classes (, , ) as output. The internal hidden layer and the next token are then used as input for the recursion.
A few words on the implementation
I found it extremely useful to use Tensorboard! With Tensorboard, you can visualize the variables and by that I discovered that I obtained NaN values fairly quickly. This was due to an exploding gradient. I limited the learning rate and things went well. The loss decreased and in the end I obtained satisfying results.
I always create classes that load the data. In that way, I have a data abstraction and that is useful because almost every project has different data. I can then reuse my models and just plugin another data loader for different projects. Furthermore, make sure that your data formats are fast to read! I now use cPickle for storing and loading my data and this saved me a lot of time since for every run I need to load many datafiles. If you have a different approach, please let me know in the comment section below!
So, now I told you about the model, I will give you the implementation in Python and using Tensorflow. If you have any questions, please ask me and send a comment in the comment section below! I will first show you the graph obtained from the Tensorboard:
I grouped relevant layers into scopes. What you can clearly see is that the input layer sends its data to the recurrence layer. This is correct, since the inputs are converted into embeddings and the embeddings are used for the recurrence layer. Then, the outputs of the recurrence layers are used to calculate the output. For backpropagation, the gradients are computed and are fed back into the network to update the weights and biases.
This is the resulting code for my Elman RNN:
This Python deep learning tutorial showed you how to implement an Elman RNN in Tensorflow. If you have any unanswered questions, feel free to ask them in the comments!
 Elman, Jeffrey L. “Distributed representations, simple recurrent networks, and grammatical structure.” Machine learning 7.2-3 (1991): 195-225.
Get updates in your inbox
Join over 8,000 data science learners.