|
| 1 | +# _*_ coding: utf-8 _*_ |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch.nn as nn |
| 5 | +from torch.autograd import Variable |
| 6 | +from torch.nn import functional as F |
| 7 | + |
| 8 | +class RCNN(nn.Module): |
| 9 | +def __init__(self, batch_size, output_size, hidden_size, vocab_size, embedding_length, weights): |
| 10 | +super(RCNN, self).__init__() |
| 11 | + |
| 12 | +""" |
| 13 | +Arguments |
| 14 | +--------- |
| 15 | +batch_size : Size of the batch which is same as the batch_size of the data returned by the TorchText BucketIterator |
| 16 | +output_size : 2 = (pos, neg) |
| 17 | +hidden_sie : Size of the hidden_state of the LSTM |
| 18 | +vocab_size : Size of the vocabulary containing unique words |
| 19 | +embedding_length : Embedding dimension of GloVe word embeddings |
| 20 | +weights : Pre-trained GloVe word_embeddings which we will use to create our word_embedding look-up table |
| 21 | +
|
| 22 | +""" |
| 23 | + |
| 24 | +self.batch_size = batch_size |
| 25 | +self.output_size = output_size |
| 26 | +self.hidden_size = hidden_size |
| 27 | +self.vocab_size = vocab_size |
| 28 | +self.embedding_length = embedding_length |
| 29 | + |
| 30 | +self.word_embeddings = nn.Embedding(vocab_size, embedding_length)# Initializing the look-up table. |
| 31 | +self.word_embeddings.weight = nn.Parameter(weights, requires_grad=False) # Assigning the look-up table to the pre-trained GloVe word embedding. |
| 32 | +self.dropout = 0.8 |
| 33 | +self.lstm = nn.LSTM(embedding_length, hidden_size, dropout=self.dropout, bidirectional=True) |
| 34 | +self.W2 = nn.Linear(2*hidden_size+embedding_length, hidden_size) |
| 35 | +self.label = nn.Linear(hidden_size, output_size) |
| 36 | + |
| 37 | +def forward(self, input_sentence, batch_size=None): |
| 38 | + |
| 39 | +""" |
| 40 | +Parameters |
| 41 | +---------- |
| 42 | +input_sentence: input_sentence of shape = (batch_size, num_sequences) |
| 43 | +batch_size : default = None. Used only for prediction on a single sentence after training (batch_size = 1) |
| 44 | +
|
| 45 | +Returns |
| 46 | +------- |
| 47 | +Output of the linear layer containing logits for positive & negative class which receives its input as the final_hidden_state of the LSTM |
| 48 | +final_output.shape = (batch_size, output_size) |
| 49 | +
|
| 50 | +""" |
| 51 | + |
| 52 | + """ |
| 53 | + |
| 54 | + The idea of the paper "Recurrent Convolutional Neural Networks for Text Classification" is that we pass the embedding vector |
| 55 | + of the text sequences through a bidirectional LSTM and then for each sequence, our final embedding vector is the concatenation of |
| 56 | + its own GloVe embedding and the left and right contextual embedding which in bidirectional LSTM is same as the corresponding hidden |
| 57 | + state. This final embedding is passed through a linear layer which maps this long concatenated encoding vector back to the hidden_size |
| 58 | + vector. After this step, we use a max pooling layer across all sequences of texts. This converts any varying length text into a fixed |
| 59 | + dimension tensor of size (batch_size, hidden_size) and finally we map this to the output layer. |
| 60 | +
|
| 61 | + """ |
| 62 | +input = self.word_embeddings(input_sentence) # embedded input of shape = (batch_size, num_sequences, embedding_length) |
| 63 | +input = input.permute(1, 0, 2) # input.size() = (num_sequences, batch_size, embedding_length) |
| 64 | +if batch_size is None: |
| 65 | +h_0 = Variable(torch.zeros(2, self.batch_size, self.hidden_size).cuda()) # Initial hidden state of the LSTM |
| 66 | +c_0 = Variable(torch.zeros(2, self.batch_size, self.hidden_size).cuda()) # Initial cell state of the LSTM |
| 67 | +else: |
| 68 | +h_0 = Variable(torch.zeros(2, batch_size, self.hidden_size).cuda()) |
| 69 | +c_0 = Variable(torch.zeros(2, batch_size, self.hidden_size).cuda()) |
| 70 | + |
| 71 | +output, (final_hidden_state, final_cell_state) = self.lstm(input, (h_0, c_0)) |
| 72 | + |
| 73 | +final_encoding = torch.cat((output, input), 2).permute(1, 0, 2) |
| 74 | +y = self.W2(final_encoding) # y.size() = (batch_size, num_sequences, hidden_size) |
| 75 | +y = y.permute(0, 2, 1) # y.size() = (batch_size, hidden_size, num_sequences) |
| 76 | +y = F.max_pool1d(y, y.size()[2]) # y.size() = (batch_size, hidden_size, 1) |
| 77 | +y = y.squeeze(2) |
| 78 | +logits = self.label(y) |
| 79 | + |
| 80 | +return logits |
0 commit comments