-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
154 lines (123 loc) · 6.22 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
import argparse
import random
import numpy as np
import torch
import torch.autograd
import torch.nn as nn
import torch.optim as optim
from sklearn.metrics import (accuracy_score, f1_score, precision_score,
recall_score, classification_report)
from torch.utils.data import DataLoader
import models
import utils
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def eval_metrics(y_true, y_pred):
acc = accuracy_score(y_true, y_pred)
p = precision_score(y_true, y_pred, average="macro")
r = recall_score(y_true, y_pred, average="macro")
f1 = f1_score(y_true, y_pred, average="macro")
report = classification_report(y_true, y_pred)
return (acc, p, r, f1, report)
def train(model, train_data_loader, criterion, optimizer):
model.train()
loss_list = []
pred_list = []
true_list = []
for inputs, targets, masks in train_data_loader:
optimizer.zero_grad()
outputs = model(inputs)
class_num = outputs.size(-1)
outputs = torch.masked_select(outputs, masks.unsqueeze(-1)).view(-1, class_num)
targets = torch.masked_select(targets, masks)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
loss_list.append(loss.item())
pred_list.append(torch.argmax(outputs, dim=-1).cpu().numpy())
true_list.append(targets.cpu().numpy())
y_pred = np.concatenate(pred_list)
y_true = np.concatenate(true_list)
loss = np.mean(loss_list)
result = eval_metrics(y_true, y_pred)
return (loss, *result)
def evaluate(model, eval_data_loader, criterion):
model.eval()
loss_list = []
pred_list = []
true_list = []
with torch.no_grad():
for inputs, targets, masks in eval_data_loader:
outputs = model(inputs)
class_num = outputs.size(-1)
outputs = torch.masked_select(outputs, masks.unsqueeze(-1)).view(-1, class_num)
targets = torch.masked_select(targets, masks)
loss = criterion(outputs, targets)
loss_list.append(loss.item())
pred_list.append(torch.argmax(outputs, dim=-1).cpu().numpy())
true_list.append(targets.cpu().numpy())
y_pred = np.concatenate(pred_list)
y_true = np.concatenate(true_list)
loss = np.mean(loss_list)
result = eval_metrics(y_true, y_pred)
return (loss, *result)
if __name__ == "__main__":
# path config
parser = argparse.ArgumentParser()
parser.add_argument("--train_data_path", type=str,
default="./data/Emotion Detection in Conversations/train/dialogues_train.txt")
parser.add_argument("--train_label_path", type=str,
default="./data/Emotion Detection in Conversations/train/dialogues_emotion_train.txt")
parser.add_argument("--dev_data_path", type=str,
default="./data/Emotion Detection in Conversations/validation/dialogues_validation.txt")
parser.add_argument("--dev_label_path", type=str,
default="./data/Emotion Detection in Conversations/validation/dialogues_emotion_validation.txt")
parser.add_argument("--test_data_path", type=str,
default="./data/Emotion Detection in Conversations/test/dialogues_test.txt")
parser.add_argument("--test_label_path", type=str,
default="./data/Emotion Detection in Conversations/test/dialogues_emotion_test.txt")
parser.add_argument("--model_save_path", type=str, default="./model/model.pt")
parser.add_argument("--istrain", action="store_true")
# model config
parser.add_argument("--batch_size", type=int, default=16)
parser.add_argument("--embedding_size", type=int, default=300)
parser.add_argument("--lstm_size", type=int, default=500)
parser.add_argument("--hidden_size", type=int, default=512)
parser.add_argument("--learning_rate", type=float, default=0.001)
parser.add_argument("--epochs", type=int, default=50)
# seed
parser.add_argument("--seed", type=int, default=2)
opts = parser.parse_args()
print(opts)
# fix random seeds
random.seed(opts.seed)
np.random.seed(opts.seed)
torch.manual_seed(opts.seed)
torch.cuda.manual_seed(opts.seed)
# build dataset
if opts.istrain:
train_dataset = utils.build_dataset(opts.train_data_path, opts.train_label_path)
dev_dataset = utils.build_dataset(opts.dev_data_path, opts.dev_label_path)
train_data_loader = DataLoader(dataset=train_dataset, batch_size=opts.batch_size, collate_fn=utils.collate_fn, shuffle=True)
dev_data_loader = DataLoader(dataset=dev_dataset, batch_size=opts.batch_size, collate_fn=utils.collate_fn, shuffle=False)
test_dataset = utils.build_dataset(opts.test_data_path, opts.test_label_path)
test_data_loader = DataLoader(dataset=test_dataset, batch_size=opts.batch_size, collate_fn=utils.collate_fn, shuffle=False)
# build model
model = models.Model(opts.embedding_size, opts.lstm_size, opts.hidden_size).to(DEVICE)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), opts.learning_rate)
if opts.istrain:
best_f1 = 0
for i in range(opts.epochs):
print("Epoch: {} ################################".format(i))
train_loss, train_acc, train_p, train_r, train_f1, _ = train(model, train_data_loader, criterion, optimizer)
dev_loss, dev_acc, dev_p, dev_r, dev_f1, _ = evaluate(model, dev_data_loader, criterion)
print("Train Loss: {:.4f} Acc: {:.4f} F1: {:.4f}({:.4f}/{:.4f})".format(train_loss, train_acc, train_f1, train_p, train_r))
print("Dev Loss: {:.4f} Acc: {:.4f} F1: {:.4f}({:.4f}/{:.4f})".format(dev_loss, dev_acc, dev_f1, dev_p, dev_r))
if dev_f1 > best_f1:
best_f1 = dev_f1
torch.save(model.state_dict(), opts.model_save_path)
print("###########################################")
model.load_state_dict(torch.load(opts.model_save_path))
test_loss, test_acc, test_p, test_r, test_f1, result = evaluate(model, test_data_loader, criterion)
print("Test Loss: {:.4f} Acc: {:.4f} F1: {:.4f}({:.4f}/{:.4f})".format(test_loss, test_acc, test_f1, test_p, test_r))
print(result)