-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
278 lines (236 loc) · 10.9 KB
/
util.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
import os
import math
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import TensorDataset
from transformers import DataProcessor, InputExample, InputFeatures
from transformers import glue_convert_examples_to_features as convert_examples_to_features
import shutil
class NLIProcessor(DataProcessor):
"""Processor for the NLI dataset"""
def __init__(self, data):
self.data = data
def get_examples(self):
examples = []
for i, id in enumerate(self.data["ids"]):
examples.append(InputExample(guid=str(id), text_a=' '.join(self.data["premises"][i]), text_b=' '.join(self.data["hypotheses"][i]),
label=self.data["labels"][i]))
return examples
def get_labels(self):
return ["contradiction", "entailment", "neutral"]
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
class ProgressMeter(object):
def __init__(self, num_batches, meters, prefix=""):
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
self.meters = meters
self.prefix = prefix
def display(self, batch):
entries = [self.prefix + self.batch_fmtstr.format(batch)]
entries += [str(meter) for meter in self.meters]
print('\t'.join(entries))
def _get_batch_fmtstr(self, num_batches):
num_digits = len(str(num_batches // 1))
fmt = '{:' + str(num_digits) + 'd}'
return '[' + fmt + '/' + fmt.format(num_batches) + ']'
def adjust_learning_rate(args, optimizer, epoch):
lr = args.learning_rate
if args.cosine:
eta_min = lr * (args.lr_decay_rate ** 3)
lr = eta_min + (lr - eta_min) * (
1 + math.cos(math.pi * epoch / args.epochs)) / 2
else:
steps = np.sum(epoch > np.asarray(args.lr_decay_epochs))
if steps > 0:
lr = lr * (args.lr_decay_rate ** steps)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def warmup_learning_rate(args, epoch, batch_id, total_batches, optimizer):
if args.warm and epoch <= args.warm_epochs:
p = (batch_id + (epoch - 1) * total_batches) / \
(args.warm_epochs * total_batches)
lr = args.warmup_from + p * (args.warmup_to - args.warmup_from)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def save_model(model, optimizer, opt, epoch, save_file, is_best):
print('==> Saving...')
state = {
'opt': opt,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'epoch': epoch,
}
torch.save(state, save_file)
if is_best:
shutil.copyfile(save_file, 'model_best.pth')
del state
def load_and_cache_examples(args, processor, tokenizer, evaluate, dataset):
print("begin convert data")
# Load data features from cache or dataset file
cached_features_file = os.path.join(
args.data_folder,
"cached_{}_{}_{}_{}".format(
evaluate,
args.model,
str(args.max_seq_length),
dataset
),
)
if os.path.exists(cached_features_file):
features = torch.load(cached_features_file)
if evaluate == "test_match" or evaluate == "test_mismatch":
all_guid = torch.tensor([f.guid for f in features], dtype=torch.long)
all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
all_attention_mask = torch.tensor([f.attention_mask for f in features], dtype=torch.long)
all_token_type_ids = torch.tensor([f.token_type_ids for f in features], dtype=torch.long)
all_labels = torch.tensor([f.label for f in features], dtype=torch.long)
dataset = TensorDataset(all_input_ids, all_attention_mask, all_token_type_ids, all_labels, all_guid)
else:
# print(features[0])
all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
all_attention_mask = torch.tensor([f.attention_mask for f in features], dtype=torch.long)
all_token_type_ids = torch.tensor([f.token_type_ids for f in features], dtype=torch.long)
all_labels = torch.tensor([f.label for f in features], dtype=torch.long)
dataset = TensorDataset(all_input_ids, all_attention_mask, all_token_type_ids, all_labels)
else:
label_list = processor.get_labels()
if evaluate == "test_match" or evaluate == "test_mismatch":
examples = processor.get_examples()
features = convert_examples_to_features(
examples,
tokenizer,
max_length=args.max_seq_length,
label_list=label_list,
output_mode="classification"
)
torch.save(features, cached_features_file)
all_guid = torch.tensor([f.guid for f in features], dtype=torch.long)
all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
all_attention_mask = torch.tensor([f.attention_mask for f in features], dtype=torch.long)
all_token_type_ids = torch.tensor([f.token_type_ids for f in features], dtype=torch.long)
all_labels = torch.tensor([f.label for f in features], dtype=torch.long)
dataset = TensorDataset(all_input_ids, all_attention_mask, all_token_type_ids, all_labels, all_guid)
else:
examples = processor.get_examples()
features = convert_examples_to_features(
examples,
tokenizer,
max_length=args.max_seq_length,
label_list=label_list,
output_mode="classification"
)
torch.save(features, cached_features_file)
all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
all_attention_mask = torch.tensor([f.attention_mask for f in features], dtype=torch.long)
all_token_type_ids = torch.tensor([f.token_type_ids for f in features], dtype=torch.long)
all_labels = torch.tensor([f.label for f in features], dtype=torch.long)
dataset = TensorDataset(all_input_ids, all_attention_mask, all_token_type_ids, all_labels)
print("finish build dataset")
return dataset
# Code widely inspired from:
# https://github.com/allenai/allennlp/blob/master/allennlp/nn/util.py.
def masked_softmax(tensor, mask):
"""
Apply a masked softmax on the last dimension of a tensor.
The input tensor and mask should be of size (batch, *, sequence_length).
Args:
tensor: The tensor on which the softmax function must be applied along
the last dimension.
mask: A mask of the same size as the tensor with 0s in the positions of
the values that must be masked and 1s everywhere else.
Returns:
A tensor of the same size as the inputs containing the result of the
softmax.
"""
tensor_shape = tensor.size()
reshaped_tensor = tensor.view(-1, tensor_shape[-1])
# Reshape the mask so it matches the size of the input tensor.
while mask.dim() < tensor.dim():
mask = mask.unsqueeze(1)
mask = mask.expand_as(tensor).contiguous().float()
reshaped_mask = mask.view(-1, mask.size()[-1])
result = nn.functional.softmax(reshaped_tensor * reshaped_mask, dim=-1)
result = result * reshaped_mask
# 1e-13 is added to avoid divisions by zero.
result = result / (result.sum(dim=-1, keepdim=True) + 1e-13)
return result.view(*tensor_shape)
# Code widely inspired from:
# https://github.com/allenai/allennlp/blob/master/allennlp/nn/util.py.
def weighted_sum(tensor, weights, mask):
"""
Apply a weighted sum on the vectors along the last dimension of 'tensor',
and mask the vectors in the result with 'mask'.
Args:
tensor: A tensor of vectors on which a weighted sum must be applied.
weights: The weights to use in the weighted sum.
mask: A mask to apply on the result of the weighted sum.
Returns:
A new tensor containing the result of the weighted sum after the mask
has been applied on it.
"""
weighted_sum = weights.bmm(tensor)
while mask.dim() < weighted_sum.dim():
mask = mask.unsqueeze(1)
mask = mask.transpose(-1, -2)
mask = mask.expand_as(weighted_sum).contiguous().float()
return weighted_sum * mask
def sort_by_seq_lens(batch, sequences_lengths, descending=True):
"""
Sort a batch of padded variable length sequences by their length.
Args:
batch: A batch of padded variable length sequences. The batch should
have the dimensions (batch_size x max_sequence_length x *).
sequences_lengths: A tensor containing the lengths of the sequences in the
input batch. The tensor should be of size (batch_size).
descending: A boolean value indicating whether to sort the sequences
by their lengths in descending order. Defaults to True.
Returns:
sorted_batch: A tensor containing the input batch reordered by
sequences lengths.
sorted_seq_lens: A tensor containing the sorted lengths of the
sequences in the input batch.
sorting_idx: A tensor containing the indices used to permute the input
batch in order to get 'sorted_batch'.
restoration_idx: A tensor containing the indices that can be used to
restore the order of the sequences in 'sorted_batch' so that it
matches the input batch.
"""
sorted_seq_lens, sorting_index =\
sequences_lengths.sort(0, descending=descending)
sorted_batch = batch.index_select(0, sorting_index)
idx_range =\
sequences_lengths.new_tensor(torch.arange(0, len(sequences_lengths)))
_, reverse_mapping = sorting_index.sort(0, descending=False)
restoration_index = idx_range.index_select(0, reverse_mapping)
return sorted_batch, sorted_seq_lens, sorting_index, restoration_index