-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_analysis_LSTM.py
279 lines (207 loc) · 11.1 KB
/
sentiment_analysis_LSTM.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
###############################################################################
####################### 1. LOAD THE TRAINING TEXT ###########################
###############################################################################
with open("data/reviews.txt") as f:
reviews = f.read()
with open("data/labels.txt") as f:
labels = f.read()
###############################################################################
########################## 2. TEXT PRE-PROCESSING ###########################
###############################################################################
from string import punctuation
def preprocess(text):
text = text.lower()
text = "".join([ch for ch in text if ch not in punctuation])
all_reviews = text.split("\n")
text = " ".join(text)
all_words = text.split()
return all_reviews, all_words
all_reviews, all_words = preprocess(reviews)
###############################################################################
################## 3. CREATE DICTIONARIES & ENCODE REVIEWS ##################
###############################################################################
from collections import Counter
word_counts = Counter(all_words)
word_list = sorted(word_counts, keys = word_counts.get, reverse = True)
vocab_to_int = {word:idx+1 for idx, word in enumerate(word_list)}
int_to_vocab = {idx:word for word, idx in vocab_to_int.items()}
encoded_reviews = [[vocab_to_int[word] for word in review] for review in all_reviews]
###############################################################################
############################# 4. ENCODE LABELS ###############################
###############################################################################
all_labels = labels.split("\n")
encoded_labels = [1 if label == "positive" else 0 for label in all_labels]
assert len(encoded_reviews) == len(encoded_labels), "# of encoded reivews & encoded labels must be the same!"
###############################################################################
##################### 5. GET RID OF LENGTH-0 REVIEWS #######################
###############################################################################
import numpy as np
import torch
encoded_labels = np.array( [label for idx, label in enumerate(encoded_labels) if len(encoded_reviews[idx]) > 0] )
encoded_reviews = [review for review in encoded_reviews if len(review) > 0]
###############################################################################
###################### 6. MAKE ALL REVIEWS SAME LENGTH #######################
###############################################################################
def pad_text(encoded_reviews, seq_length):
reviews = []
for review in encoded_reviews:
if len(review) >= seq_length:
reviews.append(review[:seq_length])
else:
reviews.append([0]*(seq_length-len(review)) + review)
return np.array(reviews)
padded_reviews = pad_text(encoded_reviews, seq_length = 200)
###############################################################################
############## 7. SPLIT DATA & GET (REVIEW, LABEL) DATALOADER ###############
###############################################################################
train_ratio = 0.8
valid_ratio = (1 - train_ratio)/2
total = padded_reviews.shape[0]
train_cutoff = int(total * train_ratio)
valid_cutoff = int(total * (1 - valid_ratio))
train_x, train_y = padded_reviews[:train_cutoff], encoded_labels[:train_cutoff]
valid_x, valid_y = padded_reviews[:train_cutoff : valid_cutoff], encoded_labels[train_cutoff : valid_cutoff]
test_x, test_y = padded_reviews[valid_cutoff:], encoded_labels[valid_cutoff:]
from torch.utils.data import TensorDataset, DataLoader
train_data = TensorDataset(train_x, train_y)
valid_data = TensorDataset(valid_x, valid_y)
test_data = TensorDataset(test_x, test_y)
batch_size = 50
train_loader = DataLoader(train_data, batch_size = batch_size, shuffle = True)
valid_loader = DataLoader(valid_data, batch_size = batch_size, shuffle = True)
test_loader = DataLoader(test_data, batch_size = batch_size, shuffle = True)
###############################################################################
######################### 8. DEFINE THE LSTM MODEL ##########################
###############################################################################
from torch import nn
class SentimentLSTM(nn.Module):
def __init__(self, n_vocab, n_embed, n_hidden, n_output, n_layers, drop_p = 0.5):
super().__init__()
# params: "n_" means dimension
self.n_vocab = n_vocab # number of unique words in vocabulary
self.n_layers = n_layers # number of LSTM layers
self.n_hidden = n_hidden # number of hidden nodes in LSTM
self.embedding = nn.Embedding(n_vocab, n_embed)
self.lstm = nn.LSTM(n_embed, n_hidden, n_layers, batch_first = True, dropout = drop_p)
self.dropout = nn.Dropout(drop_p)
self.fc = nn.Linear(n_hidden, n_output)
self.sigmoid = nn.Sigmoid()
def forward (self, input_words):
# INPUT : (batch_size, seq_length)
embedded_words = self.embedding(input_words) # (batch_size, seq_length, n_embed)
lstm_out, h = self.lstm(embedded_words) # (batch_size, seq_length, n_hidden)
lstm_out = self.dropout(lstm_out)
lstm_out = lstm_out.contiguous().view(-1, self.n_hidden) # (batch_size*seq_length, n_hidden)
fc_out = self.fc(lstm_out) # (batch_size*seq_length, n_output)
sigmoid_out = self.sigmoid(fc_out) # (batch_size*seq_length, n_output)
sigmoid_out = sigmoid_out.view(batch_size, -1) # (batch_size, seq_length*n_output)
# extract the output of ONLY the LAST output of the LAST element of the sequence
sigmoid_last = sigmoid_out[:, -1] # (batch_size, 1)
return sigmoid_last, h
def init_hidden (self, batch_size): # initialize hidden weights (h,c) to 0
device = "cuda" if torch.cuda.is_available() else "cpu"
weights = next(self.parameters()).data
h = (weights.new(self.n_layers, batch_size, self.n_hidden).zero_().to(device),
weights.new(self.n_layers, batch_size, self.n_hidden).zero_().to(device))
return h
###############################################################################
################ 9. INSTANTIATE THE MODEL W/ HYPERPARAMETERS #################
###############################################################################
n_vocab = len(vocab_to_int)
n_embed = 400
n_hidden = 512
n_output = 1 # 1 ("positive") or 0 ("negative")
n_layers = 2
net = SentimentLSTM(n_vocab, n_embed, n_hidden, n_output, n_layers)
###############################################################################
####################### 10. DEFINE LOSS & OPTIMIZER #########################
###############################################################################
from torch import optim
criterion = nn.BCELoss()
optimizer = optim.Adam(net.parameters(), lr = 0.001)
###############################################################################
########################## 11. TRAIN THE NETWORK! ###########################
###############################################################################
print_every = 100
step = 0
n_epochs = 4 # validation loss increases from ~ epoch 3 or 4
clip = 5 # for gradient clip to prevent exploding gradient problem in LSTM/RNN
device = 'cuda' if torch.cuda.is_available() else 'cpu'
for epoch in range(n_epochs):
h = net.init_hidden(batch_size)
for inputs, labels in train_loader:
step += 1
inputs, labels = inputs.to(device), labels.to(device)
# making requires_grad = False for the latest set of h
h = tuple([each.data for each in h])
net.zero_grad()
output, h = net(inputs)
loss = criterion(output.squeeze(), labels.float())
loss.backward()
nn.utils.clip_grad_norm(net.parameters(), clip)
optimizer.step()
if (step % print_every) == 0:
######################
##### VALIDATION #####
######################
net.eval()
valid_losses = []
v_h = net.init_hidden(batch_size)
for v_inputs, v_labels in valid_loader:
v_inputs, v_labels = inputs.to(device), labels.to(device)
v_h = tuple([each.data for each in v_h])
v_output, v_h = net(v_inputs)
v_loss = criterion(v_output.squeeze(), v_labels.float())
valid_losses.append(v_loss.item())
print("Epoch: {}/{}".format((epoch+1), n_epochs),
"Step: {}".format(step),
"Training Loss: {:.4f}".format(loss.item()),
"Validation Loss: {:.4f}".format(np.mean(valid_losses)))
net.train()
###############################################################################
################ 12. TEST THE TRAINED MODEL ON THE TEST SET #################
###############################################################################
net.eval()
test_losses = []
num_correct = 0
test_h = net.init_hidden(batch_size)
for inputs, labels in test_loader:
test_h = tuple([each.data for each in test_h])
test_output, test_h = net(inputs, test_h)
loss = criterion(test_output, labels)
test_losses.append(loss.item())
preds = torch.round(test_output.squeeze())
correct_tensor = preds.eq(labels.float().view_as(preds))
correct = np.squeeze(correct_tensor.numpy())
num_correct += np.sum(correct)
print("Test Loss: {:.4f}".format(np.mean(test_losses)))
print("Test Accuracy: {:.2f}".format(num_correct/len(test_loader.dataset)))
###############################################################################
############ 13. TEST THE TRAINED MODEL ON A RANDOM SINGLE REVIEW ############
###############################################################################
def predict(net, review, seq_length = 200):
device = "cuda" if torch.cuda.is_available() else "cpu"
words = preprocess(review)
encoded_words = [vocab_to_int[word] for word in words]
padded_words = pad_text([encoded_words], seq_length)
padded_words = torch.from_numpy(padded_words).to(device)
if(len(padded_words) == 0):
"Your review must contain at least 1 word!"
return None
net.eval()
h = net.init_hidden(1)
output, h = net(padded_words, h)
pred = torch.round(output.squeeze())
msg = "This is a positive review." if pred == 0 else "This is a negative review."
return msg
review1 = "It made me cry."
review2 = "It was so good it made me cry."
review3 = "It's ok."
review4 = "This movie had the best acting and the dialogue was so good. I loved it."
review5 = "Garbage"
### OUTPUT ###
predict(net, review1) ## negative ##
predict(net, review2) ## positive ##
predict(net, review3) ## negative ##
predict(net, review4) ## positive ##
predict(net, review5) ## negative ##