A language model basically calculates the probability of a particular sequence of words. To make a language model using a RNN we will require a training set comprising a large corpus of English text or text form of whatever language we want to build the model on. Let’s say we get a sentence in our training set as follows, Cats average 15 hours of sleep a day.
The first thing we do is tokenize the sentence. We form a vocabulary and then map each of these words to one-hot vectors in the vocabulary. One thing we might also want is our model to know when a sentence ends, so we add a token called EOS which stands for end of sentence. If one of the words in not there in out vocab then we use the unique token called UNK which stands for unknown words.
So for the given sentence we have the tokens as follows :
Now let is see how we can build the RNN model
RNN model
We assume that the first activation and the first input are both initialized to a zero vector. We then let the network calculate the first word and call this prediction . In the second network now, we give the model the input as the first correct word . Then the network calculates and the second word prediction . Now in the next network where it predicts the third word, we give the second words as the input.
So in the first network .
Here we can also denote as the probability of the word Cats coming in the sequence .
Now in the second layer, we take and then perform the calculations. For the third layer we take and so on till we reach the EOS token.
We can treat as the probability of the word average given the word Cats i.e. . Similarly as and finally as .
So the RNN learns to predict one word at a time going from left to right. To train this network we will define a cost function. At a certain time t, the elemental loss is
and the overall loss as

Sampling Novel Sequences
In order to sample a sequence we do something different. We perform computations till the first network in the same way as above and get . Here the vector has the same size as the size of the vocab. Each element in this vector gives a probability of the word appearing in the sequence. We randomly sample across this vector say using the numpy command np.random.choice.
Now generally as we saw above we used the as input, but here we use the sampled as the input for the next network. We continue this across the networks until we generate the EOS token.
This process sometimes generates the unknown word token UNK, one thing we could do is just reject any sample that comes out as this token and just resample for the rest of the vocab until we get a token which is not the token UNK.
Vocab level
Here in the examples before we had word level vocabulary. We can also have a letter level vocab with all the alphabets, numbers, symbols, capital alphabets and so on.
Such a vocab is only used in specialized applications where we need to have more vocabulary.
Vanishing Gradients with RNNs
Suppose we have a sentence as follows : The cat which already ate …, was full.
Now if we had multiple cats then : The cats which already ate …, were full.
So here our model need to remember that the word cat was singular or plural, so as to use the proper word at the end of the sentence. So if the word is near the end of sentence, it is influenced very less by a word which is at the start if the sequence is long.
This is a weakness of the basic RNN algorithm. This is a issue of vanishing gradients.
Exploding gradients do not usually occur but when they do we can apply gradient clipping. We look at our gradient vector and if it is bigger than some threshold, we re-scale some of the vectors.
Vanishing gradients is a problem which is much harder to solve. Next we will take a look at Greater Recurrent Units (GRU) which are a very effective solution for addressing the vanishing gradient problem and will allow our RNN to capture much longer range dependencies.