-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimdb_sentiment.py
275 lines (236 loc) · 10.9 KB
/
imdb_sentiment.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
import collections
import re
import matplotlib.pyplot as plt
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
import torchtext
import tqdm
from torch.utils.tensorboard import SummaryWriter
from wordcloud import WordCloud
from Data import IMDB_data, get_data_loader
from Networks.networks import GRU, LSTM, RNN
class IMDBSentimentAnalyzer:
def __init__(self, config):
"""
Initialize the IMDBSentimentAnalyzer with the given configuration.
Parameters:
- config (dict): Configuration dictionary containing model, data, and training parameters.
"""
self.config = config
self._setup_data()
self._setup_model()
self._initialize_weights()
self._setup_pretrained_embeddings()
self._setup_training_tools()
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = self.model.to(self.device)
self.criterion = self.criterion.to(self.device)
self.writer = SummaryWriter()
def _setup_model(self):
"""Set up the model based on the configuration."""
model_name = self.config["model"]
model_config = self.config["model_config"]
# Add common parameters to the model configuration
model_config.update(
{"vocab_size": len(self.vocab), "pad_index": self.pad_index}
)
# Initialize the model based on the specified name
if model_name == "RNN":
self.model = RNN(**model_config)
elif model_name == "LSTM":
self.model = LSTM(**model_config)
elif model_name == "GRU":
self.model = GRU(**model_config)
else:
raise ValueError("Model should be either 'RNN', 'LSTM', or 'GRU'!")
def _setup_data(self):
"""Set up data loaders based on the configuration."""
data_config = self.config["data_config"]
batch_size = self.config["batch_size"]
data = IMDB_data(**data_config)
self.train_data, self.valid_data, self.test_data, self.vocab, self.tokenizer = (
data.get_data()
)
self.pad_index = data.pad_index
self.train_loader = get_data_loader(
self.train_data, batch_size, self.pad_index, shuffle=True
)
self.valid_loader = get_data_loader(self.valid_data, batch_size, self.pad_index)
self.test_loader = get_data_loader(self.test_data, batch_size, self.pad_index)
def _initialize_weights(self):
"""Initialize model weights if specified in the configuration."""
if self.config.get("initialize_weights", False):
self.model.apply(self._init_weights)
def _setup_pretrained_embeddings(self):
"""Set up pretrained embeddings if specified in the configuration."""
vectors = torchtext.vocab.GloVe()
pretrained_embeddings = vectors.get_vecs_by_tokens(self.vocab.get_itos())
self.model.embedding.weight.data = pretrained_embeddings
def _setup_training_tools(self):
"""Set up optimizer and loss criterion for training."""
self.optimizer = optim.Adam(self.model.parameters(), lr=self.config["lr"])
self.criterion = nn.CrossEntropyLoss()
def _init_weights(self, m):
"""Initialize weights of the model."""
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
nn.init.zeros_(m.bias)
elif isinstance(m, nn.LSTM):
for name, param in m.named_parameters():
if "bias" in name:
nn.init.zeros_(param)
elif "weight" in name:
nn.init.orthogonal_(param)
def train_epoch(self, dataloader, epoch):
"""Train the model for one epoch."""
self.model.train()
epoch_losses = []
epoch_accs = []
for batch in tqdm.tqdm(dataloader, desc=f"Training epoch {epoch+1}"):
ids = batch["ids"].to(self.device)
lengths = batch["length"]
labels = batch["label"].to(self.device)
predictions = self.model(ids, lengths)
loss = self.criterion(predictions, labels)
accuracy = self._get_accuracy(predictions, labels)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
epoch_losses.append(loss.item())
epoch_accs.append(accuracy)
return torch.mean(torch.tensor(epoch_losses)), torch.mean(
torch.tensor(epoch_accs)
)
def evaluate_epoch(self, dataloader):
"""Evaluate the model for one epoch."""
self.model.eval()
epoch_losses = []
epoch_accs = []
with torch.no_grad():
for batch in tqdm.tqdm(dataloader, desc=f"Evaluating"):
ids = batch["ids"].to(self.device)
lengths = batch["length"]
labels = batch["label"].to(self.device)
predictions = self.model(ids, lengths)
loss = self.criterion(predictions, labels)
accuracy = self._get_accuracy(predictions, labels)
epoch_losses.append(loss.item())
epoch_accs.append(accuracy)
return torch.mean(torch.tensor(epoch_losses)), torch.mean(
torch.tensor(epoch_accs)
)
def train_and_evaluate(self):
"""Train and evaluate the model for a specified number of epochs."""
num_epochs = self.config["num_epochs"]
best_valid_loss = float("inf")
metrics = collections.defaultdict(list)
model_name = self.config["model"]
for epoch in range(num_epochs):
train_loss, train_acc = self.train_epoch(self.train_loader, epoch)
valid_loss, valid_acc = self.evaluate_epoch(self.valid_loader)
metrics["train_losses"].append(train_loss)
metrics["train_accs"].append(train_acc)
metrics["valid_losses"].append(valid_loss)
metrics["valid_accs"].append(valid_acc)
# Log metrics to TensorBoard
self.writer.add_scalar("Loss/Train", train_loss, epoch)
self.writer.add_scalar("Accuracy/Train", train_acc, epoch)
self.writer.add_scalar("Loss/Valid", valid_loss, epoch)
self.writer.add_scalar("Accuracy/Valid", valid_acc, epoch)
if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(self.model.state_dict(), f"{model_name}.pt")
print(f"Epoch: {epoch + 1}")
print(f"Train Loss: {train_loss:.3f}, Train Acc: {train_acc:.3f}")
print(f"Valid Loss: {valid_loss:.3f}, Valid Acc: {valid_acc:.3f}")
def test_model(self):
"""Test the trained model on the test dataset"""
model_name = self.config["model"]
try:
# Load the trained model state
self.model.load_state_dict(torch.load(f"{model_name}.pt"))
self.model.to(self.device)
self.model.eval()
# Evaluate the model on the test dataset
test_loss, test_acc = self.evaluate_epoch(self.test_loader)
# Print the results
print(f"Test Loss: {test_loss:.3f}, Test Acc: {test_acc:.3f}")
except FileNotFoundError:
print(f"Error: Model file '{model_name}.pt' not found.")
except Exception as e:
print(f"An error occurred while testing the model: {e}")
def predict_sentiment(self, text):
"""Predict the sentiment of a given text using the trained model"""
model_name = self.config["model"]
try:
# Load the trained model state
self.model.load_state_dict(torch.load(f"{model_name}.pt"))
self.model.to(self.device)
self.model.eval()
# Tokenize and preprocess the input text
tokens = self.tokenizer(text)
token_ids = self.vocab.lookup_indices(tokens)
token_length = torch.LongTensor([len(token_ids)])
# Prepare the input tensor
input_tensor = torch.LongTensor(token_ids).unsqueeze(dim=0).to(self.device)
# Get the model's prediction
prediction = self.model(input_tensor, token_length).squeeze(dim=0)
probability = torch.softmax(prediction, dim=-1)
# Get the predicted class and its probability
predicted_class_idx = prediction.argmax(dim=-1).item()
predicted_probability = probability[predicted_class_idx].item()
predicted_class = "Negative" if predicted_class_idx == 0 else "Positive"
print(
f"{model_name}:\n\tPredicted class: {predicted_class}\n\tProbability: {predicted_probability:.3f}"
)
except FileNotFoundError:
print(f"Error: Model file '{model_name}.pt' not found.")
except Exception as e:
print(f"An error occurred while predicting sentiment: {e}")
@staticmethod
def _get_accuracy(predictions, labels):
"""Calculate accuracy of the predictions."""
batch_size = predictions.shape[0]
predicted_classes = predictions.argmax(dim=-1)
correct_predictions = predicted_classes.eq(labels).sum().item()
accuracy = correct_predictions / batch_size
return accuracy
def count_parameters(self):
"""Count the number of trainable parameters in the model."""
return sum(
param.numel() for param in self.model.parameters() if param.requires_grad
)
def visualize(self):
"""Visualize word clouds for positive and negative reviews in the training data."""
data = {"text": self.train_data["text"], "label": self.train_data["label"]}
df = pd.DataFrame(data)
df["text"] = df["text"].apply(self._clean_text)
# Positive reviews word cloud
positive_reviews = df[df["label"] == 1]["text"]
self._generate_wordcloud(positive_reviews, "Wordcloud for Positive Reviews")
# Negative reviews word cloud
negative_reviews = df[df["label"] == 0]["text"]
self._generate_wordcloud(negative_reviews, "Wordcloud for Negative Reviews")
@staticmethod
def _clean_text(text):
"""Clean text by removing HTML tags, URLs, special characters, punctuation, and extra whitespace."""
text = text.lower()
text = re.sub(r"<.*?>", "", text)
text = re.sub(r"http\S+|www\S+|https\S+", "", text)
text = re.sub(r"[^a-z\s]", "", text)
text = re.sub(r"\s+", " ", text).strip()
return text
@staticmethod
def _generate_wordcloud(text_series, title):
"""Generate and display a word cloud from a series of texts."""
text = " ".join(text_series.astype(str))
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(
text
)
plt.figure(figsize=(10, 5))
plt.title(title)
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()