/
main.py
186 lines (145 loc) · 5.81 KB
/
main.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
import pandas as pd
import torch
from torch import nn
from torch.utils.data import DataLoader
from model import LangModel
from text_dataset import TextDataset
import pickle
from enum import Enum
torch.manual_seed(420)
device = "cuda" if torch.cuda.is_available() else "cpu"
langs = ['deu', 'eng', 'fra', 'ita', 'por', 'spa']
lang2label = {'deu': 0, 'eng': 1, 'fra': 2,
'ita': 3, 'por': 4, 'spa': 5}
class Mode(Enum):
TRAIN = 1
EVAL = 2
TEST = 3
PREDICT = 4
def train(dataloader, model, loss_function, optimizer):
size = len(dataloader.dataset)
model.train()
for batchnum, batch in enumerate(dataloader):
X, y = batch['data'].to(device), batch['label'].to(device)
pred = model(X)
loss = loss_function(pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# print every 100 batches
if batchnum % 100 == 0:
loss, current = loss.item(), batchnum * len(X)
print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
def validate(dataloader, model, loss_function, test=False):
size = len(dataloader.dataset)
num_batches = len(dataloader)
model.eval()
test_loss, correct = 0, 0
predictions = []
labels = []
with torch.no_grad():
for batch in dataloader:
X, y = batch['data'].to(device), batch['label'].to(device)
pred = model(X)
test_loss += loss_function(pred, y).item()
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
predictions.extend(pred.argmax(1).tolist())
labels.extend(y.tolist())
test_loss /= num_batches
correct /= size
accuracy = 100*correct
print(f"{'Test' if test else 'Validation'} Error: \n Accuracy: {accuracy:>0.1f}%, Avg loss: {test_loss:>8f} \n")
return predictions, labels
def predict(dataloader, model):
predictions = []
model.eval()
with torch.no_grad():
for batch in dataloader:
X = batch['data'].to(device)
pred = model(X)
predictions.extend([langs[p] for p in pred.argmax(1).tolist()])
return predictions
def predict_pipeline(model, data):
"""
:param model: a trained LangModel
:param data: either path to *.txt file or list of sentences
:return: list of predictions e.g. ['deu', 'eng', ...]
"""
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.preprocessing import MinMaxScaler
# load vectorizer
with open('checkpoints/vectorizer.pkl', 'rb') as f:
vectorizer: CountVectorizer = pickle.load(f)
# load scaler
with open('checkpoints/scaler.pkl', 'rb') as f:
scaler: MinMaxScaler = pickle.load(f)
# load text data
if not isinstance(data, list):
with open(data, "r") as f:
data = f.readlines()
labels = [0 for i in range(len(data))]
# preprocess data
features = vectorizer.fit_transform(data)
features = scaler.transform(features.toarray())
# create dataloader
dataloader = DataLoader(TextDataset(features, labels), batch_size=100)
# predict
predictions = predict(dataloader, model)
# print output
for text, prediction in zip(data, predictions):
print(f'Language: {prediction} | Input: {text.strip()}')
return predictions
def main(mode: Mode = Mode.TRAIN, checkpoint: str = None, data: str = "data/processed.pkl"):
# hyper parameters
batch_size = 100
# init model
model = LangModel().to(device)
if checkpoint is not None:
model.load_state_dict(torch.load(checkpoint))
# init loss
loss_function = nn.CrossEntropyLoss()
# load data
if mode == Mode.PREDICT:
predict_pipeline(model, data)
else:
# load preprocessed data (train, val, test) from file
with open(data, 'rb') as f:
data = pickle.load(f)
if mode == Mode.TRAIN:
# create dataloaders
train_dataloader = DataLoader(TextDataset(*data['train']), batch_size=batch_size, shuffle=True)
val_dataloader = DataLoader(TextDataset(*data['val']), batch_size=batch_size)
# init optimizer
optimizer = torch.optim.Adam(model.parameters())
# training loop
epochs = 5
for t in range(epochs):
print(f"Epoch {t+1}\n-------------------------------")
train(train_dataloader, model, loss_function, optimizer)
validate(val_dataloader, model, loss_function)
print("Done training!")
# save model
torch.save(model.state_dict(), "checkpoints/model.pth")
print("Saved PyTorch Model State to checkpoints/model.pth")
elif mode == Mode.EVAL or mode == Mode.TEST:
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pylab as plt
import seaborn as sns
dataloader = DataLoader(TextDataset(*data['val' if mode == Mode.EVAL else 'test']), batch_size=batch_size)
predictions, labels = validate(dataloader, model, loss_function, test=mode == Mode.TEST)
print(f'Accuracy: {accuracy_score(labels, predictions) * 100:.2f}%')
matrix = pd.DataFrame(data=confusion_matrix(labels, predictions), columns=langs, index=langs)
print(matrix)
sns.set(font_scale=1.2)
ax = sns.heatmap(matrix, cmap='coolwarm', annot=True, fmt='.5g', cbar=False)
ax.xaxis.set_ticks_position('top')
ax.xaxis.set_label_position('top')
plt.xlabel('Predicted', fontsize=22)
plt.ylabel('Actual', fontsize=22)
plt.show()
print("Done!")
if __name__ == '__main__':
# main(mode=Mode.TRAIN, data="data/processed.pkl")
# main(mode=Mode.EVAL, checkpoint="checkpoints/model.pth")
main(mode=Mode.TEST, checkpoint="checkpoints/model.pth")
# main(mode=Mode.PREDICT, checkpoint="checkpoints/model.pth", data="data/input.txt")