-
Notifications
You must be signed in to change notification settings - Fork 35
/
data_utils.py
286 lines (240 loc) · 10.3 KB
/
data_utils.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
# 预训练语料构建
import glob
import os
os.environ['TF_KERAS'] = '1' # 必须使用tf.keras
import numpy as np
import pandas as pd
import tensorflow as tf
from bert4keras.backend import K
from bert4keras.snippets import parallel_apply
from bert4keras.tokenizers import Tokenizer
from tqdm import tqdm
class TrainingDataset(object):
"""预训练数据集生成器。"""
def __init__(self, tokenizer, sequence_length=512):
"""参数说明:tokenizer必须是bert4keras自带的tokenizer类;"""
self.tokenizer = tokenizer
self.sequence_length = sequence_length
self.token_pad_id = tokenizer._token_pad_id
self.token_cls_id = tokenizer._token_start_id
self.token_sep_id = tokenizer._token_end_id
self.token_mask_id = tokenizer._token_mask_id
self.vocab_size = tokenizer._vocab_size
def padding(self, sequence, padding_value=None):
"""对单个序列进行补0。"""
if padding_value is None:
padding_value = self.token_pad_id
sequence = sequence[:self.sequence_length]
padding_length = self.sequence_length - len(sequence)
return sequence + [padding_value] * padding_length
def sentence_process(self, text):
"""单个文本的处理函数,返回处理后的instance。"""
raise NotImplementedError
def paragraph_process(self, texts, starts, ends, paddings):
"""单个段落(多个文本)的处理函数
说明:texts是单句组成的list;starts是每个instance的起始id;
ends是每个instance的终止id;paddings是每个instance的填充id。
做法:不断塞句子,直到长度最接近sequence_length,然后padding。
"""
instances, instance = [], [[start] for start in starts]
for text in texts:
# 处理单个句子
sub_instance = self.sentence_process(text)
sub_instance = [i[:self.sequence_length - 2] for i in sub_instance]
new_length = len(instance[0]) + len(sub_instance[0])
# 如果长度即将溢出
if new_length > self.sequence_length - 1:
# 插入终止符,并padding
complete_instance = []
for item, end, pad in zip(instance, ends, paddings):
item.append(end)
item = self.padding(item, pad)
complete_instance.append(item)
# 存储结果,并构建新样本
instances.append(complete_instance)
instance = [[start] for start in starts]
# 样本续接
for item, sub_item in zip(instance, sub_instance):
item.extend(sub_item)
# 插入终止符,并padding
complete_instance = []
for item, end, pad in zip(instance, ends, paddings):
item.append(end)
item = self.padding(item, pad)
complete_instance.append(item)
# 存储最后的instance
instances.append(complete_instance)
return instances
def tfrecord_serialize(self, instances, instance_keys):
"""转为tfrecord的字符串,等待写入到文件。"""
def create_feature(x):
return tf.train.Feature(int64_list=tf.train.Int64List(value=x))
serialized_instances = []
for instance in instances:
features = {
k: create_feature(v)
for k, v in zip(instance_keys, instance)
}
tf_features = tf.train.Features(feature=features)
tf_example = tf.train.Example(features=tf_features)
serialized_instance = tf_example.SerializeToString()
serialized_instances.append(serialized_instance)
return serialized_instances
def process(self, corpus, record_name, workers=8, max_queue_size=2000):
"""处理输入语料(corpus),最终转为tfrecord格式(record_name)
自带多进程支持,如果cpu核心数多,请加大workers和max_queue_size。
"""
writer = tf.io.TFRecordWriter(record_name)
globals()['count'] = 0
def write_to_tfrecord(serialized_instances):
globals()['count'] += len(serialized_instances)
for serialized_instance in serialized_instances:
writer.write(serialized_instance)
def paragraph_process(texts):
instances = self.paragraph_process(texts)
serialized_instances = self.tfrecord_serialize(instances)
return serialized_instances
parallel_apply(
func=paragraph_process,
iterable=corpus,
workers=workers,
max_queue_size=max_queue_size,
callback=write_to_tfrecord,
)
writer.close()
print('write %s examples into %s' % (globals()['count'], record_name))
@staticmethod
def load_tfrecord(record_names, batch_size, parse_function):
"""加载处理成tfrecord格式的语料。"""
if not isinstance(record_names, list):
record_names = [record_names]
dataset = tf.data.TFRecordDataset(record_names) # 加载
dataset = dataset.map(parse_function) # 解析
dataset = dataset.repeat() # 循环
dataset = dataset.shuffle(batch_size * 1000) # 打乱
dataset = dataset.batch(batch_size) # 成批
return dataset
class TrainingDatasetRoBERTa(TrainingDataset):
"""预训练数据集生成器(RoBERTa模式)。"""
def __init__(
self, tokenizer, word_segment, mask_rate=0.15, sequence_length=512
):
"""参数说明:
tokenizer必须是bert4keras自带的tokenizer类;
word_segment是任意分词函数。
"""
super(TrainingDatasetRoBERTa, self).__init__(tokenizer, sequence_length)
self.word_segment = word_segment
self.mask_rate = mask_rate
def token_process(self, token_id):
"""
以80%的几率替换为[MASK],以10%的几率保持不变,以10%的几率替换为一个随机token。
"""
rand = np.random.random()
if rand <= 0.8:
return self.token_mask_id
elif rand <= 0.9:
return token_id
else:
return np.random.randint(0, self.vocab_size)
def sentence_process(self, text):
"""单个文本的处理函数
流程:
1.分词;
2.转id;
3.按照mask_rate构建全词mask的序列来指定哪些token是否要被mask。
"""
words = self.word_segment(text)
rands = np.random.random(len(words))
token_ids, mask_ids = [], []
for rand, word in zip(rands, words):
word_tokens = self.tokenizer.tokenize(text=word)[1:-1]
word_token_ids = self.tokenizer.tokens_to_ids(word_tokens)
token_ids.extend(word_token_ids)
if rand < self.mask_rate:
word_mask_ids = [
self.token_process(i) + 1 for i in word_token_ids
]
else:
word_mask_ids = [0] * len(word_tokens)
mask_ids.extend(word_mask_ids)
return [token_ids, mask_ids]
def paragraph_process(self, texts):
"""给原方法补上starts、ends、paddings。"""
starts = [self.token_cls_id, 0]
ends = [self.token_sep_id, 0]
paddings = [self.token_pad_id, 0]
return super().paragraph_process(texts, starts, ends, paddings)
def tfrecord_serialize(self, instances):
"""给原方法补上instance_keys。"""
instance_keys = ['token_ids', 'mask_ids']
return super().tfrecord_serialize(instances, instance_keys)
@staticmethod
def load_tfrecord(record_names, sequence_length, batch_size):
"""给原方法补上parse_function。"""
def parse_function(serialized):
features = {
'token_ids': tf.io.FixedLenFeature([sequence_length], tf.int64),
'mask_ids': tf.io.FixedLenFeature([sequence_length], tf.int64),
}
features = tf.io.parse_single_example(serialized, features)
token_ids = features['token_ids']
mask_ids = features['mask_ids']
segment_ids = K.zeros_like(token_ids, dtype='int64')
is_masked = K.not_equal(mask_ids, 0)
masked_token_ids = K.switch(is_masked, mask_ids - 1, token_ids)
x = {
'Input-Token': masked_token_ids,
'Input-Segment': segment_ids,
'token_ids': token_ids,
'is_masked': K.cast(is_masked, K.floatx()),
}
y = {
'mlm_loss': K.zeros([1]),
'mlm_acc': K.zeros([1]),
}
return x, y
return TrainingDataset.load_tfrecord(
record_names, batch_size, parse_function
)
if __name__ == '__main__':
sequence_length = 512
workers = 8
max_queue_size = 10000
dict_path = 'pre_models/vocab.txt'
tokenizer = Tokenizer(dict_path, do_lower_case=True)
def some_texts():
filenames = glob.glob('data/*')
np.random.shuffle(filenames)
count, texts = 0, []
for filename in filenames:
df = pd.read_csv(filename, sep='\t')
for _, row in df.iterrows():
l = row['text'].strip()
if len(l.split()) > sequence_length - 2:
l = l.split()
len_ = sequence_length - 2
texts.extend([
' '.join(l[i * len_: (i + 1) * len_])
for i in range((len(l) // len_) + 1)
])
else:
texts.extend([l])
count += 1
if count == 10: # 10篇文章合在一起再处理
yield texts
count, texts = 0, []
if texts:
yield texts
def word_segment(text):
return text.split()
TD = TrainingDatasetRoBERTa(
tokenizer, word_segment, sequence_length=sequence_length
)
for i in range(10): # 数据重复10遍
TD.process(
corpus=tqdm(some_texts()),
record_name=f'corpus_tfrecord/corpus.{i}.tfrecord',
workers=workers,
max_queue_size=max_queue_size,
)