-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbirnn_model.py
35 lines (29 loc) · 1.68 KB
/
birnn_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import torch.nn as nn
class RNN(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, n_layers,
bidirectional, dropout, pad_idx):
super().__init__()
#TO-DO
#1. Initialize Embedding Layer
self.embedding = nn.Embedding(vocab_size, embedding_dim, padding_idx = pad_idx)
#2. Initialize RNN layer
self.rnn = nn.RNN(embedding_dim,
hidden_dim,
num_layers = n_layers,
bidirectional = bidirectional,
dropout = dropout)
#3. Initialize a fully connected layer with Linear transformation
self.fc = nn.Linear(hidden_dim * 2, output_dim)
#4. Initialize Dropout
self.dropout = nn.Dropout(dropout)
def forward(self, text, text_lengths):
#text = [sent_len, batch_size]
#TO-DO
#1. Apply embedding layer that matches each word to its vector and apply dropout. Dim [sent_len, batch_size, emb_dim]
embedded = self.dropout(self.embedding(text))
#2. Run the RNN along the sentences of length sent_len. #output = [sent len, batch size, hid dim * num directions]; #hidden = [num layers * num directions, batch size, hid dim]
packed_embedded = nn.utils.rnn.pack_padded_sequence(embedded, text_lengths.to('cpu'))
output, hidden = self.rnn(packed_embedded)
#3. Concat the final forward (hidden[-2,:,:]) and backward (hidden[-1,:,:]) hidden layers and apply dropout
hidden = self.dropout(torch.cat((hidden[0,:,:], hidden[1,:,:]), dim = 1))
return self.fc(hidden)