-
Notifications
You must be signed in to change notification settings - Fork 5
/
model_classifier.py
142 lines (110 loc) · 5.3 KB
/
model_classifier.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import torch.nn as nn
import torch
import torch.nn.functional as F
# from torch.autograd import Variable
class uniRNN(nn.Module):
def __init__(self, options):
super(uniRNN, self).__init__()
self.options = options
print options
self.char_embedding = nn.Embedding(options['vocab_size'], options['embedding_size'])
self.lstm = nn.LSTM(options['embedding_size'], options['hidden_size'], batch_first = True)
self.output_layer = nn.Linear(options['hidden_size'], options['target_size'])
def forward(self, sentence_batch, hidden = None):
# sentence_batch = Variable(sentence_batch)
if len(sentence_batch.size()) == 2:
char_embedding = self.char_embedding(sentence_batch)
else:
shape = sentence_batch.size()
sentence_batch_flat = sentence_batch.view(shape[0] * shape[1], shape[2])
char_embedding = torch.mm(sentence_batch_flat, self.char_embedding.weight)
char_embedding = char_embedding.view(shape[0], shape[1], char_embedding.size()[-1])
char_embedding = F.tanh(char_embedding)
if not hidden:
lstm_out, new_hidden = self.lstm(char_embedding)
else:
lstm_out, new_hidden = self.lstm(char_embedding, hidden)
# print lstm_out.shape, lstm_out.shape[0] * lstm_out.shape[1]
lstm_out = lstm_out.contiguous()
# print type(lstm_out)
# print lstm_out.shape
lstm_out = lstm_out[:,-1,:]
# lstm_out = lstm_out.view(lstm_out.size(0) * lstm_out.size(1), lstm_out.size(2))
logits = self.output_layer(lstm_out)
return logits
class biRNN(nn.Module):
def __init__(self, options):
super(biRNN, self).__init__()
self.options = options
print options
self.drop = nn.Dropout(0.7)
self.char_embedding = nn.Embedding(options['vocab_size'], options['embedding_size'])
self.lstm = nn.LSTM(options['embedding_size'], options['hidden_size'], batch_first = True, bidirectional=True)
self.output_layer = nn.Linear(2*options['hidden_size'], options['target_size'])
def forward(self, sentence_batch, hidden = None):
# sentence_batch = Variable(sentence_batch)
if len(sentence_batch.size()) == 2:
char_embedding = self.char_embedding(sentence_batch)
else:
shape = sentence_batch.size()
sentence_batch_flat = sentence_batch.view(shape[0] * shape[1], shape[2])
char_embedding = torch.mm(sentence_batch_flat, self.char_embedding.weight)
char_embedding = char_embedding.view(shape[0], shape[1], char_embedding.size()[-1])
char_embedding = F.tanh(char_embedding)
if not hidden:
lstm_out, new_hidden = self.lstm(char_embedding)
else:
lstm_out, new_hidden = self.lstm(char_embedding, hidden)
# print lstm_out.shape, lstm_out.shape[0] * lstm_out.shape[1]
lstm_out = lstm_out.contiguous()
# print lstm_out.size()
# print type(lstm_out)
# print lstm_out.shape
lstm_out = lstm_out[:,-1,:] + lstm_out[:,0,:]
# lstm_out = lstm_out.view(lstm_out.size(0) * lstm_out.size(1), lstm_out.size(2))
logits = self.output_layer(lstm_out)
return logits
class CnnTextClassifier(nn.Module):
def __init__(self, options, window_sizes=(3, 4, 5)):
super(CnnTextClassifier, self).__init__()
self.options = options
self.embedding = nn.Embedding(options['vocab_size'], options['embedding_size'])
self.convs = nn.ModuleList([
nn.Conv2d(1, options['hidden_size'], [window_size, options['embedding_size']], padding=(window_size - 1, 0))
for window_size in window_sizes
])
self.fc = nn.Linear(options['hidden_size'] * len(window_sizes), options['target_size'])
def forward(self, sentence_batch):
if len(sentence_batch.size()) == 2:
char_embedding = self.embedding(sentence_batch)
else:
shape = sentence_batch.size()
sentence_batch_flat = sentence_batch.view(shape[0] * shape[1], shape[2])
char_embedding = torch.mm(sentence_batch_flat, self.embedding.weight)
char_embedding = char_embedding.view(shape[0], shape[1], char_embedding.size()[-1])
# x = self.embedding(char_embedding) # [B, T, E]
# Apply a convolution + max pool layer for each window size
x = torch.unsqueeze(char_embedding, 1) # [B, C, T, E] Add a channel dim.
xs = []
for conv in self.convs:
x2 = F.relu(conv(x)) # [B, F, T, 1]
x2 = torch.squeeze(x2, -1) # [B, F, T]
x2 = F.max_pool1d(x2, x2.size(2)) # [B, F, 1]
xs.append(x2)
x = torch.cat(xs, 2) # [B, F, window]
# FC
x = x.view(x.size(0), -1) # [B, F * window]
logits = self.fc(x) # [B, class]
return logits
def main():
rnn_options = {
'vocab_size' : 100,
'hidden_size' : 200,
'target_size' : 2,
'embedding_size' : 200,
}
chrrnn = CnnTextClassifier(rnn_options)
sent_batch = torch.FloatTensor(32, 10, 100).random_(0, 10)
print chrrnn(sent_batch)
if __name__ == '__main__':
main()