-
Notifications
You must be signed in to change notification settings - Fork 2
/
lstm_crf_pos_run.py
189 lines (148 loc) · 6.4 KB
/
lstm_crf_pos_run.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import torch
import torch.autograd as autograd
import torch.nn as nn
import pickle
from nltk import word_tokenize
from nltk import pos_tag
import sys
print(sys.argv[1])
torch.manual_seed(1)
def argmax(vec):
_, idx = torch.max(vec, 1)
return idx.item()
def prepare_sequence(seq, to_ix):
idxs = map(lambda x: to_ix[x.lower()] if x in to_ix else to_ix['<unk>'], seq)
return torch.tensor(idxs, dtype=torch.long)
def log_sum_exp(vec):
max_score = vec[0, argmax(vec)]
max_score_broadcast = max_score.view(1, -1).expand(1, vec.size()[1])
return max_score + \
torch.log(torch.sum(torch.exp(vec - max_score_broadcast)))
class BiLSTM_CRF(nn.Module):
def __init__(self, vocab_size, tag_to_ix, embedding_dim, hidden_dim, tag_count, tag_embedding_size):
super(BiLSTM_CRF, self).__init__()
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.vocab_size = vocab_size
self.tag_to_ix = tag_to_ix
self.tagset_size = len(tag_to_ix)
self.word_embeds = nn.Embedding(vocab_size, embedding_dim)
self.pos_embeds = nn.Embedding(tag_count, tag_embedding_size)
self.lstm = nn.LSTM(embedding_dim + tag_embedding_size, hidden_dim // 2,
num_layers=1, bidirectional=True)
self.hidden2tag = nn.Linear(hidden_dim, self.tagset_size)
self.transitions = nn.Parameter(
torch.randn(self.tagset_size, self.tagset_size))
self.transitions.data[tag_to_ix[START_TAG], :] = -10000
self.transitions.data[:, tag_to_ix[STOP_TAG]] = -10000
self.transitions.data[1][2] = -100000
self.transitions.data[2][0] = -100000
self.transitions.data[1][1] = -100000
self.transitions.data[2][tag_to_ix[START_TAG]] = -100000
self.hidden = self.init_hidden()
def init_hidden(self):
return (torch.randn(2, 1, self.hidden_dim // 2),
torch.randn(2, 1, self.hidden_dim // 2))
def _forward_alg(self, feats):
init_alphas = torch.full((1, self.tagset_size), -10000.)
init_alphas[0][self.tag_to_ix[START_TAG]] = 0.
forward_var = init_alphas
for feat in feats:
alphas_t = []
for next_tag in range(self.tagset_size):
emit_score = feat[next_tag].view(
1, -1).expand(1, self.tagset_size)
trans_score = self.transitions[next_tag].view(1, -1)
next_tag_var = forward_var + trans_score + emit_score
alphas_t.append(log_sum_exp(next_tag_var).view(1))
forward_var = torch.cat(alphas_t).view(1, -1)
terminal_var = forward_var + self.transitions[self.tag_to_ix[STOP_TAG]]
alpha = log_sum_exp(terminal_var)
return alpha
def _get_lstm_features(self, sentence, sentence_pos):
self.hidden = self.init_hidden()
embeds = self.word_embeds(sentence).view(len(sentence), 1, -1)
pos_embeds_out = self.pos_embeds(sentence_pos).view(len(sentence), 1, -1)
tot_embedding = torch.cat((embeds, pos_embeds_out), dim=2)
lstm_out, self.hidden = self.lstm(tot_embedding, self.hidden)
lstm_out = lstm_out.view(len(sentence), self.hidden_dim)
lstm_feats = self.hidden2tag(lstm_out)
return lstm_feats
def _score_sentence(self, feats, tags):
score = torch.zeros(1)
tags = torch.cat([torch.tensor([self.tag_to_ix[START_TAG]], dtype=torch.long), tags])
for i, feat in enumerate(feats):
score = score + \
self.transitions[tags[i + 1], tags[i]] + feat[tags[i + 1]]
score = score + self.transitions[self.tag_to_ix[STOP_TAG], tags[-1]]
return score
def _viterbi_decode(self, feats):
backpointers = []
init_vvars = torch.full((1, self.tagset_size), -10000.)
init_vvars[0][self.tag_to_ix[START_TAG]] = 0
forward_var = init_vvars
for feat in feats:
bptrs_t = []
viterbivars_t = []
for next_tag in range(self.tagset_size):
next_tag_var = forward_var + self.transitions[next_tag]
best_tag_id = argmax(next_tag_var)
bptrs_t.append(best_tag_id)
viterbivars_t.append(next_tag_var[0][best_tag_id].view(1))
forward_var = (torch.cat(viterbivars_t) + feat).view(1, -1)
backpointers.append(bptrs_t)
terminal_var = forward_var + self.transitions[self.tag_to_ix[STOP_TAG]]
best_tag_id = argmax(terminal_var)
path_score = terminal_var[0][best_tag_id]
best_path = [best_tag_id]
for bptrs_t in reversed(backpointers):
best_tag_id = bptrs_t[best_tag_id]
best_path.append(best_tag_id)
start = best_path.pop()
assert start == self.tag_to_ix[START_TAG]
best_path.reverse()
return path_score, best_path
def neg_log_likelihood(self, sentence, sentence_pos, tags):
feats = self._get_lstm_features(sentence, sentence_pos)
forward_score = self._forward_alg(feats)
gold_score = self._score_sentence(feats, tags)
return forward_score - gold_score
def forward(self, sentence, sentence_pos):
lstm_feats = self._get_lstm_features(sentence, sentence_pos)
score, tag_seq = self._viterbi_decode(lstm_feats)
return score, tag_seq
# load word2id
with open('word2id.pickle', 'rb') as f:
word2id = pickle.load(f)
# load pos2id
with open('pos2id.pickle', 'rb') as f:
pos2id = pickle.load(f)
# load the trained model
model = torch.load("best_model_lstm_crf_pos.pt")
sentence = sys.argv[1]
# tokenize the sentence
sentence = word_tokenize(sentence)
# keep original sentence tokens to get the output
original_sentence = sentence
# obtain the pos tags
sentence_pos = map(lambda tag: pos2id[tag] if tag in pos2id else pos2id['<unk>'], [x[1] for x in pos_tag(sentence)])
sentence_pos = torch.tensor(sentence_pos, dtype=torch.long)
START_TAG = "<START>"
STOP_TAG = "<STOP>"
with torch.no_grad():
sentence = prepare_sequence(sentence, word2id)
_, tags = model(sentence, sentence_pos)
# print the aspect terms to the stdout
aspects = []
i = 0
while(i < len(sentence)):
if tags[i] == 1:
aspect = original_sentence[i]
i += 1
while(i < len(sentence) and tags[i]==2):
aspect += " " + original_sentence[i]
i += 1
aspects.append(aspect)
else:
i += 1
print(aspects)