Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

extract_features.py 14 KB

You have to be logged in to leave a comment. Sign In
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
  1. # coding=utf-8
  2. # Copyright 2018 The Google AI Language Team Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Extract pre-computed feature vectors from BERT."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import codecs
  20. import collections
  21. import json
  22. import re
  23. import modeling
  24. import tokenization
  25. import tensorflow as tf
  26. flags = tf.flags
  27. FLAGS = flags.FLAGS
  28. flags.DEFINE_string("input_file", None, "")
  29. flags.DEFINE_string("output_file", None, "")
  30. flags.DEFINE_string("layers", "-1,-2,-3,-4", "")
  31. flags.DEFINE_string(
  32. "bert_config_file", None,
  33. "The config json file corresponding to the pre-trained BERT model. "
  34. "This specifies the model architecture.")
  35. flags.DEFINE_integer(
  36. "max_seq_length", 128,
  37. "The maximum total input sequence length after WordPiece tokenization. "
  38. "Sequences longer than this will be truncated, and sequences shorter "
  39. "than this will be padded.")
  40. flags.DEFINE_string(
  41. "init_checkpoint", None,
  42. "Initial checkpoint (usually from a pre-trained BERT model).")
  43. flags.DEFINE_string("vocab_file", None,
  44. "The vocabulary file that the BERT model was trained on.")
  45. flags.DEFINE_bool(
  46. "do_lower_case", True,
  47. "Whether to lower case the input text. Should be True for uncased "
  48. "models and False for cased models.")
  49. flags.DEFINE_integer("batch_size", 32, "Batch size for predictions.")
  50. flags.DEFINE_bool("use_tpu", False, "Whether to use TPU or GPU/CPU.")
  51. flags.DEFINE_string("master", None,
  52. "If using a TPU, the address of the master.")
  53. flags.DEFINE_integer(
  54. "num_tpu_cores", 8,
  55. "Only used if `use_tpu` is True. Total number of TPU cores to use.")
  56. flags.DEFINE_bool(
  57. "use_one_hot_embeddings", False,
  58. "If True, tf.one_hot will be used for embedding lookups, otherwise "
  59. "tf.nn.embedding_lookup will be used. On TPUs, this should be True "
  60. "since it is much faster.")
  61. class InputExample(object):
  62. def __init__(self, unique_id, text_a, text_b):
  63. self.unique_id = unique_id
  64. self.text_a = text_a
  65. self.text_b = text_b
  66. class InputFeatures(object):
  67. """A single set of features of data."""
  68. def __init__(self, unique_id, tokens, input_ids, input_mask, input_type_ids):
  69. self.unique_id = unique_id
  70. self.tokens = tokens
  71. self.input_ids = input_ids
  72. self.input_mask = input_mask
  73. self.input_type_ids = input_type_ids
  74. def input_fn_builder(features, seq_length):
  75. """Creates an `input_fn` closure to be passed to TPUEstimator."""
  76. all_unique_ids = []
  77. all_input_ids = []
  78. all_input_mask = []
  79. all_input_type_ids = []
  80. for feature in features:
  81. all_unique_ids.append(feature.unique_id)
  82. all_input_ids.append(feature.input_ids)
  83. all_input_mask.append(feature.input_mask)
  84. all_input_type_ids.append(feature.input_type_ids)
  85. def input_fn(params):
  86. """The actual input function."""
  87. batch_size = params["batch_size"]
  88. num_examples = len(features)
  89. # This is for demo purposes and does NOT scale to large data sets. We do
  90. # not use Dataset.from_generator() because that uses tf.py_func which is
  91. # not TPU compatible. The right way to load data is with TFRecordReader.
  92. d = tf.data.Dataset.from_tensor_slices({
  93. "unique_ids":
  94. tf.constant(all_unique_ids, shape=[num_examples], dtype=tf.int32),
  95. "input_ids":
  96. tf.constant(
  97. all_input_ids, shape=[num_examples, seq_length],
  98. dtype=tf.int32),
  99. "input_mask":
  100. tf.constant(
  101. all_input_mask,
  102. shape=[num_examples, seq_length],
  103. dtype=tf.int32),
  104. "input_type_ids":
  105. tf.constant(
  106. all_input_type_ids,
  107. shape=[num_examples, seq_length],
  108. dtype=tf.int32),
  109. })
  110. d = d.batch(batch_size=batch_size, drop_remainder=False)
  111. return d
  112. return input_fn
  113. def model_fn_builder(bert_config, init_checkpoint, layer_indexes, use_tpu,
  114. use_one_hot_embeddings):
  115. """Returns `model_fn` closure for TPUEstimator."""
  116. def model_fn(features, labels, mode, params): # pylint: disable=unused-argument
  117. """The `model_fn` for TPUEstimator."""
  118. unique_ids = features["unique_ids"]
  119. input_ids = features["input_ids"]
  120. input_mask = features["input_mask"]
  121. input_type_ids = features["input_type_ids"]
  122. model = modeling.BertModel(
  123. config=bert_config,
  124. is_training=False,
  125. input_ids=input_ids,
  126. input_mask=input_mask,
  127. token_type_ids=input_type_ids,
  128. use_one_hot_embeddings=use_one_hot_embeddings)
  129. if mode != tf.estimator.ModeKeys.PREDICT:
  130. raise ValueError("Only PREDICT modes are supported: %s" % (mode))
  131. tvars = tf.trainable_variables()
  132. scaffold_fn = None
  133. (assignment_map,
  134. initialized_variable_names) = modeling.get_assignment_map_from_checkpoint(
  135. tvars, init_checkpoint)
  136. if use_tpu:
  137. def tpu_scaffold():
  138. tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
  139. return tf.train.Scaffold()
  140. scaffold_fn = tpu_scaffold
  141. else:
  142. tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
  143. tf.logging.info("**** Trainable Variables ****")
  144. for var in tvars:
  145. init_string = ""
  146. if var.name in initialized_variable_names:
  147. init_string = ", *INIT_FROM_CKPT*"
  148. tf.logging.info(" name = %s, shape = %s%s", var.name, var.shape,
  149. init_string)
  150. all_layers = model.get_all_encoder_layers()
  151. predictions = {
  152. "unique_id": unique_ids,
  153. }
  154. for (i, layer_index) in enumerate(layer_indexes):
  155. predictions["layer_output_%d" % i] = all_layers[layer_index]
  156. output_spec = tf.contrib.tpu.TPUEstimatorSpec(
  157. mode=mode, predictions=predictions, scaffold_fn=scaffold_fn)
  158. return output_spec
  159. return model_fn
  160. def convert_examples_to_features(examples, seq_length, tokenizer):
  161. """Loads a data file into a list of `InputBatch`s."""
  162. features = []
  163. for (ex_index, example) in enumerate(examples):
  164. tokens_a = tokenizer.tokenize(example.text_a)
  165. tokens_b = None
  166. if example.text_b:
  167. tokens_b = tokenizer.tokenize(example.text_b)
  168. if tokens_b:
  169. # Modifies `tokens_a` and `tokens_b` in place so that the total
  170. # length is less than the specified length.
  171. # Account for [CLS], [SEP], [SEP] with "- 3"
  172. _truncate_seq_pair(tokens_a, tokens_b, seq_length - 3)
  173. else:
  174. # Account for [CLS] and [SEP] with "- 2"
  175. if len(tokens_a) > seq_length - 2:
  176. tokens_a = tokens_a[0:(seq_length - 2)]
  177. # The convention in BERT is:
  178. # (a) For sequence pairs:
  179. # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
  180. # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1
  181. # (b) For single sequences:
  182. # tokens: [CLS] the dog is hairy . [SEP]
  183. # type_ids: 0 0 0 0 0 0 0
  184. #
  185. # Where "type_ids" are used to indicate whether this is the first
  186. # sequence or the second sequence. The embedding vectors for `type=0` and
  187. # `type=1` were learned during pre-training and are added to the wordpiece
  188. # embedding vector (and position vector). This is not *strictly* necessary
  189. # since the [SEP] token unambiguously separates the sequences, but it makes
  190. # it easier for the model to learn the concept of sequences.
  191. #
  192. # For classification tasks, the first vector (corresponding to [CLS]) is
  193. # used as as the "sentence vector". Note that this only makes sense because
  194. # the entire model is fine-tuned.
  195. tokens = []
  196. input_type_ids = []
  197. tokens.append("[CLS]")
  198. input_type_ids.append(0)
  199. for token in tokens_a:
  200. tokens.append(token)
  201. input_type_ids.append(0)
  202. tokens.append("[SEP]")
  203. input_type_ids.append(0)
  204. if tokens_b:
  205. for token in tokens_b:
  206. tokens.append(token)
  207. input_type_ids.append(1)
  208. tokens.append("[SEP]")
  209. input_type_ids.append(1)
  210. input_ids = tokenizer.convert_tokens_to_ids(tokens)
  211. # The mask has 1 for real tokens and 0 for padding tokens. Only real
  212. # tokens are attended to.
  213. input_mask = [1] * len(input_ids)
  214. # Zero-pad up to the sequence length.
  215. while len(input_ids) < seq_length:
  216. input_ids.append(0)
  217. input_mask.append(0)
  218. input_type_ids.append(0)
  219. assert len(input_ids) == seq_length
  220. assert len(input_mask) == seq_length
  221. assert len(input_type_ids) == seq_length
  222. if ex_index < 5:
  223. tf.logging.info("*** Example ***")
  224. tf.logging.info("unique_id: %s" % (example.unique_id))
  225. tf.logging.info("tokens: %s" % " ".join(
  226. [tokenization.printable_text(x) for x in tokens]))
  227. tf.logging.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
  228. tf.logging.info("input_mask: %s" % " ".join([str(x) for x in input_mask]))
  229. tf.logging.info(
  230. "input_type_ids: %s" % " ".join([str(x) for x in input_type_ids]))
  231. features.append(
  232. InputFeatures(
  233. unique_id=example.unique_id,
  234. tokens=tokens,
  235. input_ids=input_ids,
  236. input_mask=input_mask,
  237. input_type_ids=input_type_ids))
  238. return features
  239. def _truncate_seq_pair(tokens_a, tokens_b, max_length):
  240. """Truncates a sequence pair in place to the maximum length."""
  241. # This is a simple heuristic which will always truncate the longer sequence
  242. # one token at a time. This makes more sense than truncating an equal percent
  243. # of tokens from each, since if one sequence is very short then each token
  244. # that's truncated likely contains more information than a longer sequence.
  245. while True:
  246. total_length = len(tokens_a) + len(tokens_b)
  247. if total_length <= max_length:
  248. break
  249. if len(tokens_a) > len(tokens_b):
  250. tokens_a.pop()
  251. else:
  252. tokens_b.pop()
  253. def read_examples(input_file):
  254. """Read a list of `InputExample`s from an input file."""
  255. examples = []
  256. unique_id = 0
  257. with tf.gfile.GFile(input_file, "r") as reader:
  258. while True:
  259. line = tokenization.convert_to_unicode(reader.readline())
  260. if not line:
  261. break
  262. line = line.strip()
  263. text_a = None
  264. text_b = None
  265. m = re.match(r"^(.*) \|\|\| (.*)$", line)
  266. if m is None:
  267. text_a = line
  268. else:
  269. text_a = m.group(1)
  270. text_b = m.group(2)
  271. examples.append(
  272. InputExample(unique_id=unique_id, text_a=text_a, text_b=text_b))
  273. unique_id += 1
  274. return examples
  275. def main(_):
  276. tf.logging.set_verbosity(tf.logging.INFO)
  277. layer_indexes = [int(x) for x in FLAGS.layers.split(",")]
  278. bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file)
  279. tokenizer = tokenization.FullTokenizer(
  280. vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)
  281. is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2
  282. run_config = tf.contrib.tpu.RunConfig(
  283. master=FLAGS.master,
  284. tpu_config=tf.contrib.tpu.TPUConfig(
  285. num_shards=FLAGS.num_tpu_cores,
  286. per_host_input_for_training=is_per_host))
  287. examples = read_examples(FLAGS.input_file)
  288. features = convert_examples_to_features(
  289. examples=examples, seq_length=FLAGS.max_seq_length, tokenizer=tokenizer)
  290. unique_id_to_feature = {}
  291. for feature in features:
  292. unique_id_to_feature[feature.unique_id] = feature
  293. model_fn = model_fn_builder(
  294. bert_config=bert_config,
  295. init_checkpoint=FLAGS.init_checkpoint,
  296. layer_indexes=layer_indexes,
  297. use_tpu=FLAGS.use_tpu,
  298. use_one_hot_embeddings=FLAGS.use_one_hot_embeddings)
  299. # If TPU is not available, this will fall back to normal Estimator on CPU
  300. # or GPU.
  301. estimator = tf.contrib.tpu.TPUEstimator(
  302. use_tpu=FLAGS.use_tpu,
  303. model_fn=model_fn,
  304. config=run_config,
  305. predict_batch_size=FLAGS.batch_size)
  306. input_fn = input_fn_builder(
  307. features=features, seq_length=FLAGS.max_seq_length)
  308. with codecs.getwriter("utf-8")(tf.gfile.Open(FLAGS.output_file,
  309. "w")) as writer:
  310. for result in estimator.predict(input_fn, yield_single_examples=True):
  311. unique_id = int(result["unique_id"])
  312. feature = unique_id_to_feature[unique_id]
  313. output_json = collections.OrderedDict()
  314. output_json["linex_index"] = unique_id
  315. all_features = []
  316. for (i, token) in enumerate(feature.tokens):
  317. all_layers = []
  318. for (j, layer_index) in enumerate(layer_indexes):
  319. layer_output = result["layer_output_%d" % j]
  320. layers = collections.OrderedDict()
  321. layers["index"] = layer_index
  322. layers["values"] = [
  323. round(float(x), 6) for x in layer_output[i:(i + 1)].flat
  324. ]
  325. all_layers.append(layers)
  326. features = collections.OrderedDict()
  327. features["token"] = token
  328. features["layers"] = all_layers
  329. all_features.append(features)
  330. output_json["features"] = all_features
  331. writer.write(json.dumps(output_json) + "\n")
  332. if __name__ == "__main__":
  333. flags.mark_flag_as_required("input_file")
  334. flags.mark_flag_as_required("vocab_file")
  335. flags.mark_flag_as_required("bert_config_file")
  336. flags.mark_flag_as_required("init_checkpoint")
  337. flags.mark_flag_as_required("output_file")
  338. tf.app.run()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...