-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.py
304 lines (266 loc) · 10.6 KB
/
pipeline.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from NLarge.model.RNN import TextClassifierRNN
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.utils.rnn as rnn_utils
import numpy as np
import collections
import matplotlib.pyplot as plt
import gensim.downloader as api
from gensim.corpora import Dictionary
from nltk.tokenize import word_tokenize
import tqdm
class TextClassificationPipeline:
def __init__(
self,
augmented_data,
test_data,
max_length,
test_size,
batch_size=512,
embedding_dim=300,
hidden_dim=300,
n_layers=2,
bidirectional=True,
dropout_rate=0.5,
lr=5e-4,
model_class: type = TextClassifierRNN,
):
# Set initial attributes
self.augmented_data = augmented_data
self.test_data = test_data
self.max_length = max_length
self.test_size = test_size
self.batch_size = batch_size
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.bidirectional = bidirectional
self.dropout_rate = dropout_rate
self.lr = lr
self.model_class = model_class
# Initialize device
self.device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
)
# Prepare data and model
self._prepare_data()
self._build_model()
def tokenize_example(self, example, max_length):
tokens = word_tokenize(example["text"])[:max_length]
length = len(tokens)
return {"tokens": tokens, "length": length}
def build_vocab(self, data, min_count=5):
"""Builds a vocabulary from a list of sentences.
Args:
data: A list of sentences.
min_count: The minimum frequency for a word to be included in the vocabulary.
Returns:
A gensim Dictionary object.
"""
dictionary = Dictionary(data)
dictionary.filter_extremes(no_below=min_count, no_above=1.0)
dictionary.add_documents([["<unk>", "<pad>"]])
return dictionary
def numericalize_example(self, example, vocab):
doc_bow = vocab.doc2bow(example["tokens"])
ids = [id for id, _ in doc_bow]
return {"ids": ids}
def get_collate_fn(self, pad_index):
def collate_fn(batch):
batch_ids = [i["ids"] for i in batch]
batch_ids = nn.utils.rnn.pad_sequence(
batch_ids, padding_value=pad_index, batch_first=True
)
batch_length = [i["length"] for i in batch]
batch_length = torch.stack(batch_length)
batch_label = [i["label"] for i in batch]
batch_label = torch.stack(batch_label)
batch = {
"ids": batch_ids,
"length": batch_length,
"label": batch_label,
}
return batch
return collate_fn
def get_data_loader(self, dataset, batch_size, pad_index, shuffle=False):
collate_fn = self.get_collate_fn(pad_index)
data_loader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
collate_fn=collate_fn,
shuffle=shuffle,
)
return data_loader
def train(self, dataloader, model, criterion, optimizer, device="cpu"):
if self.device:
device = self.device
model.train()
epoch_losses = []
epoch_accs = []
for batch in tqdm.tqdm(dataloader, desc="training..."):
ids = batch["ids"].to(device)
length = batch["length"]
length = torch.clamp(length, max=ids.size(1))
label = batch["label"].to(device)
prediction = model(ids, length)
loss = criterion(prediction, label)
accuracy = self.get_accuracy(prediction, label)
optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch_losses.append(loss.item())
epoch_accs.append(accuracy.item())
return np.mean(epoch_losses), np.mean(epoch_accs)
def evaluate(self, dataloader, model, criterion, device="cpu"):
if self.device:
device = self.device
model.eval()
epoch_losses = []
epoch_accs = []
with torch.no_grad():
for batch in tqdm.tqdm(dataloader, desc="evaluating..."):
ids = batch["ids"].to(device)
length = batch["length"]
length = torch.clamp(length, max=ids.size(1))
label = batch["label"].to(device)
prediction = model(ids, length)
loss = criterion(prediction, label)
accuracy = self.get_accuracy(prediction, label)
epoch_losses.append(loss.item())
epoch_accs.append(accuracy.item())
return np.mean(epoch_losses), np.mean(epoch_accs)
def get_accuracy(self, prediction, label):
batch_size, _ = prediction.shape
predicted_classes = prediction.argmax(dim=-1)
correct_predictions = predicted_classes.eq(label).sum()
accuracy = correct_predictions / batch_size
return accuracy
def _prepare_data(self):
# Tokenize augmented dataset
self.augmented_data = self.augmented_data.map(
self.tokenize_example, fn_kwargs={"max_length": self.max_length}
)
self.test_data = self.test_data.map(
self.tokenize_example, fn_kwargs={"max_length": self.max_length}
)
# Split train and validation data
train_valid_split = self.augmented_data.train_test_split(
test_size=self.test_size
)
self.train_data = train_valid_split["train"]
self.valid_data = train_valid_split["test"]
# Build vocabulary
min_freq = 5
self.vocab = self.build_vocab(self.train_data["tokens"], min_freq)
self.vocab.compactify()
unk_index = self.vocab.token2id.get("<unk>")
pad_index = self.vocab.token2id.get("<pad>")
self.vocab.default_index = unk_index
# Numericalize datasets
self.train_data = self.train_data.map(
self.numericalize_example, fn_kwargs={"vocab": self.vocab}
)
self.valid_data = self.valid_data.map(
self.numericalize_example, fn_kwargs={"vocab": self.vocab}
)
self.test_data = self.test_data.map(
self.numericalize_example, fn_kwargs={"vocab": self.vocab}
)
# Set data format for PyTorch
self.train_data = self.train_data.with_format(
type="torch", columns=["ids", "label", "length"]
)
self.valid_data = self.valid_data.with_format(
type="torch", columns=["ids", "label", "length"]
)
self.test_data = self.test_data.with_format(
type="torch", columns=["ids", "label", "length"]
)
# Create data loaders
self.train_data_loader = self.get_data_loader(
self.train_data, self.batch_size, pad_index, shuffle=True
)
self.valid_data_loader = self.get_data_loader(
self.valid_data, self.batch_size, pad_index
)
self.test_data_loader = self.get_data_loader(
self.test_data, self.batch_size, pad_index
)
def _build_model(self):
# Initialize model
vocab_size = len(self.vocab)
output_dim = len(self.train_data.unique("label"))
# Initialize word embeddings
word_vectors = api.load("glove-wiki-gigaword-300")
words_in_vocab = list(self.vocab.token2id.keys())
pretrained_embedding = torch.zeros(
len(self.vocab), word_vectors.vector_size
).to(self.device)
for i, word in enumerate(words_in_vocab):
if word in word_vectors:
pretrained_embedding[i] = torch.tensor(word_vectors[word])
# Define the possible arguments for the model
model_args = {
'vocab_size': vocab_size,
'embedding_dim': self.embedding_dim,
'hidden_dim': self.hidden_dim,
'output_dim': output_dim,
'n_layers': self.n_layers,
'bidirectional': self.bidirectional,
'dropout': self.dropout_rate,
'pretrained_embedding': pretrained_embedding
}
# Filter the arguments to only include those relevant to the model_class
model_class_args = {k: v for k, v in model_args.items() if k in self.model_class.__init__.__code__.co_varnames}
# Instantiate the model using the provided model_class
self.model = self.model_class(**model_class_args).to(self.device)
# Set optimizer and loss criterion
self.optimizer = optim.Adam(self.model.parameters(), lr=self.lr)
self.criterion = nn.CrossEntropyLoss()
def train_model(self, n_epochs=10):
self.metrics = collections.defaultdict(list)
best_valid_loss = float("inf")
for epoch in range(n_epochs):
train_loss, train_acc = self.train(
self.train_data_loader,
self.model,
self.criterion,
self.optimizer,
self.device,
)
valid_loss, valid_acc = self.evaluate(
self.valid_data_loader, self.model, self.criterion, self.device
)
self.metrics["train_losses"].append(train_loss)
self.metrics["train_accs"].append(train_acc)
self.metrics["valid_losses"].append(valid_loss)
self.metrics["valid_accs"].append(valid_acc)
if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(self.model.state_dict(), "best_model.pt")
print(f"Epoch: {epoch + 1}/{n_epochs}")
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 plot_loss(self, title=""):
plt.figure(figsize=(10, 6))
plt.title(title)
plt.plot(self.metrics["train_losses"], label="Train Loss")
plt.plot(self.metrics["valid_losses"], label="Validation Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.xticks(range(len(self.metrics["train_losses"])))
plt.legend()
plt.grid()
plt.show()
def plot_acc(self, title=""):
plt.figure(figsize=(10, 6))
plt.title(title)
plt.plot(self.metrics["train_accs"], label="Train Acc")
plt.plot(self.metrics["valid_accs"], label="Validation Acc")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.xticks(range(len(self.metrics["train_accs"])))
plt.legend()
plt.grid()
plt.show()