-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain_sentiment_regressor.py
154 lines (126 loc) · 5.77 KB
/
train_sentiment_regressor.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 os
import time
import torch
import pickle
import argparse
import numpy as np
from tqdm import tqdm
import torch
import torch.optim as optim
from models import T5EncoderRegressor
from glove_models import GloVeT5EncoderRegressor
from dataloader import RegressionLoader
from transformers.optimization import AdamW, get_scheduler
from transformers.trainer_pt_utils import get_parameter_names
from sklearn.metrics import f1_score, accuracy_score
def configure_dataloaders(batch_size):
"Prepare dataloaders"
train_loader = RegressionLoader("data/empathetic_dialogues/train_vader.csv", batch_size, shuffle=True)
valid_loader = RegressionLoader("data/empathetic_dialogues/valid_vader.csv", batch_size, shuffle=False)
test_loader = RegressionLoader("data/empathetic_dialogues/test_vader.csv", batch_size, shuffle=False)
return train_loader, valid_loader, test_loader
def configure_transformer_optimizer(model, args):
"Prepare AdamW optimizer for transformer encoders"
decay_parameters = get_parameter_names(model, [torch.nn.LayerNorm])
# decay_parameters = [name for name in decay_parameters if "bias" not in name]
decay_parameters = [name for name in decay_parameters if "bias" not in name]
optimizer_grouped_parameters = [
{
"params": [p for n, p in model.named_parameters() if n in decay_parameters],
"weight_decay": args.wd,
},
{
"params": [p for n, p in model.named_parameters() if n not in decay_parameters],
"weight_decay": 0.0,
},
]
optimizer_kwargs = {
"betas": (args.adam_beta1, args.adam_beta2),
"eps": args.adam_epsilon,
"lr": args.lr
}
optimizer = AdamW(optimizer_grouped_parameters, **optimizer_kwargs)
return optimizer
def configure_scheduler(optimizer, num_training_steps, args):
"Prepare scheduler"
warmup_steps = (
args.warmup_steps
if args.warmup_steps > 0
else math.ceil(num_training_steps * args.warmup_ratio)
)
lr_scheduler = get_scheduler(
args.lr_scheduler_type,
optimizer,
num_warmup_steps=warmup_steps,
num_training_steps=num_training_steps,
)
return lr_scheduler
def train_or_eval_model(model, dataloader, optimizer=None, train=False):
losses = []
assert not train or optimizer!=None
if train:
model.train()
else:
model.eval()
for utterances, gold_scores in tqdm(dataloader, leave=False):
if train:
optimizer.zero_grad()
scores = model(utterances)
loss = loss_function(scores, torch.tensor(gold_scores).cuda())
if train:
loss.backward()
optimizer.step()
losses.append(loss.item())
avg_loss = round(np.mean(losses), 4)
return avg_loss
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--lr", type=float, default=1e-5, help="Learning rate for transformers.")
parser.add_argument("--wd", default=0.0, type=float, help="Weight decay for transformers.")
parser.add_argument("--adam-epsilon", default=1e-8, type=float, help="Epsilon for AdamW optimizer.")
parser.add_argument("--adam-beta1", default=0.9, type=float, help="beta1 for AdamW optimizer.")
parser.add_argument("--adam-beta2", default=0.999, type=float, help="beta2 for AdamW optimizer.")
parser.add_argument("--lr-scheduler-type", default="linear")
parser.add_argument("--warmup-steps", type=int, default=0, help="Steps used for a linear warmup from 0 to lr.")
parser.add_argument("--warmup-ratio", type=float, default=0.0, help="Ratio of total training steps used for a linear warmup from 0 to lr.")
parser.add_argument("--batch-size", type=int, default=8, help="Batch size.")
parser.add_argument("--epochs", type=int, default=6, help="Number of epochs.")
parser.add_argument("--size", default="base", help="Which model size for T5: base or large")
parser.add_argument("--model", default="t5", help="Which model t5 or glv-t5")
args = parser.parse_args()
print(args)
global loss_function
global tokenizer
batch_size = args.batch_size
n_epochs = args.epochs
size = args.size
run_ID = int(time.time())
print ("run id:", run_ID)
if args.model == "t5":
model = T5EncoderRegressor(size).cuda()
elif args.model == "glove-t5":
model = GloVeT5EncoderRegressor(size).cuda()
loss_function = torch.nn.MSELoss().cuda()
optimizer = configure_transformer_optimizer(model, args)
train_loader, valid_loader, test_loader = configure_dataloaders(batch_size)
lf = open("results/logs.tsv", "a")
lf.write(str(run_ID) + "\tvader sentiment\t" + str(args) + "\n")
best_loss = np.inf
for e in range(n_epochs):
train_loss = train_or_eval_model(model, train_loader, optimizer, True)
valid_loss = train_or_eval_model(model, valid_loader)
test_loss = train_or_eval_model(model, test_loader)
x = "Epoch {}: train loss: {}; valid loss: {}; test loss: {}".format(e+1, train_loss, valid_loss, test_loss)
print (x)
lf.write(x + "\n")
if valid_loss < best_loss:
if not os.path.exists("saved/sentiment/" + str(run_ID) + "/"):
os.makedirs("saved/sentiment/" + str(run_ID) + "/")
torch.save(model.state_dict(), "saved/sentiment/"+ str(run_ID) + "/model.pt")
best_loss = valid_loss
lf.write("\n\n")
lf.close()
print ("Best valid loss: {}".format(best_loss))
content = [str(best_loss), "vader sentiment", str(run_ID), str(args)]
with open("results/results.txt", "a") as f:
f.write("\t".join(content) + "\n")