-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
52 lines (41 loc) · 1.97 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import torch
import torch.nn as nn
class FFN_static(nn.Module):
def __init__(self, embed_dim, num_hidden, labels_num, embedding_matrix):
super(FFN_static, self).__init__()
self.embedding = nn.Embedding.from_pretrained(embedding_matrix, padding_idx=0, freeze=True)
self.linear_1 = nn.Linear(embed_dim, num_hidden, bias=True)
self.linear_2 = nn.Linear(num_hidden, num_hidden, bias=True)
self.linear_3 = nn.Linear(num_hidden, labels_num, bias=True)
self.relu = nn.ReLU()
def forward(self, x):
embeds = self.embedding(x)
embeds = embeds.mean(dim=1)
h1 = self.linear_1(embeds)
h1 = self.relu(h1)
h2 = self.linear_2(h1)
h2 = self.relu(h2)
o = self.linear_3(h2)
return o
class LSTMEncoder_static(nn.Module):
def __init__(self, embed_dim, hidden_dim, num_classes, embedding_matrix, num_layers, bidirectional=False):
super().__init__()
self.embedding = nn.Embedding.from_pretrained(embedding_matrix, padding_idx=0, freeze=True)
self.lstm = nn.LSTM(embed_dim, hidden_dim, num_layers, batch_first=True, bidirectional=bidirectional)
if bidirectional:
self.linear = nn.Linear(hidden_dim*2, num_classes)
else:
self.linear = nn.Linear(hidden_dim, num_classes)
def forward(self, input_ids):
# (batch_size, seq_len, embed_dim)
input_embeds = self.embedding(input_ids)
# (batch_size, seq_len, hidden_dim)
lstm_hidden, _ = self.lstm(input_embeds)
# Use the hidden state of the last LSTM cell for classification
if self.lstm.bidirectional:
lstm_hidden = torch.cat((lstm_hidden[:, -1, :self.lstm.hidden_size], lstm_hidden[:, 0, self.lstm.hidden_size:]), dim=-1)
else:
lstm_hidden = lstm_hidden[:, -1, :]
# (batch_size, num_classes)
logits = self.linear(lstm_hidden)
return logits