-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
216 lines (176 loc) · 7.24 KB
/
run.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
#!/usr/bin/env python
# coding: utf-8
import tqdm
import torch
import numpy as np
import pandas as pd
import torchcontrib
import utils
from transformers import AdamW
from transformers import RobertaConfig
from transformers import get_linear_schedule_with_warmup
from config import Config as config
from model import TweetModel
from dataset import TweetDataset
def calculate_jaccard_score(
original_tweet,
target_string,
sentiment_val,
idx_start,
idx_end,
offsets,
verbose=False):
if idx_end < idx_start:
idx_end = idx_start
filtered_output = ""
for ix in range(idx_start, idx_end + 1):
filtered_output += original_tweet[offsets[ix][0]: offsets[ix][1]]
if (ix+1) < len(offsets) and offsets[ix][1] < offsets[ix+1][0]:
filtered_output += " "
if len(original_tweet.split()) < 2:
filtered_output = original_tweet
jac = utils.jaccard(target_string.strip(), filtered_output.strip())
return jac, filtered_output
def loss_fn(start_logits, end_logits, start_positions, end_positions):
"""
Return the sum of the cross entropy losses for both the start and end logits
"""
loss_fct = torch.nn.CrossEntropyLoss()
start_loss = loss_fct(start_logits, start_positions)
end_loss = loss_fct(end_logits, end_positions)
total_loss = (start_loss + end_loss)
return total_loss
def train_fn(data_loader, model, optimizer, device, scheduler=None):
model.train()
losses = utils.AverageMeter()
tk0 = tqdm.tqdm(data_loader, total=len(data_loader))
for bi, d in enumerate(tk0):
ids = d['ids']
token_type_ids = d['token_type_ids']
mask = d['mask']
start_labels = d['targets_start']
end_labels = d['targets_end']
ids = ids.to(device, dtype=torch.long)
token_type_ids = token_type_ids.to(device, dtype=torch.long)
mask = mask.to(device, dtype=torch.long)
start_labels = start_labels.to(device, dtype=torch.long)
end_labels = end_labels.to(device, dtype=torch.long)
model.zero_grad()
outputs_start, outputs_end = model(ids=ids, mask=mask, token_type_ids=token_type_ids)
loss = loss_fn(outputs_start, outputs_end,
start_labels, end_labels)
loss.backward()
optimizer.step()
scheduler.step()
losses.update(loss.item(), ids.size(0))
tk0.set_postfix(loss=losses.avg)
def eval_fn(data_loader, model, device):
model.eval()
losses = utils.AverageMeter()
jaccards = utils.AverageMeter()
with torch.no_grad():
for bi, d in enumerate(data_loader):
ids = d["ids"]
token_type_ids = d["token_type_ids"]
mask = d["mask"]
sentiment = d["sentiment"]
orig_selected = d["orig_selected"]
orig_tweet = d["orig_tweet"]
targets_start = d["targets_start"]
targets_end = d["targets_end"]
offsets = d["offsets"].cpu().numpy()
ids = ids.to(device, dtype=torch.long)
token_type_ids = token_type_ids.to(device, dtype=torch.long)
mask = mask.to(device, dtype=torch.long)
targets_start = targets_start.to(device, dtype=torch.long)
targets_end = targets_end.to(device, dtype=torch.long)
outputs_start, outputs_end = model(
ids=ids,
mask=mask,
token_type_ids=token_type_ids
)
loss = loss_fn(outputs_start, outputs_end, targets_start, targets_end)
outputs_start = torch.softmax(outputs_start, dim=1).cpu().detach().numpy()
outputs_end = torch.softmax(outputs_end, dim=1).cpu().detach().numpy()
jaccard_scores = []
for px, tweet in enumerate(orig_tweet):
selected_tweet = orig_selected[px]
tweet_sentiment = sentiment[px]
jaccard_score, _ = calculate_jaccard_score(
original_tweet=tweet,
target_string=selected_tweet,
sentiment_val=tweet_sentiment,
idx_start=np.argmax(outputs_start[px, :]),
idx_end=np.argmax(outputs_end[px, :]),
offsets=offsets[px]
)
jaccard_scores.append(jaccard_score)
jaccards.update(np.mean(jaccard_scores), ids.size(0))
losses.update(loss.item(), ids.size(0))
return jaccards.avg
def run(fold):
dfx = pd.read_csv(config.TRAINING_FILE)
df_train = dfx[dfx.kfold != fold].reset_index(drop=True)
df_valid = dfx[dfx.kfold == fold].reset_index(drop=True)
train_dataset = TweetDataset(
tweet=df_train.text.values,
sentiment=df_train.sentiment.values,
selected_text=df_train.selected_text.values)
train_data_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=config.TRAIN_BATCH_SIZE,
num_workers=4,
shuffle=True)
valid_dataset = TweetDataset(
tweet=df_valid.text.values,
sentiment=df_valid.sentiment.values,
selected_text=df_valid.selected_text.values)
valid_data_loader = torch.utils.data.DataLoader(
valid_dataset,
batch_size=config.VALID_BATCH_SIZE,
num_workers=4,
shuffle=False)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model_config = RobertaConfig.from_pretrained(config.ROBERTA_PATH)
model_config.output_hidden_states = True
model = TweetModel(conf=model_config)
model = model.to(device)
num_train_steps = int(len(df_train) / config.TRAIN_BATCH_SIZE * config.EPOCHS)
param_optimizer = list(model.named_parameters())
no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
optimizer_parameters = [
{'params': [p for n, p in param_optimizer
if not any(nd in n for nd in no_decay)],
'weight_decay': config.WEIGHT_DECAY},
{'params': [p for n, p in param_optimizer
if any(nd in n for nd in no_decay)],
'weight_decay': 0.0}]
base_opt = AdamW(optimizer_parameters,lr=config.LEARNING_RATE)
optimizer = torchcontrib.optim.SWA(
base_opt,
swa_start=int(num_train_steps * config.SWA_RATIO),
swa_freq=config.SWA_FREQ,
swa_lr=None)
scheduler = get_linear_schedule_with_warmup(
optimizer=optimizer,
num_warmup_steps=int(num_train_steps * config.WARMUP_RATIO),
num_training_steps=num_train_steps)
print(f'Training is starting for fold = {fold}')
for epoch in range(config.EPOCHS):
train_fn(train_data_loader, model, optimizer,device,scheduler=scheduler)
jaccard = eval_fn(valid_data_loader, model, device)
if config.USE_SWA:
optimizer.swap_swa_sgd()
torch.save(model.state_dict(),f'{config.MODEL_SAVE_PATH}/model_{fold}.bin')
return jaccard
if __name__ == '__main__':
utils.seed_everything(seed=config.SEED)
fold_scores = []
for i in range(config.N_FOLDS):
fold_score = run(i)
fold_scores.append(fold_score)
print('\nScores without SWA:')
for i in range(config.N_FOLDS):
print(f'Fold={i}, Jaccard = {fold_scores[i]}')
print(f'Mean = {np.mean(fold_scores)}')
print(f'Std = {np.std(fold_scores)}')