forked from PaddlePaddle/PaddleNLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
379 lines (337 loc) · 13.5 KB
/
data.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# -*- coding: utf-8 -*-
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import io
from functools import partial
import numpy as np
import jieba
import paddle
from paddlenlp.data import Stack, Tuple, Pad, Vocab
from paddlenlp.transformers import BertTokenizer
from paddlenlp.datasets import load_dataset
from utils import convert_example_for_lstm, convert_example_for_distill, convert_pair_example
def load_vocab(vocab_file):
"""Loads a vocabulary file into a dictionary."""
vocab = {}
with open(vocab_file, "r", encoding="utf-8") as reader:
tokens = reader.readlines()
for index, token in enumerate(tokens):
token = token.rstrip("\n").split("\t")[0]
vocab[token] = index
return vocab
def ngram_sampling(words, words_2=None, p_ng=0.25, ngram_range=(2, 6)):
if np.random.rand() < p_ng:
ngram_len = np.random.randint(ngram_range[0], ngram_range[1] + 1)
ngram_len = min(ngram_len, len(words))
start = np.random.randint(0, len(words) - ngram_len + 1)
words = words[start:start + ngram_len]
if words_2:
words_2 = words_2[start:start + ngram_len]
return words if not words_2 else (words, words_2)
def flatten(list_of_list):
final_list = []
for each_list in list_of_list:
final_list += each_list
return final_list
def apply_data_augmentation(data,
task_name,
tokenizer,
n_iter=20,
p_mask=0.1,
p_ng=0.25,
ngram_range=(2, 6),
whole_word_mask=False,
seed=0):
"""
Data Augmentation contains Masking and n-gram sampling. Tokenization and
Masking are performed at the same time, so that the masked token can be
directly replaced by `mask_token`, after what sampling is performed.
"""
def _data_augmentation(data,
tokenized_list,
whole_word_mask=whole_word_mask):
# 1. Masking
words = []
if not whole_word_mask:
words = [
tokenizer.mask_token if np.random.rand() < p_mask else word
for word in tokenized_list
]
else:
for word in data.split():
words += [[tokenizer.mask_token]] if np.random.rand(
) < p_mask else [tokenizer.tokenize(word)]
# 2. N-gram sampling
words = ngram_sampling(words, p_ng=p_ng, ngram_range=ngram_range)
words = flatten(words) if isinstance(words[0], list) else words
return words
np.random.seed(seed)
new_data = []
for example in data:
if task_name == 'qqp':
data_list = tokenizer.tokenize(example['sentence1'])
data_list_2 = tokenizer.tokenize(example['sentence2'])
new_data.append({
"sentence1": data_list,
"sentence2": data_list_2,
"labels": example['labels']
})
else:
data_list = tokenizer.tokenize(example['sentence'])
new_data.append({
"sentence": data_list,
"labels": example['labels']
})
for example in data:
for _ in range(n_iter):
if task_name == 'qqp':
words = _data_augmentation(example['sentence1'], data_list)
words_2 = _data_augmentation(example['sentence2'], data_list_2)
new_data.append({
"sentence1": words,
"sentence2": words_2,
"labels": example['labels']
})
else:
words = _data_augmentation(example['sentence'], data_list)
new_data.append({
"sentence": words,
"labels": example['labels']
})
return new_data
def apply_data_augmentation_for_cn(data,
tokenizer,
vocab,
n_iter=20,
p_mask=0.1,
p_ng=0.25,
ngram_range=(2, 10),
seed=0):
"""
Because BERT and jieba have different `tokenize` function, it returns
jieba_tokenizer(example['text'], bert_tokenizer(example['text']) and
example['label]) for each example in data.
jieba tokenization and Masking are performed at the same time, so that the
masked token can be directly replaced by `mask_token`, and other tokens
could be tokenized by BERT's tokenizer, from which tokenized example for
student model and teacher model would get at the same time.
"""
np.random.seed(seed)
new_data = []
for example in data:
if not example['text']:
continue
text_tokenized = list(jieba.cut(example['text']))
lstm_tokens = text_tokenized
bert_tokens = tokenizer.tokenize(example['text'])
new_data.append({
"lstm_tokens": lstm_tokens,
"bert_tokens": bert_tokens,
"label": example['label']
})
for _ in range(n_iter):
# 1. Masking
lstm_tokens, bert_tokens = [], []
for word in text_tokenized:
if np.random.rand() < p_mask:
lstm_tokens.append([vocab.unk_token])
bert_tokens.append([tokenizer.unk_token])
else:
lstm_tokens.append([word])
bert_tokens.append(tokenizer.tokenize(word))
# 2. N-gram sampling
lstm_tokens, bert_tokens = ngram_sampling(lstm_tokens, bert_tokens,
p_ng, ngram_range)
lstm_tokens, bert_tokens = flatten(lstm_tokens), flatten(
bert_tokens)
if lstm_tokens and bert_tokens:
new_data.append({
"lstm_tokens": lstm_tokens,
"bert_tokens": bert_tokens,
"label": example['label']
})
return new_data
def create_data_loader_for_small_model(task_name,
vocab_path,
model_name=None,
batch_size=64,
max_seq_length=128,
shuffle=True):
"""Data loader for bi-lstm, not bert."""
if task_name == 'chnsenticorp':
train_ds, dev_ds = load_dataset(task_name, splits=["train", "dev"])
else:
train_ds, dev_ds = load_dataset(
'glue', task_name, splits=["train", "dev"])
if task_name == 'chnsenticorp':
vocab = Vocab.load_vocabulary(
vocab_path,
unk_token='[UNK]',
pad_token='[PAD]',
bos_token=None,
eos_token=None, )
pad_val = vocab['[PAD]']
else:
vocab = BertTokenizer.from_pretrained(model_name)
pad_val = vocab.pad_token_id
trans_fn = partial(
convert_example_for_lstm,
task_name=task_name,
vocab=vocab,
max_seq_length=max_seq_length,
is_test=False)
batchify_fn = lambda samples, fn=Tuple(
Pad(axis=0, pad_val=pad_val), # input_ids
Stack(dtype="int64"), # seq len
Stack(dtype="int64") # label
): fn(samples)
train_ds = train_ds.map(trans_fn, lazy=True)
dev_ds = dev_ds.map(trans_fn, lazy=True)
train_data_loader, dev_data_loader = create_dataloader(
train_ds, dev_ds, batch_size, batchify_fn, shuffle)
return train_data_loader, dev_data_loader
def create_distill_loader(task_name,
model_name,
vocab_path,
batch_size=64,
max_seq_length=128,
shuffle=True,
n_iter=20,
whole_word_mask=False,
seed=0):
"""
Returns batch data for bert and small model.
Bert and small model have different input representations.
"""
tokenizer = BertTokenizer.from_pretrained(model_name)
if task_name == 'chnsenticorp':
train_ds, dev_ds = load_dataset(task_name, splits=["train", "dev"])
vocab = Vocab.load_vocabulary(
vocab_path,
unk_token='[UNK]',
pad_token='[PAD]',
bos_token=None,
eos_token=None, )
pad_val = vocab['[PAD]']
data_aug_fn = partial(
apply_data_augmentation_for_cn,
tokenizer=tokenizer,
vocab=vocab,
n_iter=n_iter,
seed=seed)
else:
train_ds, dev_ds = load_dataset(
'glue', task_name, splits=["train", "dev"])
vocab = tokenizer
pad_val = tokenizer.pad_token_id
data_aug_fn = partial(
apply_data_augmentation,
task_name=task_name,
tokenizer=tokenizer,
n_iter=n_iter,
whole_word_mask=whole_word_mask,
seed=seed)
train_ds = train_ds.map(data_aug_fn, batched=True)
print("Data augmentation has been applied.")
trans_fn = partial(
convert_example_for_distill,
task_name=task_name,
tokenizer=tokenizer,
label_list=train_ds.label_list,
max_seq_length=max_seq_length,
vocab=vocab)
trans_fn_dev = partial(
convert_example_for_distill,
task_name=task_name,
tokenizer=tokenizer,
label_list=train_ds.label_list,
max_seq_length=max_seq_length,
vocab=vocab,
is_tokenized=False)
if task_name == 'qqp':
batchify_fn = lambda samples, fn=Tuple(
Pad(axis=0, pad_val=tokenizer.pad_token_id), # bert input
Pad(axis=0, pad_val=tokenizer.pad_token_type_id), # bert segment
Pad(axis=0, pad_val=pad_val), # small input_ids
Stack(dtype="int64"), # small seq len
Pad(axis=0, pad_val=pad_val), # small input_ids
Stack(dtype="int64"), # small seq len
Stack(dtype="int64") # small label
): fn(samples)
else:
batchify_fn = lambda samples, fn=Tuple(
Pad(axis=0, pad_val=tokenizer.pad_token_id), # bert input
Pad(axis=0, pad_val=tokenizer.pad_token_type_id), # bert segment
Pad(axis=0, pad_val=pad_val), # small input_ids
Stack(dtype="int64"), # small seq len
Stack(dtype="int64") # small label
): fn(samples)
train_ds = train_ds.map(trans_fn, lazy=True)
dev_ds = dev_ds.map(trans_fn_dev, lazy=True)
train_data_loader, dev_data_loader = create_dataloader(
train_ds, dev_ds, batch_size, batchify_fn, shuffle)
return train_data_loader, dev_data_loader
def create_pair_loader_for_small_model(task_name,
model_name,
vocab_path,
batch_size=64,
max_seq_length=128,
shuffle=True,
is_test=False):
"""Only support QQP now."""
tokenizer = BertTokenizer.from_pretrained(model_name)
train_ds, dev_ds = load_dataset('glue', task_name, splits=["train", "dev"])
vocab = Vocab.load_vocabulary(
vocab_path,
unk_token='[UNK]',
pad_token='[PAD]',
bos_token=None,
eos_token=None, )
trans_func = partial(
convert_pair_example,
task_name=task_name,
vocab=tokenizer,
is_tokenized=False,
max_seq_length=max_seq_length,
is_test=is_test)
train_ds = train_ds.map(trans_func, lazy=True)
dev_ds = dev_ds.map(trans_func, lazy=True)
batchify_fn = lambda samples, fn=Tuple(
Pad(axis=0, pad_val=vocab['[PAD]']), # input
Stack(), # length
Pad(axis=0, pad_val=vocab['[PAD]']), # input
Stack(), # length
Stack(dtype="int64" if train_ds.label_list else "float32") # label
): fn(samples)
train_data_loader, dev_data_loader = create_dataloader(
train_ds, dev_ds, batch_size, batchify_fn, shuffle)
return train_data_loader, dev_data_loader
def create_dataloader(train_ds, dev_ds, batch_size, batchify_fn, shuffle=True):
train_batch_sampler = paddle.io.DistributedBatchSampler(
train_ds, batch_size=batch_size, shuffle=shuffle)
dev_batch_sampler = paddle.io.BatchSampler(
dev_ds, batch_size=batch_size, shuffle=False)
train_data_loader = paddle.io.DataLoader(
dataset=train_ds,
batch_sampler=train_batch_sampler,
collate_fn=batchify_fn,
num_workers=0,
return_list=True)
dev_data_loader = paddle.io.DataLoader(
dataset=dev_ds,
batch_sampler=dev_batch_sampler,
collate_fn=batchify_fn,
num_workers=0,
return_list=True)
return train_data_loader, dev_data_loader