-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
146 lines (113 loc) · 5.24 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
import torch
import argparse
import random
from torch import optim
import torch.nn as nn
from tqdm import tqdm
import numpy as np
from sklearn.metrics import classification_report
from text_processing import get_model, get_wordembedding
from lstms import SequenceTagger
from utils import get_features, load_datasamples, get_processed_datasamples, feature_to_idx, get_evaluation_result
def main(args):
if torch.cuda.is_available():
device = torch.device('cuda:0')
print("---Running on GPU.")
else:
device = torch.device('cpu')
print("---Running on CPU.")
# load & preprocess dataset
print("---Initiating Dataset Loading & Pre-processing.")
train_dataset, test_dataset = load_datasamples(args.subtask)
train_dataset = get_processed_datasamples(train_dataset[:200], args.subtask)
test_dataset = get_processed_datasamples(test_dataset[:200], args.subtask)
train_datasamples = get_features(train_dataset, args.nlp_pipeline, args.subtask, device)
test_datasamples = get_features(test_dataset, args.nlp_pipeline, args.subtask, device)
pos_datasample = np.concatenate((train_datasamples[:,1], test_datasamples[:,1]))
pos_to_ix, r_pos_to_ix, tag_to_ix, r_tag_to_ix = feature_to_idx(pos_datasample, args.subtask)
print("---Done Dataset Loading & Pre-processing.")
# get ELMo word embedding model
print("---Initiating Word embedding (ELMo).")
embedding_model = get_model()
train_set = get_wordembedding(embedding_model, train_datasamples[:,0])
test_set = get_wordembedding(embedding_model, test_datasamples[:,0])
print("---Done Word embedding (ELMo).")
# model
emb_dim = train_set[0].size()[2]
model = SequenceTagger(input_dim=emb_dim, hidden_dim=emb_dim, pos_to_ix=pos_to_ix, tag_to_ix=tag_to_ix, subtask=args.subtask).to(device)
loss_function = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=args.learning_rate)
print("---Initiating training process.")
# Subtask 1
if args.subtask == "1":
# training
for epoch in range(args.num_epochs):
model.train()
for idx in tqdm(range(len(train_set))):
model.zero_grad()
rnd = random.random()
returning_layer = round(rnd)
pred = model(train_set[idx], returning_layer)
# predicting POS tags
if returning_layer == 0:
pred = pred.reshape(-1, len(pos_to_ix))
cur_target = torch.tensor([r_pos_to_ix[x] for x in train_datasamples[idx,1]])
loss = loss_function(pred, cur_target)
else:
pred = pred.reshape(1, len(tag_to_ix))
cur_target = torch.tensor([train_datasamples[idx,2]])
loss = loss_function(pred, cur_target)
loss.backward(retain_graph=True)
optimizer.step()
# evaluation
model.eval()
prediction_list = []
gold_list = []
for idx in range(len(test_set)):
pred = model(test_set[idx], 1)
pred = pred.reshape(-1, len(tag_to_ix))
pred = np.argmax(pred.detach().numpy())
prediction_list.append(pred)
gold_list.append(test_datasamples[idx,2])
print("Subtask 1 result")
print(classification_report(gold_list, prediction_list))
# Subtask 2
else:
# training
for epoch in range(args.num_epochs):
model.train()
for idx in tqdm(range(len(train_set))):
model.zero_grad()
rnd = random.random()
returning_layer = round(rnd)
pred = model(train_set[idx], returning_layer)
# predicting POS tags
if returning_layer == 0:
pred = pred.reshape(-1, len(pos_to_ix))
cur_target = torch.tensor([r_pos_to_ix[x] for x in train_datasamples[idx,1]])
loss = loss_function(pred, cur_target)
else:
pred = pred.reshape(-1, len(tag_to_ix))
cur_target = torch.tensor([r_tag_to_ix[x] for x in train_datasamples[idx,2]])
loss = loss_function(pred, cur_target)
loss.backward(retain_graph=True)
optimizer.step()
# evaluation
model.eval()
prediction_list = []
gold_list = []
for idx in range(len(test_set)):
pred = model(test_set[idx], 1)
pred = pred.reshape(-1, len(tag_to_ix))
pred = [tag_to_ix [x] for x in np.argmax(pred.detach().numpy(), axis=1)]
prediction_list.append(pred)
gold_list.append(test_datasamples[idx,2])
get_evaluation_result(gold_list, prediction_list)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--nlp_pipeline", default="spacy", type=str, help="NLP preprocessing pipeline.")
parser.add_argument("--subtask", default="1", help="Selection of subtask (1 or 2).")
parser.add_argument("--learning_rate", default=1e-4, help="Learning rate.")
parser.add_argument("--num_epochs", default=10, help="Number of epochs for training.")
args = parser.parse_args()
main(args)