-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
358 lines (293 loc) · 15.4 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import os
import argparse
import torch
import torch.nn as nn
from datasets import load_dataset
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
import pickle
import numpy as np
from scripts.preprocess import generate_masked_sentences, generate_word2idx, preprocess_data
from models import Approach1MaskPrediction, Approach1EntityClassification, Approach2, Approach3CombinedModel
def main():
torch.manual_seed(17)
# Create the argument parser
parser = argparse.ArgumentParser(description='Script for loading data and training neural networks')
# Add the command line arguments
parser.add_argument('--approach-1', action='store_true', help='Perform action on approach 1')
parser.add_argument('--approach-2', action='store_true', help='Perform action on approach 2')
parser.add_argument('--approach-3', action='store_true', help='Perform action on approach 3')
parser.add_argument('--save', action='store_true', default=True, help='Save the trained models')
parser.add_argument('--train', action='store_true', help='Train the models')
parser.add_argument('--validate', action='store_true', default=False, help='Evaluate the trained models')
parser.add_argument('--test', action='store_true', default=False, help='Test the trained models on the test set')
# Parse the arguments
args = parser.parse_args()
# Check if any of the approaches were selected
models_selected = []
if not args.approach_1 and not args.approach_2 and not args.approach_3:
# Select all approaches
models_selected = [1, 2, 3]
if args.approach_1:
# Select approach 1
models_selected.append(1)
if args.approach_2:
# Select approach 2
models_selected.append(2)
if args.approach_3:
# Select approach 3
models_selected.append(3)
# Define hyperparameters to share between all three approaches
# All these parameters are shared between models so we can compare the approaches as fairly as possible
PAD = '<PAD>'
batch_size = 32
num_epochs = 10
lr = 0.001
max_len = 32 # Length of sentence
emb_dim = 128 # The embedding dimension of each token
# Check if the models directory exists
if not os.path.exists('models'):
# Create the models directory
os.makedirs('models')
wnut = load_dataset("wnut_17")
global label_list
label_list=wnut["train"].features[f"ner_tags"].feature.names
# Preprocess data
# Check if models/data/train_data.pkl and models/data/test_data.pkl exists
if not os.path.exists('models/data'):
os.makedirs('models/data')
train_data_exists = os.path.exists('models/data/train_data.pkl')
test_data_exists = os.path.exists('models/data/test_data.pkl')
val_data_exists = os.path.exists('models/data/val_data.pkl')
# If the train data does not exist, generate it
if not train_data_exists:
train_data = generate_masked_sentences(wnut['train'])
# Save the processed data
with open('models/data/train_data.pkl', 'wb') as f:
pickle.dump(train_data, f)
else:
# Load data
with open('models/data/train_data.pkl', 'rb') as f:
train_data = pickle.load(f)
# If the test data does not exist, generate it
if not test_data_exists:
test_data = generate_masked_sentences(wnut['test'])
# Save the processed data
with open('models/data/test_data.pkl', 'wb') as f:
pickle.dump(test_data, f)
else:
# Load data
with open('models/data/test_data.pkl', 'rb') as f:
test_data = pickle.load(f)
if not val_data_exists:
val_data = generate_masked_sentences(wnut['validation'])
# Save the processed data
with open('models/data/val_data.pkl', 'wb') as f:
pickle.dump(val_data, f)
else:
# Load data
with open('models/data/val_data.pkl', 'rb') as f:
val_data = pickle.load(f)
# Load datasets
word2idx, idx2word = generate_word2idx(train_data, max_len, PAD)
# Vocab length
vocab_dim = len(idx2word)
print("Vocab length", vocab_dim)
# Get all ner_tags from wnut_17 dataset
ner_tags = wnut['train'].features['ner_tags'].feature.names
num_entities = len(ner_tags) # Number of entities including non entity
print("Number of entities", num_entities)
state = {
'PAD': PAD,
'batch_size': batch_size,
'num_epochs': num_epochs,
'lr': lr,
'max_len': max_len,
'emb_dim': emb_dim,
'num_entities': num_entities,
'vocab_dim': vocab_dim,
'word2idx': word2idx,
'save': args.save
}
if args.train:
train(models_selected, train_data, state)
if args.test:
evaluate(models_selected, test_data, state)
if args.validate:
evaluate(models_selected, val_data, state)
def train(models_to_train: list, train_data: list, state: dict):
train_sentence_feats, train_mask_labels, train_named_entity_sentence_feats, train_named_entity_data_labels, _, train_approach2_labels, train_approach_3_task_2_labels = preprocess_data(train_data, state['word2idx'], state['max_len'], state['num_entities'], state['PAD'])
# Train the models
if 1 in models_to_train:
print("Approach 1: Training mask prediction model")
approach1_mask_prediction = Approach1MaskPrediction(state['vocab_dim'], state['emb_dim'])
criterion = nn.BCELoss()
optimizer = torch.optim.Adam(approach1_mask_prediction.parameters(), lr=state['lr'])
for epoch in range(state['num_epochs']):
for i in range(0, len(train_sentence_feats), state['batch_size']):
batch_feats = train_sentence_feats[i:i+state['batch_size']]
batch_labels = train_mask_labels[i:i+state['batch_size']]
optimizer.zero_grad()
y_pred = approach1_mask_prediction(batch_feats)
loss = criterion(y_pred, batch_labels)
loss.backward()
optimizer.step()
print("Epoch: {}/{}...".format(epoch+1, state['num_epochs']),
"Loss: {:.6f}...".format(loss.item()))
# Save the model
if state['save']:
torch.save(approach1_mask_prediction.state_dict(), 'models/approach1_mask_prediction.pt')
print("Approach 1: Training entity classification model")
approach1_entity_classification = Approach1EntityClassification(state['vocab_dim'], state['emb_dim'], state['num_entities'])
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(approach1_entity_classification.parameters(), lr=state['lr'])
for epoch in range(state['num_epochs']+50):
for i in range(0, len(train_named_entity_sentence_feats), state['batch_size']):
batch_feats = train_named_entity_sentence_feats[i:i+state['batch_size']]
batch_labels = train_named_entity_data_labels[i:i+state['batch_size']]
optimizer.zero_grad()
y_pred = approach1_entity_classification(batch_feats)
loss = criterion(y_pred, batch_labels)
loss.backward()
optimizer.step()
print("Epoch: {}/{}...".format(epoch+1, state['num_epochs']),
"Loss: {:.6f}...".format(loss.item()))
# Save the model
if state['save']:
torch.save(approach1_entity_classification.state_dict(), 'models/approach1_entity_classification.pt')
if 2 in models_to_train:
print("Approach 2: Training model")
approach2model = Approach2(state['vocab_dim'], state['emb_dim'], state['num_entities'])
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(approach2model.parameters(), lr=state['lr'])
for epoch in range(state['num_epochs']):
for i in range(0, len(train_sentence_feats), state['batch_size']):
batch_feats = train_sentence_feats[i:i+state['batch_size']]
batch_labels = train_approach2_labels[i:i+state['batch_size']]
optimizer.zero_grad()
y_pred = approach2model(batch_feats)
loss = criterion(y_pred, batch_labels)
loss.backward()
optimizer.step()
print("Epoch: {}/{}...".format(epoch+1, state['num_epochs']),
"Loss: {:.6f}...".format(loss.item()))
# Save the model
if state['save']:
torch.save(approach2model.state_dict(), 'models/approach2.pt')
if 3 in models_to_train:
print("Approach 3: Training model")
combined_model = Approach3CombinedModel(state['vocab_dim'], state['emb_dim'], state['num_entities'])
criterion1 = nn.BCELoss()
criterion2 = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(combined_model.parameters(), lr=state['lr'])
for epoch in range(state['num_epochs']):
for i in range(0, len(train_sentence_feats), state['batch_size']):
batch_feats = train_sentence_feats[i:i+state['batch_size']]
batch_labels_1 = train_mask_labels[i:i+state['batch_size']] # Labels if mask is a named entity or not
batch_labels_2 = train_approach_3_task_2_labels[i:i+state['batch_size']] # Labels for what type of named entity the mask is
optimizer.zero_grad()
y_pred1, y_pred2 = combined_model(batch_feats)
loss1 = criterion1(y_pred1, batch_labels_1)
loss2 = criterion2(y_pred2, batch_labels_2)
loss = loss1 + loss2
loss.backward()
optimizer.step()
print("Epoch: {}/{}...".format(epoch+1, state['num_epochs']),
"Loss: {:.6f}...".format(loss.item()))
# Save the model
if state['save']:
torch.save(combined_model.state_dict(), 'models/approach3.pt')
def evaluate(models_to_evaluate, test_data: list, state: dict):
# Check if anything is stored in the models directory
if not os.listdir('models'):
print('No models found in models directory.')
return
test_sentence_feats, test_mask_labels, _, _, approach1_model_2_test_data, test_approach2_labels, test_approach_3_task_2_labels = preprocess_data(test_data, state['word2idx'], state['max_len'], state['num_entities'], state['PAD'])
if 1 in models_to_evaluate:
print("Approach 1: Evaluating binary entity model")
# Load models for approach 1
approach1_mask_prediction = Approach1MaskPrediction(state['vocab_dim'], state['emb_dim'])
approach1_mask_prediction.load_state_dict(torch.load('models/approach1_mask_prediction.pt'))
approach1_entity_classification = Approach1EntityClassification(state['vocab_dim'], state['emb_dim'], state['num_entities'])
approach1_entity_classification.load_state_dict(torch.load('models/approach1_entity_classification.pt'))
# Evaluate performance
approach1_mask_prediction.eval()
with torch.no_grad():
y_pred = approach1_mask_prediction(test_sentence_feats)
threshold = 0.5
y_pred = (y_pred > threshold).float() # Convert probabilities to binary predictions
y_test = test_mask_labels
print("Accuracy of mask prediction model", accuracy_score(y_test, y_pred))
print("F1 score of mask prediction model", f1_score(y_test, y_pred, average='macro'))
# Convert predictions and gold labels tensors to lists of integers
preds = y_pred.tolist()
golds= y_test.tolist()
pred_list = [label_list[int(pred[0])] for pred in preds]
gold_list = [label_list[int(gold[0])] for gold in golds]
#save predictions and gold labels to csv using pandas
np.savetxt('predictions/approach1_pred_binary.csv', pred_list, delimiter=',', fmt='%s')
np.savetxt('predictions/approach1_gold_binary.csv', gold_list, delimiter=',', fmt='%s')
print("Approach 1: Evaluating entity classification model")
# Only run Approach1EntityClassification if y_pred is 1
approach1_entity_classification.eval()
with torch.no_grad():
# Get indices where y_pred is 1
indices = torch.nonzero(y_pred).squeeze(1)
indices = indices[:, 0]
if indices.shape[0] == 0:
print("No entities found in test data")
return
y_pred = approach1_entity_classification(test_sentence_feats[indices])
y_pred = torch.argmax(y_pred, axis=1)
y_test = torch.argmax(approach1_model_2_test_data[indices], axis=1)
print("Accuracy of entity classification model", accuracy_score(y_test, y_pred))
print("F1 score of entity classification model", f1_score(y_test, y_pred, average='macro'))
# Convert predictions and gold labels tensors to lists of integers
preds = y_pred.tolist()
golds = y_test.tolist()
pred_list = [label_list[int(pred)] for pred in preds]
gold_list = [label_list[int(gold)] for gold in golds]
#save predictions and gold labels to csv using pandas
np.savetxt('predictions/approach1_pred_multiclass.csv', pred_list, delimiter=',', fmt='%s')
np.savetxt('predictions/approach1_gold_multiclass.csv', gold_list, delimiter=',', fmt='%s')
if 2 in models_to_evaluate:
print("Approach 2: Evaluating model")
approach2model = Approach2(state['vocab_dim'], state['emb_dim'], state['num_entities'])
approach2model.load_state_dict(torch.load('models/approach2.pt'))
# Evaluate
approach2model.eval()
with torch.no_grad():
y_pred = approach2model(test_sentence_feats)
y_pred = torch.argmax(y_pred, axis=1)
y_test = torch.argmax(test_approach2_labels, axis=1)
print("Accuracy of approach 2 model", accuracy_score(y_test, y_pred))
print("F1 score of approach 2 model", f1_score(y_test, y_pred, average='macro'))
if 3 in models_to_evaluate:
print("Approach 3: Evaluating model")
combined_model = Approach3CombinedModel(state['vocab_dim'], state['emb_dim'], state['num_entities'])
combined_model.load_state_dict(torch.load('models/approach3.pt'))
# Evaluate
combined_model.eval()
with torch.no_grad():
y_pred1, y_pred2 = combined_model(test_sentence_feats)
y_pred1 = torch.round(y_pred1)
y_pred2 = torch.argmax(y_pred2, axis=1)
y_test1 = test_mask_labels
y_test2 = torch.argmax(test_approach_3_task_2_labels, axis=1)
print("Accuracy of approach 3 model 1", accuracy_score(y_test1, y_pred1))
print("F1 score of approach 3 model 1", f1_score(y_test1, y_pred1, average='macro'))
print("Accuracy of approach 3 model 2", accuracy_score(y_test2, y_pred2))
print("F1 score of approach 3 model 2", f1_score(y_test2, y_pred2, average='macro'))
if __name__ == '__main__':
'''
Usage:
To train all models and save them to disk
`python main.py --train`
To train only approach 1 and 2 without saving them
`python main.py --train --approach-1 --approach-2 --save False`
To test all models from disk. Remember to train them first
`python main.py --test`
To evaluate only approach 1 and 2
`python main.py --test --approach-1 --approach-2`
'''
main()