Spaces:
Sleeping
Sleeping
SuperBigtoo
commited on
Commit
•
93acf27
1
Parent(s):
99819ca
adding model thai_news_classify
Browse files- bert/LICENSE +202 -0
- bert/README.md +155 -0
- bert/__pycache__/bpe_helper.cpython-311.pyc +0 -0
- bert/bpe_helper.py +52 -0
- bert/create_pretraining_data.py +455 -0
- bert/modeling.py +995 -0
- bert/optimization.py +171 -0
- bert/requirements.txt +5 -0
- bert/run_classifier.py +976 -0
- bert/run_pretraining.py +493 -0
- bert/tokenization.py +393 -0
bert/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
Apache License
|
3 |
+
Version 2.0, January 2004
|
4 |
+
http://www.apache.org/licenses/
|
5 |
+
|
6 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7 |
+
|
8 |
+
1. Definitions.
|
9 |
+
|
10 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
11 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
12 |
+
|
13 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14 |
+
the copyright owner that is granting the License.
|
15 |
+
|
16 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
17 |
+
other entities that control, are controlled by, or are under common
|
18 |
+
control with that entity. For the purposes of this definition,
|
19 |
+
"control" means (i) the power, direct or indirect, to cause the
|
20 |
+
direction or management of such entity, whether by contract or
|
21 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23 |
+
|
24 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25 |
+
exercising permissions granted by this License.
|
26 |
+
|
27 |
+
"Source" form shall mean the preferred form for making modifications,
|
28 |
+
including but not limited to software source code, documentation
|
29 |
+
source, and configuration files.
|
30 |
+
|
31 |
+
"Object" form shall mean any form resulting from mechanical
|
32 |
+
transformation or translation of a Source form, including but
|
33 |
+
not limited to compiled object code, generated documentation,
|
34 |
+
and conversions to other media types.
|
35 |
+
|
36 |
+
"Work" shall mean the work of authorship, whether in Source or
|
37 |
+
Object form, made available under the License, as indicated by a
|
38 |
+
copyright notice that is included in or attached to the work
|
39 |
+
(an example is provided in the Appendix below).
|
40 |
+
|
41 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42 |
+
form, that is based on (or derived from) the Work and for which the
|
43 |
+
editorial revisions, annotations, elaborations, or other modifications
|
44 |
+
represent, as a whole, an original work of authorship. For the purposes
|
45 |
+
of this License, Derivative Works shall not include works that remain
|
46 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47 |
+
the Work and Derivative Works thereof.
|
48 |
+
|
49 |
+
"Contribution" shall mean any work of authorship, including
|
50 |
+
the original version of the Work and any modifications or additions
|
51 |
+
to that Work or Derivative Works thereof, that is intentionally
|
52 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
55 |
+
means any form of electronic, verbal, or written communication sent
|
56 |
+
to the Licensor or its representatives, including but not limited to
|
57 |
+
communication on electronic mailing lists, source code control systems,
|
58 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
59 |
+
Licensor for the purpose of discussing and improving the Work, but
|
60 |
+
excluding communication that is conspicuously marked or otherwise
|
61 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
62 |
+
|
63 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64 |
+
on behalf of whom a Contribution has been received by Licensor and
|
65 |
+
subsequently incorporated within the Work.
|
66 |
+
|
67 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68 |
+
this License, each Contributor hereby grants to You a perpetual,
|
69 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70 |
+
copyright license to reproduce, prepare Derivative Works of,
|
71 |
+
publicly display, publicly perform, sublicense, and distribute the
|
72 |
+
Work and such Derivative Works in Source or Object form.
|
73 |
+
|
74 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75 |
+
this License, each Contributor hereby grants to You a perpetual,
|
76 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77 |
+
(except as stated in this section) patent license to make, have made,
|
78 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79 |
+
where such license applies only to those patent claims licensable
|
80 |
+
by such Contributor that are necessarily infringed by their
|
81 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
82 |
+
with the Work to which such Contribution(s) was submitted. If You
|
83 |
+
institute patent litigation against any entity (including a
|
84 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85 |
+
or a Contribution incorporated within the Work constitutes direct
|
86 |
+
or contributory patent infringement, then any patent licenses
|
87 |
+
granted to You under this License for that Work shall terminate
|
88 |
+
as of the date such litigation is filed.
|
89 |
+
|
90 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
91 |
+
Work or Derivative Works thereof in any medium, with or without
|
92 |
+
modifications, and in Source or Object form, provided that You
|
93 |
+
meet the following conditions:
|
94 |
+
|
95 |
+
(a) You must give any other recipients of the Work or
|
96 |
+
Derivative Works a copy of this License; and
|
97 |
+
|
98 |
+
(b) You must cause any modified files to carry prominent notices
|
99 |
+
stating that You changed the files; and
|
100 |
+
|
101 |
+
(c) You must retain, in the Source form of any Derivative Works
|
102 |
+
that You distribute, all copyright, patent, trademark, and
|
103 |
+
attribution notices from the Source form of the Work,
|
104 |
+
excluding those notices that do not pertain to any part of
|
105 |
+
the Derivative Works; and
|
106 |
+
|
107 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108 |
+
distribution, then any Derivative Works that You distribute must
|
109 |
+
include a readable copy of the attribution notices contained
|
110 |
+
within such NOTICE file, excluding those notices that do not
|
111 |
+
pertain to any part of the Derivative Works, in at least one
|
112 |
+
of the following places: within a NOTICE text file distributed
|
113 |
+
as part of the Derivative Works; within the Source form or
|
114 |
+
documentation, if provided along with the Derivative Works; or,
|
115 |
+
within a display generated by the Derivative Works, if and
|
116 |
+
wherever such third-party notices normally appear. The contents
|
117 |
+
of the NOTICE file are for informational purposes only and
|
118 |
+
do not modify the License. You may add Your own attribution
|
119 |
+
notices within Derivative Works that You distribute, alongside
|
120 |
+
or as an addendum to the NOTICE text from the Work, provided
|
121 |
+
that such additional attribution notices cannot be construed
|
122 |
+
as modifying the License.
|
123 |
+
|
124 |
+
You may add Your own copyright statement to Your modifications and
|
125 |
+
may provide additional or different license terms and conditions
|
126 |
+
for use, reproduction, or distribution of Your modifications, or
|
127 |
+
for any such Derivative Works as a whole, provided Your use,
|
128 |
+
reproduction, and distribution of the Work otherwise complies with
|
129 |
+
the conditions stated in this License.
|
130 |
+
|
131 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132 |
+
any Contribution intentionally submitted for inclusion in the Work
|
133 |
+
by You to the Licensor shall be under the terms and conditions of
|
134 |
+
this License, without any additional terms or conditions.
|
135 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136 |
+
the terms of any separate license agreement you may have executed
|
137 |
+
with Licensor regarding such Contributions.
|
138 |
+
|
139 |
+
6. Trademarks. This License does not grant permission to use the trade
|
140 |
+
names, trademarks, service marks, or product names of the Licensor,
|
141 |
+
except as required for reasonable and customary use in describing the
|
142 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
143 |
+
|
144 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145 |
+
agreed to in writing, Licensor provides the Work (and each
|
146 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148 |
+
implied, including, without limitation, any warranties or conditions
|
149 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151 |
+
appropriateness of using or redistributing the Work and assume any
|
152 |
+
risks associated with Your exercise of permissions under this License.
|
153 |
+
|
154 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
155 |
+
whether in tort (including negligence), contract, or otherwise,
|
156 |
+
unless required by applicable law (such as deliberate and grossly
|
157 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158 |
+
liable to You for damages, including any direct, indirect, special,
|
159 |
+
incidental, or consequential damages of any character arising as a
|
160 |
+
result of this License or out of the use or inability to use the
|
161 |
+
Work (including but not limited to damages for loss of goodwill,
|
162 |
+
work stoppage, computer failure or malfunction, or any and all
|
163 |
+
other commercial damages or losses), even if such Contributor
|
164 |
+
has been advised of the possibility of such damages.
|
165 |
+
|
166 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
168 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169 |
+
or other liability obligations and/or rights consistent with this
|
170 |
+
License. However, in accepting such obligations, You may act only
|
171 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172 |
+
of any other Contributor, and only if You agree to indemnify,
|
173 |
+
defend, and hold each Contributor harmless for any liability
|
174 |
+
incurred by, or claims asserted against, such Contributor by reason
|
175 |
+
of your accepting any such warranty or additional liability.
|
176 |
+
|
177 |
+
END OF TERMS AND CONDITIONS
|
178 |
+
|
179 |
+
APPENDIX: How to apply the Apache License to your work.
|
180 |
+
|
181 |
+
To apply the Apache License to your work, attach the following
|
182 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183 |
+
replaced with your own identifying information. (Don't include
|
184 |
+
the brackets!) The text should be enclosed in the appropriate
|
185 |
+
comment syntax for the file format. We also recommend that a
|
186 |
+
file or class name and description of purpose be included on the
|
187 |
+
same "printed page" as the copyright notice for easier
|
188 |
+
identification within third-party archives.
|
189 |
+
|
190 |
+
Copyright [yyyy] [name of copyright owner]
|
191 |
+
|
192 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193 |
+
you may not use this file except in compliance with the License.
|
194 |
+
You may obtain a copy of the License at
|
195 |
+
|
196 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
197 |
+
|
198 |
+
Unless required by applicable law or agreed to in writing, software
|
199 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201 |
+
See the License for the specific language governing permissions and
|
202 |
+
limitations under the License.
|
bert/README.md
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# BERT-th
|
3 |
+
|
4 |
+
Google's [**BERT**](https://github.com/google-research/bert) is currently the state-of-the-art method of pre-training text representations which additionally provides multilingual models. ~~Unfortunately, Thai is the only one in 103 languages that is excluded due to difficulties in word segmentation.~~
|
5 |
+
|
6 |
+
BERT-th presents the Thai-only pre-trained model based on the BERT-Base structure. It is now available to download.
|
7 |
+
* **[`BERT-Base, Thai`](https://drive.google.com/open?id=1J3uuXZr_Se_XIFHj7zlTJ-C9wzI9W_ot)**: BERT-Base architecture, Thai-only model
|
8 |
+
|
9 |
+
BERT-th also includes relevant codes and scripts along with the pre-trained model, all of which are the modified versions of those in the original BERT project.
|
10 |
+
|
11 |
+
## Preprocessing
|
12 |
+
|
13 |
+
### Data Source
|
14 |
+
|
15 |
+
Training data for BERT-th come from [the latest article dump of Thai Wikipedia](https://dumps.wikimedia.org/thwiki/latest/thwiki-latest-pages-articles.xml.bz2) on November 2, 2018. The raw texts are extracted by using [WikiExtractor](https://github.com/attardi/wikiextractor).
|
16 |
+
|
17 |
+
### Sentence Segmentation
|
18 |
+
|
19 |
+
Input data need to be segmented into separate sentences before further processing by BERT modules. Since Thai language has no explicit marker at the end of a sentence, it is quite problematic to pinpoint sentence boundaries. To the best of our knowledge, there is still no implementation of Thai sentence segmentation elsewhere. So, in this project, sentence segmentation is done by applying simple heuristics, considering spaces, sentence length and common conjunctions.
|
20 |
+
|
21 |
+
After preprocessing, the training corpus consists of approximately 2 million sentences and 40 million words (counting words after word segmentation by [PyThaiNLP](https://github.com/PyThaiNLP/pythainlp)). The plain and segmented texts can be downloaded **[`here`](https://drive.google.com/file/d/1QZSOpikO6Qc02gRmyeb_UiRLtTmUwGz1/view?usp=sharing)**.
|
22 |
+
|
23 |
+
## Tokenization
|
24 |
+
|
25 |
+
BERT uses [WordPiece](https://arxiv.org/pdf/1609.08144.pdf) as a tokenization mechanism. But it is Google internal, we cannot apply existing Thai word segmentation and then utilize WordPiece to learn the set of subword units. The best alternative is [SentencePiece](https://github.com/google/sentencepiece) which implements [BPE](https://arxiv.org/abs/1508.07909) and needs no word segmentation.
|
26 |
+
|
27 |
+
In this project, we adopt a pre-trained Thai SentencePiece model from [BPEmb](https://github.com/bheinzerling/bpemb). The model of 25000 vocabularies is chosen and the vocabulary file has to be augmented with BERT's special characters, including '[PAD]', '[CLS]', '[SEP]' and '[MASK]'. The model and vocabulary files can be downloaded **[`here`](https://drive.google.com/file/d/1F7pCgt3vPlarI9RxKtOZUrC_67KMNQ1W/view?usp=sharing)**.
|
28 |
+
|
29 |
+
`SentencePiece` and `bpe_helper.py` from BPEmb are both used to tokenize data. `ThaiTokenizer class` has been added to BERT's `tokenization.py` for tokenizing Thai texts.
|
30 |
+
|
31 |
+
## Pre-training
|
32 |
+
|
33 |
+
The data can be prepared before pre-training by using this script.
|
34 |
+
|
35 |
+
```shell
|
36 |
+
export BPE_DIR=/path/to/bpe
|
37 |
+
export TEXT_DIR=/path/to/text
|
38 |
+
export DATA_DIR=/path/to/data
|
39 |
+
|
40 |
+
python create_pretraining_data.py \
|
41 |
+
--input_file=$TEXT_DIR/thaiwikitext_sentseg \
|
42 |
+
--output_file=$DATA_DIR/tf_examples.tfrecord \
|
43 |
+
--vocab_file=$BPE_DIR/th.wiki.bpe.op25000.vocab \
|
44 |
+
--max_seq_length=128 \
|
45 |
+
--max_predictions_per_seq=20 \
|
46 |
+
--masked_lm_prob=0.15 \
|
47 |
+
--random_seed=12345 \
|
48 |
+
--dupe_factor=5 \
|
49 |
+
--thai_text=True \
|
50 |
+
--spm_file=$BPE_DIR/th.wiki.bpe.op25000.model
|
51 |
+
```
|
52 |
+
|
53 |
+
Then, the following script can be run to learn a model from scratch.
|
54 |
+
|
55 |
+
```shell
|
56 |
+
export DATA_DIR=/path/to/data
|
57 |
+
export BERT_BASE_DIR=/path/to/bert_base
|
58 |
+
|
59 |
+
python run_pretraining.py \
|
60 |
+
--input_file=$DATA_DIR/tf_examples.tfrecord \
|
61 |
+
--output_dir=$BERT_BASE_DIR \
|
62 |
+
--do_train=True \
|
63 |
+
--do_eval=True \
|
64 |
+
--bert_config_file=$BERT_BASE_DIR/bert_config.json \
|
65 |
+
--train_batch_size=32 \
|
66 |
+
--max_seq_length=128 \
|
67 |
+
--max_predictions_per_seq=20 \
|
68 |
+
--num_train_steps=1000000 \
|
69 |
+
--num_warmup_steps=100000 \
|
70 |
+
--learning_rate=1e-4 \
|
71 |
+
--save_checkpoints_steps=200000
|
72 |
+
```
|
73 |
+
|
74 |
+
We have trained the model for 1 million steps. On Tesla K80 GPU, it took around 20 days to complete. Though, we provide a snapshot at 0.8 million steps because it yields better results for downstream classification tasks.
|
75 |
+
|
76 |
+
## Downstream Classification Tasks
|
77 |
+
|
78 |
+
### XNLI
|
79 |
+
|
80 |
+
[XNLI](http://www.nyu.edu/projects/bowman/xnli/) is a dataset for evaluating a cross-lingual inferential classification task. The development and test sets contain 15 languages which data are thoroughly edited. The machine-translated versions of training data are also provided.
|
81 |
+
|
82 |
+
The Thai-only pre-trained BERT model can be applied to the XNLI task by using training data which are translated to Thai. Spaces between words in the training data need to be removed to make them consistent with inputs in the pre-training step. The processed files of XNLI related to Thai language can be downloaded **[`here`](https://drive.google.com/file/d/1ZAk1JfR6a0TSCkeyQ-EkRtk1w_mQDWFG/view?usp=sharing)**.
|
83 |
+
|
84 |
+
Afterwards, the XNLI task can be learned by using this script.
|
85 |
+
|
86 |
+
```shell
|
87 |
+
export BPE_DIR=/path/to/bpe
|
88 |
+
export XNLI_DIR=/path/to/xnli
|
89 |
+
export OUTPUT_DIR=/path/to/output
|
90 |
+
export BERT_BASE_DIR=/path/to/bert_base
|
91 |
+
|
92 |
+
python run_classifier.py \
|
93 |
+
--task_name=XNLI \
|
94 |
+
--do_train=true \
|
95 |
+
--do_eval=true \
|
96 |
+
--data_dir=$XNLI_DIR \
|
97 |
+
--vocab_file=$BPE_DIR/th.wiki.bpe.op25000.vocab \
|
98 |
+
--bert_config_file=$BERT_BASE_DIR/bert_config.json \
|
99 |
+
--init_checkpoint=$BERT_BASE_DIR/model.ckpt \
|
100 |
+
--max_seq_length=128 \
|
101 |
+
--train_batch_size=32 \
|
102 |
+
--learning_rate=5e-5 \
|
103 |
+
--num_train_epochs=2.0 \
|
104 |
+
--output_dir=$OUTPUT_DIR \
|
105 |
+
--xnli_language=th \
|
106 |
+
--spm_file=$BPE_DIR/th.wiki.bpe.op25000.model
|
107 |
+
```
|
108 |
+
|
109 |
+
This table compares the Thai-only model with XNLI baselines and the Multilingual Cased model which is also trained by using translated data.
|
110 |
+
|
111 |
+
<!-- Use html table because github markdown doesn't support colspan -->
|
112 |
+
<table>
|
113 |
+
<tr>
|
114 |
+
<td colspan="2" align="center"><b>XNLI Baseline</b></td>
|
115 |
+
<td colspan="2" align="center"><b>BERT</b></td>
|
116 |
+
</tr>
|
117 |
+
<tr>
|
118 |
+
<td align="center">Translate Train</td>
|
119 |
+
<td align="center">Translate Test</td>
|
120 |
+
<td align="center">Multilingual Model</td>
|
121 |
+
<td align="center">Thai-only Model</td>
|
122 |
+
</tr>
|
123 |
+
<td align="center">62.8</td>
|
124 |
+
<td align="center">64.4</td>
|
125 |
+
<td align="center">66.1</td>
|
126 |
+
<td align="center"><b>68.9</b></td>
|
127 |
+
</table>
|
128 |
+
|
129 |
+
### Wongnai Review Dataset
|
130 |
+
|
131 |
+
Wongnai Review Dataset collects restaurant reviews and ratings from [Wongnai](https://www.wongnai.com/) website. The task is to classify a review into one of five ratings (1 to 5 stars). The dataset can be downloaded **[`here`](https://github.com/wongnai/wongnai-corpus)** and the following script can be run to use the Thai-only model for this task.
|
132 |
+
|
133 |
+
```shell
|
134 |
+
export BPE_DIR=/path/to/bpe
|
135 |
+
export WONGNAI_DIR=/path/to/wongnai
|
136 |
+
export OUTPUT_DIR=/path/to/output
|
137 |
+
export BERT_BASE_DIR=/path/to/bert_base
|
138 |
+
|
139 |
+
python run_classifier.py \
|
140 |
+
--task_name=wongnai \
|
141 |
+
--do_train=true \
|
142 |
+
--do_predict=true \
|
143 |
+
--data_dir=$WONGNAI_DIR \
|
144 |
+
--vocab_file=$BPE_DIR/th.wiki.bpe.op25000.vocab \
|
145 |
+
--bert_config_file=$BERT_BASE_DIR/bert_config.json \
|
146 |
+
--init_checkpoint=$BERT_BASE_DIR/model.ckpt \
|
147 |
+
--max_seq_length=128 \
|
148 |
+
--train_batch_size=32 \
|
149 |
+
--learning_rate=5e-5 \
|
150 |
+
--num_train_epochs=2.0 \
|
151 |
+
--output_dir=$OUTPUT_DIR \
|
152 |
+
--spm_file=$BPE_DIR/th.wiki.bpe.op25000.model
|
153 |
+
```
|
154 |
+
|
155 |
+
Without additional preprocessing and further fine-tuning, the Thai-only BERT model can achieve 0.56612 and 0.57057 for public and private test-set scores respectively.
|
bert/__pycache__/bpe_helper.cpython-311.pyc
ADDED
Binary file (4.15 kB). View file
|
|
bert/bpe_helper.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from math import log
|
2 |
+
|
3 |
+
|
4 |
+
class BPE(object):
|
5 |
+
|
6 |
+
def __init__(self, vocab_file):
|
7 |
+
with open(vocab_file, encoding="utf8") as f:
|
8 |
+
self.words = [l.split()[0] for l in f]
|
9 |
+
log_len = log(len(self.words))
|
10 |
+
self.wordcost = {
|
11 |
+
k: log((i+1) * log_len)
|
12 |
+
for i, k in enumerate(self.words)}
|
13 |
+
self.maxword = max(len(x) for x in self.words)
|
14 |
+
|
15 |
+
def encode(self, s):
|
16 |
+
"""Uses dynamic programming to infer the location of spaces in a string
|
17 |
+
without spaces."""
|
18 |
+
|
19 |
+
s = s.replace(" ", "▁")
|
20 |
+
|
21 |
+
# Find the best match for the i first characters, assuming cost has
|
22 |
+
# been built for the i-1 first characters.
|
23 |
+
# Returns a pair (match_cost, match_length).
|
24 |
+
def best_match(i):
|
25 |
+
candidates = enumerate(reversed(cost[max(0, i - self.maxword):i]))
|
26 |
+
return min(
|
27 |
+
(c + self.wordcost.get(s[i-k-1:i], 9e999), k+1)
|
28 |
+
for k, c in candidates)
|
29 |
+
|
30 |
+
# Build the cost array.
|
31 |
+
cost = [0]
|
32 |
+
for i in range(1, len(s) + 1):
|
33 |
+
c, k = best_match(i)
|
34 |
+
cost.append(c)
|
35 |
+
|
36 |
+
# Backtrack to recover the minimal-cost string.
|
37 |
+
out = []
|
38 |
+
i = len(s)
|
39 |
+
while i > 0:
|
40 |
+
c, k = best_match(i)
|
41 |
+
assert c == cost[i]
|
42 |
+
out.append(s[i-k:i])
|
43 |
+
|
44 |
+
i -= k
|
45 |
+
|
46 |
+
return " ".join(reversed(out))
|
47 |
+
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
bpe = BPE("en.wiki.bpe.op25000.vocab")
|
51 |
+
print(bpe.encode(' this is our house in boomchakalaka'))
|
52 |
+
# >>> ▁this ▁is ▁our ▁house ▁in ▁boom ch ak al aka
|
bert/create_pretraining_data.py
ADDED
@@ -0,0 +1,455 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""Create masked LM/next sentence masked_lm TF examples for BERT."""
|
16 |
+
|
17 |
+
from __future__ import absolute_import
|
18 |
+
from __future__ import division
|
19 |
+
from __future__ import print_function
|
20 |
+
|
21 |
+
import collections
|
22 |
+
import random
|
23 |
+
|
24 |
+
import tokenization
|
25 |
+
import tensorflow as tf
|
26 |
+
|
27 |
+
flags = tf.flags
|
28 |
+
|
29 |
+
FLAGS = flags.FLAGS
|
30 |
+
|
31 |
+
flags.DEFINE_string("input_file", None,
|
32 |
+
"Input raw text file (or comma-separated list of files).")
|
33 |
+
|
34 |
+
flags.DEFINE_string(
|
35 |
+
"output_file", None,
|
36 |
+
"Output TF example file (or comma-separated list of files).")
|
37 |
+
|
38 |
+
flags.DEFINE_string("vocab_file", None,
|
39 |
+
"The vocabulary file that the BERT model was trained on.")
|
40 |
+
|
41 |
+
flags.DEFINE_bool(
|
42 |
+
"do_lower_case", True,
|
43 |
+
"Whether to lower case the input text. Should be True for uncased "
|
44 |
+
"models and False for cased models.")
|
45 |
+
|
46 |
+
flags.DEFINE_integer("max_seq_length", 128, "Maximum sequence length.")
|
47 |
+
|
48 |
+
flags.DEFINE_integer("max_predictions_per_seq", 20,
|
49 |
+
"Maximum number of masked LM predictions per sequence.")
|
50 |
+
|
51 |
+
flags.DEFINE_integer("random_seed", 12345, "Random seed for data generation.")
|
52 |
+
|
53 |
+
flags.DEFINE_integer(
|
54 |
+
"dupe_factor", 10,
|
55 |
+
"Number of times to duplicate the input data (with different masks).")
|
56 |
+
|
57 |
+
flags.DEFINE_float("masked_lm_prob", 0.15, "Masked LM probability.")
|
58 |
+
|
59 |
+
flags.DEFINE_float(
|
60 |
+
"short_seq_prob", 0.1,
|
61 |
+
"Probability of creating sequences which are shorter than the "
|
62 |
+
"maximum length.")
|
63 |
+
|
64 |
+
flags.DEFINE_bool(
|
65 |
+
"thai_text", False,
|
66 |
+
"Whether to process Thai language.")
|
67 |
+
|
68 |
+
flags.DEFINE_string(
|
69 |
+
"spm_file", None,
|
70 |
+
"SentencePiece model file for Thai language.")
|
71 |
+
|
72 |
+
|
73 |
+
class TrainingInstance(object):
|
74 |
+
"""A single training instance (sentence pair)."""
|
75 |
+
|
76 |
+
def __init__(self, tokens, segment_ids, masked_lm_positions, masked_lm_labels,
|
77 |
+
is_random_next):
|
78 |
+
self.tokens = tokens
|
79 |
+
self.segment_ids = segment_ids
|
80 |
+
self.is_random_next = is_random_next
|
81 |
+
self.masked_lm_positions = masked_lm_positions
|
82 |
+
self.masked_lm_labels = masked_lm_labels
|
83 |
+
|
84 |
+
def __str__(self):
|
85 |
+
s = ""
|
86 |
+
s += "tokens: %s\n" % (" ".join(
|
87 |
+
[tokenization.printable_text(x) for x in self.tokens]))
|
88 |
+
s += "segment_ids: %s\n" % (" ".join([str(x) for x in self.segment_ids]))
|
89 |
+
s += "is_random_next: %s\n" % self.is_random_next
|
90 |
+
s += "masked_lm_positions: %s\n" % (" ".join(
|
91 |
+
[str(x) for x in self.masked_lm_positions]))
|
92 |
+
s += "masked_lm_labels: %s\n" % (" ".join(
|
93 |
+
[tokenization.printable_text(x) for x in self.masked_lm_labels]))
|
94 |
+
s += "\n"
|
95 |
+
return s
|
96 |
+
|
97 |
+
def __repr__(self):
|
98 |
+
return self.__str__()
|
99 |
+
|
100 |
+
|
101 |
+
def write_instance_to_example_files(instances, tokenizer, max_seq_length,
|
102 |
+
max_predictions_per_seq, output_files):
|
103 |
+
"""Create TF example files from `TrainingInstance`s."""
|
104 |
+
writers = []
|
105 |
+
for output_file in output_files:
|
106 |
+
writers.append(tf.python_io.TFRecordWriter(output_file))
|
107 |
+
|
108 |
+
writer_index = 0
|
109 |
+
|
110 |
+
total_written = 0
|
111 |
+
for (inst_index, instance) in enumerate(instances):
|
112 |
+
input_ids = tokenizer.convert_tokens_to_ids(instance.tokens)
|
113 |
+
input_mask = [1] * len(input_ids)
|
114 |
+
segment_ids = list(instance.segment_ids)
|
115 |
+
assert len(input_ids) <= max_seq_length
|
116 |
+
|
117 |
+
while len(input_ids) < max_seq_length:
|
118 |
+
input_ids.append(0)
|
119 |
+
input_mask.append(0)
|
120 |
+
segment_ids.append(0)
|
121 |
+
|
122 |
+
assert len(input_ids) == max_seq_length
|
123 |
+
assert len(input_mask) == max_seq_length
|
124 |
+
assert len(segment_ids) == max_seq_length
|
125 |
+
|
126 |
+
masked_lm_positions = list(instance.masked_lm_positions)
|
127 |
+
masked_lm_ids = tokenizer.convert_tokens_to_ids(instance.masked_lm_labels)
|
128 |
+
masked_lm_weights = [1.0] * len(masked_lm_ids)
|
129 |
+
|
130 |
+
while len(masked_lm_positions) < max_predictions_per_seq:
|
131 |
+
masked_lm_positions.append(0)
|
132 |
+
masked_lm_ids.append(0)
|
133 |
+
masked_lm_weights.append(0.0)
|
134 |
+
|
135 |
+
next_sentence_label = 1 if instance.is_random_next else 0
|
136 |
+
|
137 |
+
features = collections.OrderedDict()
|
138 |
+
features["input_ids"] = create_int_feature(input_ids)
|
139 |
+
features["input_mask"] = create_int_feature(input_mask)
|
140 |
+
features["segment_ids"] = create_int_feature(segment_ids)
|
141 |
+
features["masked_lm_positions"] = create_int_feature(masked_lm_positions)
|
142 |
+
features["masked_lm_ids"] = create_int_feature(masked_lm_ids)
|
143 |
+
features["masked_lm_weights"] = create_float_feature(masked_lm_weights)
|
144 |
+
features["next_sentence_labels"] = create_int_feature([next_sentence_label])
|
145 |
+
|
146 |
+
tf_example = tf.train.Example(features=tf.train.Features(feature=features))
|
147 |
+
|
148 |
+
writers[writer_index].write(tf_example.SerializeToString())
|
149 |
+
writer_index = (writer_index + 1) % len(writers)
|
150 |
+
|
151 |
+
total_written += 1
|
152 |
+
|
153 |
+
if inst_index < 20:
|
154 |
+
tf.logging.info("*** Example ***")
|
155 |
+
tf.logging.info("tokens: %s" % " ".join(
|
156 |
+
[tokenization.printable_text(x) for x in instance.tokens]))
|
157 |
+
|
158 |
+
for feature_name in features.keys():
|
159 |
+
feature = features[feature_name]
|
160 |
+
values = []
|
161 |
+
if feature.int64_list.value:
|
162 |
+
values = feature.int64_list.value
|
163 |
+
elif feature.float_list.value:
|
164 |
+
values = feature.float_list.value
|
165 |
+
tf.logging.info(
|
166 |
+
"%s: %s" % (feature_name, " ".join([str(x) for x in values])))
|
167 |
+
|
168 |
+
for writer in writers:
|
169 |
+
writer.close()
|
170 |
+
|
171 |
+
tf.logging.info("Wrote %d total instances", total_written)
|
172 |
+
|
173 |
+
|
174 |
+
def create_int_feature(values):
|
175 |
+
feature = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values)))
|
176 |
+
return feature
|
177 |
+
|
178 |
+
|
179 |
+
def create_float_feature(values):
|
180 |
+
feature = tf.train.Feature(float_list=tf.train.FloatList(value=list(values)))
|
181 |
+
return feature
|
182 |
+
|
183 |
+
|
184 |
+
def create_training_instances(input_files, tokenizer, max_seq_length,
|
185 |
+
dupe_factor, short_seq_prob, masked_lm_prob,
|
186 |
+
max_predictions_per_seq, rng):
|
187 |
+
"""Create `TrainingInstance`s from raw text."""
|
188 |
+
all_documents = [[]]
|
189 |
+
|
190 |
+
# Input file format:
|
191 |
+
# (1) One sentence per line. These should ideally be actual sentences, not
|
192 |
+
# entire paragraphs or arbitrary spans of text. (Because we use the
|
193 |
+
# sentence boundaries for the "next sentence prediction" task).
|
194 |
+
# (2) Blank lines between documents. Document boundaries are needed so
|
195 |
+
# that the "next sentence prediction" task doesn't span between documents.
|
196 |
+
for input_file in input_files:
|
197 |
+
with tf.gfile.GFile(input_file, "r") as reader:
|
198 |
+
while True:
|
199 |
+
line = tokenization.convert_to_unicode(reader.readline())
|
200 |
+
if not line:
|
201 |
+
break
|
202 |
+
line = line.strip()
|
203 |
+
|
204 |
+
# Empty lines are used as document delimiters
|
205 |
+
if not line:
|
206 |
+
all_documents.append([])
|
207 |
+
tokens = tokenizer.tokenize(line)
|
208 |
+
if tokens:
|
209 |
+
all_documents[-1].append(tokens)
|
210 |
+
|
211 |
+
# Remove empty documents
|
212 |
+
all_documents = [x for x in all_documents if x]
|
213 |
+
rng.shuffle(all_documents)
|
214 |
+
|
215 |
+
vocab_words = list(tokenizer.vocab.keys())
|
216 |
+
instances = []
|
217 |
+
for _ in range(dupe_factor):
|
218 |
+
for document_index in range(len(all_documents)):
|
219 |
+
instances.extend(
|
220 |
+
create_instances_from_document(
|
221 |
+
all_documents, document_index, max_seq_length, short_seq_prob,
|
222 |
+
masked_lm_prob, max_predictions_per_seq, vocab_words, rng))
|
223 |
+
|
224 |
+
rng.shuffle(instances)
|
225 |
+
return instances
|
226 |
+
|
227 |
+
|
228 |
+
def create_instances_from_document(
|
229 |
+
all_documents, document_index, max_seq_length, short_seq_prob,
|
230 |
+
masked_lm_prob, max_predictions_per_seq, vocab_words, rng):
|
231 |
+
"""Creates `TrainingInstance`s for a single document."""
|
232 |
+
document = all_documents[document_index]
|
233 |
+
|
234 |
+
# Account for [CLS], [SEP], [SEP]
|
235 |
+
max_num_tokens = max_seq_length - 3
|
236 |
+
|
237 |
+
# We *usually* want to fill up the entire sequence since we are padding
|
238 |
+
# to `max_seq_length` anyways, so short sequences are generally wasted
|
239 |
+
# computation. However, we *sometimes*
|
240 |
+
# (i.e., short_seq_prob == 0.1 == 10% of the time) want to use shorter
|
241 |
+
# sequences to minimize the mismatch between pre-training and fine-tuning.
|
242 |
+
# The `target_seq_length` is just a rough target however, whereas
|
243 |
+
# `max_seq_length` is a hard limit.
|
244 |
+
target_seq_length = max_num_tokens
|
245 |
+
if rng.random() < short_seq_prob:
|
246 |
+
target_seq_length = rng.randint(2, max_num_tokens)
|
247 |
+
|
248 |
+
# We DON'T just concatenate all of the tokens from a document into a long
|
249 |
+
# sequence and choose an arbitrary split point because this would make the
|
250 |
+
# next sentence prediction task too easy. Instead, we split the input into
|
251 |
+
# segments "A" and "B" based on the actual "sentences" provided by the user
|
252 |
+
# input.
|
253 |
+
instances = []
|
254 |
+
current_chunk = []
|
255 |
+
current_length = 0
|
256 |
+
i = 0
|
257 |
+
while i < len(document):
|
258 |
+
segment = document[i]
|
259 |
+
current_chunk.append(segment)
|
260 |
+
current_length += len(segment)
|
261 |
+
if i == len(document) - 1 or current_length >= target_seq_length:
|
262 |
+
if current_chunk:
|
263 |
+
# `a_end` is how many segments from `current_chunk` go into the `A`
|
264 |
+
# (first) sentence.
|
265 |
+
a_end = 1
|
266 |
+
if len(current_chunk) >= 2:
|
267 |
+
a_end = rng.randint(1, len(current_chunk) - 1)
|
268 |
+
|
269 |
+
tokens_a = []
|
270 |
+
for j in range(a_end):
|
271 |
+
tokens_a.extend(current_chunk[j])
|
272 |
+
|
273 |
+
tokens_b = []
|
274 |
+
# Random next
|
275 |
+
is_random_next = False
|
276 |
+
if len(current_chunk) == 1 or rng.random() < 0.5:
|
277 |
+
is_random_next = True
|
278 |
+
target_b_length = target_seq_length - len(tokens_a)
|
279 |
+
|
280 |
+
# This should rarely go for more than one iteration for large
|
281 |
+
# corpora. However, just to be careful, we try to make sure that
|
282 |
+
# the random document is not the same as the document
|
283 |
+
# we're processing.
|
284 |
+
for _ in range(10):
|
285 |
+
random_document_index = rng.randint(0, len(all_documents) - 1)
|
286 |
+
if random_document_index != document_index:
|
287 |
+
break
|
288 |
+
|
289 |
+
random_document = all_documents[random_document_index]
|
290 |
+
random_start = rng.randint(0, len(random_document) - 1)
|
291 |
+
for j in range(random_start, len(random_document)):
|
292 |
+
tokens_b.extend(random_document[j])
|
293 |
+
if len(tokens_b) >= target_b_length:
|
294 |
+
break
|
295 |
+
# We didn't actually use these segments so we "put them back" so
|
296 |
+
# they don't go to waste.
|
297 |
+
num_unused_segments = len(current_chunk) - a_end
|
298 |
+
i -= num_unused_segments
|
299 |
+
# Actual next
|
300 |
+
else:
|
301 |
+
is_random_next = False
|
302 |
+
for j in range(a_end, len(current_chunk)):
|
303 |
+
tokens_b.extend(current_chunk[j])
|
304 |
+
truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng)
|
305 |
+
|
306 |
+
assert len(tokens_a) >= 1
|
307 |
+
assert len(tokens_b) >= 1
|
308 |
+
|
309 |
+
tokens = []
|
310 |
+
segment_ids = []
|
311 |
+
tokens.append("[CLS]")
|
312 |
+
segment_ids.append(0)
|
313 |
+
for token in tokens_a:
|
314 |
+
tokens.append(token)
|
315 |
+
segment_ids.append(0)
|
316 |
+
|
317 |
+
tokens.append("[SEP]")
|
318 |
+
segment_ids.append(0)
|
319 |
+
|
320 |
+
for token in tokens_b:
|
321 |
+
tokens.append(token)
|
322 |
+
segment_ids.append(1)
|
323 |
+
tokens.append("[SEP]")
|
324 |
+
segment_ids.append(1)
|
325 |
+
|
326 |
+
(tokens, masked_lm_positions,
|
327 |
+
masked_lm_labels) = create_masked_lm_predictions(
|
328 |
+
tokens, masked_lm_prob, max_predictions_per_seq, vocab_words, rng)
|
329 |
+
instance = TrainingInstance(
|
330 |
+
tokens=tokens,
|
331 |
+
segment_ids=segment_ids,
|
332 |
+
is_random_next=is_random_next,
|
333 |
+
masked_lm_positions=masked_lm_positions,
|
334 |
+
masked_lm_labels=masked_lm_labels)
|
335 |
+
instances.append(instance)
|
336 |
+
current_chunk = []
|
337 |
+
current_length = 0
|
338 |
+
i += 1
|
339 |
+
|
340 |
+
return instances
|
341 |
+
|
342 |
+
|
343 |
+
def create_masked_lm_predictions(tokens, masked_lm_prob,
|
344 |
+
max_predictions_per_seq, vocab_words, rng):
|
345 |
+
"""Creates the predictions for the masked LM objective."""
|
346 |
+
|
347 |
+
cand_indexes = []
|
348 |
+
for (i, token) in enumerate(tokens):
|
349 |
+
if token == "[CLS]" or token == "[SEP]":
|
350 |
+
continue
|
351 |
+
cand_indexes.append(i)
|
352 |
+
|
353 |
+
rng.shuffle(cand_indexes)
|
354 |
+
|
355 |
+
output_tokens = list(tokens)
|
356 |
+
|
357 |
+
masked_lm = collections.namedtuple("masked_lm", ["index", "label"]) # pylint: disable=invalid-name
|
358 |
+
|
359 |
+
num_to_predict = min(max_predictions_per_seq,
|
360 |
+
max(1, int(round(len(tokens) * masked_lm_prob))))
|
361 |
+
|
362 |
+
masked_lms = []
|
363 |
+
covered_indexes = set()
|
364 |
+
for index in cand_indexes:
|
365 |
+
if len(masked_lms) >= num_to_predict:
|
366 |
+
break
|
367 |
+
if index in covered_indexes:
|
368 |
+
continue
|
369 |
+
covered_indexes.add(index)
|
370 |
+
|
371 |
+
masked_token = None
|
372 |
+
# 80% of the time, replace with [MASK]
|
373 |
+
if rng.random() < 0.8:
|
374 |
+
masked_token = "[MASK]"
|
375 |
+
else:
|
376 |
+
# 10% of the time, keep original
|
377 |
+
if rng.random() < 0.5:
|
378 |
+
masked_token = tokens[index]
|
379 |
+
# 10% of the time, replace with random word
|
380 |
+
else:
|
381 |
+
masked_token = vocab_words[rng.randint(0, len(vocab_words) - 1)]
|
382 |
+
|
383 |
+
output_tokens[index] = masked_token
|
384 |
+
|
385 |
+
masked_lms.append(masked_lm(index=index, label=tokens[index]))
|
386 |
+
|
387 |
+
masked_lms = sorted(masked_lms, key=lambda x: x.index)
|
388 |
+
|
389 |
+
masked_lm_positions = []
|
390 |
+
masked_lm_labels = []
|
391 |
+
for p in masked_lms:
|
392 |
+
masked_lm_positions.append(p.index)
|
393 |
+
masked_lm_labels.append(p.label)
|
394 |
+
|
395 |
+
return (output_tokens, masked_lm_positions, masked_lm_labels)
|
396 |
+
|
397 |
+
|
398 |
+
def truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng):
|
399 |
+
"""Truncates a pair of sequences to a maximum sequence length."""
|
400 |
+
while True:
|
401 |
+
total_length = len(tokens_a) + len(tokens_b)
|
402 |
+
if total_length <= max_num_tokens:
|
403 |
+
break
|
404 |
+
|
405 |
+
trunc_tokens = tokens_a if len(tokens_a) > len(tokens_b) else tokens_b
|
406 |
+
assert len(trunc_tokens) >= 1
|
407 |
+
|
408 |
+
# We want to sometimes truncate from the front and sometimes from the
|
409 |
+
# back to add more randomness and avoid biases.
|
410 |
+
if rng.random() < 0.5:
|
411 |
+
del trunc_tokens[0]
|
412 |
+
else:
|
413 |
+
trunc_tokens.pop()
|
414 |
+
|
415 |
+
|
416 |
+
def main(_):
|
417 |
+
tf.logging.set_verbosity(tf.logging.INFO)
|
418 |
+
|
419 |
+
if FLAGS.thai_text:
|
420 |
+
if not FLAGS.spm_file:
|
421 |
+
print("Please specify the SentencePiece model file by using --spm_file.")
|
422 |
+
return
|
423 |
+
tokenizer = tokenization.ThaiTokenizer(vocab_file=FLAGS.vocab_file, spm_file=FLAGS.spm_file)
|
424 |
+
else:
|
425 |
+
tokenizer = tokenization.FullTokenizer(
|
426 |
+
vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)
|
427 |
+
|
428 |
+
input_files = []
|
429 |
+
for input_pattern in FLAGS.input_file.split(","):
|
430 |
+
input_files.extend(tf.gfile.Glob(input_pattern))
|
431 |
+
|
432 |
+
tf.logging.info("*** Reading from input files ***")
|
433 |
+
for input_file in input_files:
|
434 |
+
tf.logging.info(" %s", input_file)
|
435 |
+
|
436 |
+
rng = random.Random(FLAGS.random_seed)
|
437 |
+
instances = create_training_instances(
|
438 |
+
input_files, tokenizer, FLAGS.max_seq_length, FLAGS.dupe_factor,
|
439 |
+
FLAGS.short_seq_prob, FLAGS.masked_lm_prob, FLAGS.max_predictions_per_seq,
|
440 |
+
rng)
|
441 |
+
|
442 |
+
output_files = FLAGS.output_file.split(",")
|
443 |
+
tf.logging.info("*** Writing to output files ***")
|
444 |
+
for output_file in output_files:
|
445 |
+
tf.logging.info(" %s", output_file)
|
446 |
+
|
447 |
+
write_instance_to_example_files(instances, tokenizer, FLAGS.max_seq_length,
|
448 |
+
FLAGS.max_predictions_per_seq, output_files)
|
449 |
+
|
450 |
+
|
451 |
+
if __name__ == "__main__":
|
452 |
+
flags.mark_flag_as_required("input_file")
|
453 |
+
flags.mark_flag_as_required("output_file")
|
454 |
+
flags.mark_flag_as_required("vocab_file")
|
455 |
+
tf.app.run()
|
bert/modeling.py
ADDED
@@ -0,0 +1,995 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""The main BERT model and related functions."""
|
16 |
+
|
17 |
+
from __future__ import absolute_import
|
18 |
+
from __future__ import division
|
19 |
+
from __future__ import print_function
|
20 |
+
|
21 |
+
import collections
|
22 |
+
import copy
|
23 |
+
import json
|
24 |
+
import math
|
25 |
+
import re
|
26 |
+
import six
|
27 |
+
import tensorflow as tf
|
28 |
+
|
29 |
+
|
30 |
+
class BertConfig(object):
|
31 |
+
"""Configuration for `BertModel`."""
|
32 |
+
|
33 |
+
def __init__(self,
|
34 |
+
vocab_size,
|
35 |
+
hidden_size=768,
|
36 |
+
num_hidden_layers=12,
|
37 |
+
num_attention_heads=12,
|
38 |
+
intermediate_size=3072,
|
39 |
+
hidden_act="gelu",
|
40 |
+
hidden_dropout_prob=0.1,
|
41 |
+
attention_probs_dropout_prob=0.1,
|
42 |
+
max_position_embeddings=512,
|
43 |
+
type_vocab_size=16,
|
44 |
+
initializer_range=0.02):
|
45 |
+
"""Constructs BertConfig.
|
46 |
+
|
47 |
+
Args:
|
48 |
+
vocab_size: Vocabulary size of `inputs_ids` in `BertModel`.
|
49 |
+
hidden_size: Size of the encoder layers and the pooler layer.
|
50 |
+
num_hidden_layers: Number of hidden layers in the Transformer encoder.
|
51 |
+
num_attention_heads: Number of attention heads for each attention layer in
|
52 |
+
the Transformer encoder.
|
53 |
+
intermediate_size: The size of the "intermediate" (i.e., feed-forward)
|
54 |
+
layer in the Transformer encoder.
|
55 |
+
hidden_act: The non-linear activation function (function or string) in the
|
56 |
+
encoder and pooler.
|
57 |
+
hidden_dropout_prob: The dropout probability for all fully connected
|
58 |
+
layers in the embeddings, encoder, and pooler.
|
59 |
+
attention_probs_dropout_prob: The dropout ratio for the attention
|
60 |
+
probabilities.
|
61 |
+
max_position_embeddings: The maximum sequence length that this model might
|
62 |
+
ever be used with. Typically set this to something large just in case
|
63 |
+
(e.g., 512 or 1024 or 2048).
|
64 |
+
type_vocab_size: The vocabulary size of the `token_type_ids` passed into
|
65 |
+
`BertModel`.
|
66 |
+
initializer_range: The stdev of the truncated_normal_initializer for
|
67 |
+
initializing all weight matrices.
|
68 |
+
"""
|
69 |
+
self.vocab_size = vocab_size
|
70 |
+
self.hidden_size = hidden_size
|
71 |
+
self.num_hidden_layers = num_hidden_layers
|
72 |
+
self.num_attention_heads = num_attention_heads
|
73 |
+
self.hidden_act = hidden_act
|
74 |
+
self.intermediate_size = intermediate_size
|
75 |
+
self.hidden_dropout_prob = hidden_dropout_prob
|
76 |
+
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
77 |
+
self.max_position_embeddings = max_position_embeddings
|
78 |
+
self.type_vocab_size = type_vocab_size
|
79 |
+
self.initializer_range = initializer_range
|
80 |
+
|
81 |
+
@classmethod
|
82 |
+
def from_dict(cls, json_object):
|
83 |
+
"""Constructs a `BertConfig` from a Python dictionary of parameters."""
|
84 |
+
config = BertConfig(vocab_size=None)
|
85 |
+
for (key, value) in six.iteritems(json_object):
|
86 |
+
config.__dict__[key] = value
|
87 |
+
return config
|
88 |
+
|
89 |
+
@classmethod
|
90 |
+
def from_json_file(cls, json_file):
|
91 |
+
"""Constructs a `BertConfig` from a json file of parameters."""
|
92 |
+
with tf.gfile.GFile(json_file, "r") as reader:
|
93 |
+
text = reader.read()
|
94 |
+
return cls.from_dict(json.loads(text))
|
95 |
+
|
96 |
+
def to_dict(self):
|
97 |
+
"""Serializes this instance to a Python dictionary."""
|
98 |
+
output = copy.deepcopy(self.__dict__)
|
99 |
+
return output
|
100 |
+
|
101 |
+
def to_json_string(self):
|
102 |
+
"""Serializes this instance to a JSON string."""
|
103 |
+
return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n"
|
104 |
+
|
105 |
+
|
106 |
+
class BertModel(object):
|
107 |
+
"""BERT model ("Bidirectional Embedding Representations from a Transformer").
|
108 |
+
|
109 |
+
Example usage:
|
110 |
+
|
111 |
+
```python
|
112 |
+
# Already been converted into WordPiece token ids
|
113 |
+
input_ids = tf.constant([[31, 51, 99], [15, 5, 0]])
|
114 |
+
input_mask = tf.constant([[1, 1, 1], [1, 1, 0]])
|
115 |
+
token_type_ids = tf.constant([[0, 0, 1], [0, 2, 0]])
|
116 |
+
|
117 |
+
config = modeling.BertConfig(vocab_size=32000, hidden_size=512,
|
118 |
+
num_hidden_layers=8, num_attention_heads=6, intermediate_size=1024)
|
119 |
+
|
120 |
+
model = modeling.BertModel(config=config, is_training=True,
|
121 |
+
input_ids=input_ids, input_mask=input_mask, token_type_ids=token_type_ids)
|
122 |
+
|
123 |
+
label_embeddings = tf.get_variable(...)
|
124 |
+
pooled_output = model.get_pooled_output()
|
125 |
+
logits = tf.matmul(pooled_output, label_embeddings)
|
126 |
+
...
|
127 |
+
```
|
128 |
+
"""
|
129 |
+
|
130 |
+
def __init__(self,
|
131 |
+
config,
|
132 |
+
is_training,
|
133 |
+
input_ids,
|
134 |
+
input_mask=None,
|
135 |
+
token_type_ids=None,
|
136 |
+
use_one_hot_embeddings=True,
|
137 |
+
scope=None):
|
138 |
+
"""Constructor for BertModel.
|
139 |
+
|
140 |
+
Args:
|
141 |
+
config: `BertConfig` instance.
|
142 |
+
is_training: bool. rue for training model, false for eval model. Controls
|
143 |
+
whether dropout will be applied.
|
144 |
+
input_ids: int32 Tensor of shape [batch_size, seq_length].
|
145 |
+
input_mask: (optional) int32 Tensor of shape [batch_size, seq_length].
|
146 |
+
token_type_ids: (optional) int32 Tensor of shape [batch_size, seq_length].
|
147 |
+
use_one_hot_embeddings: (optional) bool. Whether to use one-hot word
|
148 |
+
embeddings or tf.embedding_lookup() for the word embeddings. On the TPU,
|
149 |
+
it is must faster if this is True, on the CPU or GPU, it is faster if
|
150 |
+
this is False.
|
151 |
+
scope: (optional) variable scope. Defaults to "bert".
|
152 |
+
|
153 |
+
Raises:
|
154 |
+
ValueError: The config is invalid or one of the input tensor shapes
|
155 |
+
is invalid.
|
156 |
+
"""
|
157 |
+
config = copy.deepcopy(config)
|
158 |
+
if not is_training:
|
159 |
+
config.hidden_dropout_prob = 0.0
|
160 |
+
config.attention_probs_dropout_prob = 0.0
|
161 |
+
|
162 |
+
input_shape = get_shape_list(input_ids, expected_rank=2)
|
163 |
+
batch_size = input_shape[0]
|
164 |
+
seq_length = input_shape[1]
|
165 |
+
|
166 |
+
if input_mask is None:
|
167 |
+
input_mask = tf.ones(shape=[batch_size, seq_length], dtype=tf.int32)
|
168 |
+
|
169 |
+
if token_type_ids is None:
|
170 |
+
token_type_ids = tf.zeros(shape=[batch_size, seq_length], dtype=tf.int32)
|
171 |
+
|
172 |
+
with tf.variable_scope(scope, default_name="bert"):
|
173 |
+
with tf.variable_scope("embeddings"):
|
174 |
+
# Perform embedding lookup on the word ids.
|
175 |
+
(self.embedding_output, self.embedding_table) = embedding_lookup(
|
176 |
+
input_ids=input_ids,
|
177 |
+
vocab_size=config.vocab_size,
|
178 |
+
embedding_size=config.hidden_size,
|
179 |
+
initializer_range=config.initializer_range,
|
180 |
+
word_embedding_name="word_embeddings",
|
181 |
+
use_one_hot_embeddings=use_one_hot_embeddings)
|
182 |
+
|
183 |
+
# Add positional embeddings and token type embeddings, then layer
|
184 |
+
# normalize and perform dropout.
|
185 |
+
self.embedding_output = embedding_postprocessor(
|
186 |
+
input_tensor=self.embedding_output,
|
187 |
+
use_token_type=True,
|
188 |
+
token_type_ids=token_type_ids,
|
189 |
+
token_type_vocab_size=config.type_vocab_size,
|
190 |
+
token_type_embedding_name="token_type_embeddings",
|
191 |
+
use_position_embeddings=True,
|
192 |
+
position_embedding_name="position_embeddings",
|
193 |
+
initializer_range=config.initializer_range,
|
194 |
+
max_position_embeddings=config.max_position_embeddings,
|
195 |
+
dropout_prob=config.hidden_dropout_prob)
|
196 |
+
|
197 |
+
with tf.variable_scope("encoder"):
|
198 |
+
# This converts a 2D mask of shape [batch_size, seq_length] to a 3D
|
199 |
+
# mask of shape [batch_size, seq_length, seq_length] which is used
|
200 |
+
# for the attention scores.
|
201 |
+
attention_mask = create_attention_mask_from_input_mask(
|
202 |
+
input_ids, input_mask)
|
203 |
+
|
204 |
+
# Run the stacked transformer.
|
205 |
+
# `sequence_output` shape = [batch_size, seq_length, hidden_size].
|
206 |
+
self.all_encoder_layers = transformer_model(
|
207 |
+
input_tensor=self.embedding_output,
|
208 |
+
attention_mask=attention_mask,
|
209 |
+
hidden_size=config.hidden_size,
|
210 |
+
num_hidden_layers=config.num_hidden_layers,
|
211 |
+
num_attention_heads=config.num_attention_heads,
|
212 |
+
intermediate_size=config.intermediate_size,
|
213 |
+
intermediate_act_fn=get_activation(config.hidden_act),
|
214 |
+
hidden_dropout_prob=config.hidden_dropout_prob,
|
215 |
+
attention_probs_dropout_prob=config.attention_probs_dropout_prob,
|
216 |
+
initializer_range=config.initializer_range,
|
217 |
+
do_return_all_layers=True)
|
218 |
+
|
219 |
+
self.sequence_output = self.all_encoder_layers[-1]
|
220 |
+
# The "pooler" converts the encoded sequence tensor of shape
|
221 |
+
# [batch_size, seq_length, hidden_size] to a tensor of shape
|
222 |
+
# [batch_size, hidden_size]. This is necessary for segment-level
|
223 |
+
# (or segment-pair-level) classification tasks where we need a fixed
|
224 |
+
# dimensional representation of the segment.
|
225 |
+
with tf.variable_scope("pooler"):
|
226 |
+
# We "pool" the model by simply taking the hidden state corresponding
|
227 |
+
# to the first token. We assume that this has been pre-trained
|
228 |
+
first_token_tensor = tf.squeeze(self.sequence_output[:, 0:1, :], axis=1)
|
229 |
+
self.pooled_output = tf.layers.dense(
|
230 |
+
first_token_tensor,
|
231 |
+
config.hidden_size,
|
232 |
+
activation=tf.tanh,
|
233 |
+
kernel_initializer=create_initializer(config.initializer_range))
|
234 |
+
|
235 |
+
def get_pooled_output(self):
|
236 |
+
return self.pooled_output
|
237 |
+
|
238 |
+
def get_sequence_output(self):
|
239 |
+
"""Gets final hidden layer of encoder.
|
240 |
+
|
241 |
+
Returns:
|
242 |
+
float Tensor of shape [batch_size, seq_length, hidden_size] corresponding
|
243 |
+
to the final hidden of the transformer encoder.
|
244 |
+
"""
|
245 |
+
return self.sequence_output
|
246 |
+
|
247 |
+
def get_all_encoder_layers(self):
|
248 |
+
return self.all_encoder_layers
|
249 |
+
|
250 |
+
def get_embedding_output(self):
|
251 |
+
"""Gets output of the embedding lookup (i.e., input to the transformer).
|
252 |
+
|
253 |
+
Returns:
|
254 |
+
float Tensor of shape [batch_size, seq_length, hidden_size] corresponding
|
255 |
+
to the output of the embedding layer, after summing the word
|
256 |
+
embeddings with the positional embeddings and the token type embeddings,
|
257 |
+
then performing layer normalization. This is the input to the transformer.
|
258 |
+
"""
|
259 |
+
return self.embedding_output
|
260 |
+
|
261 |
+
def get_embedding_table(self):
|
262 |
+
return self.embedding_table
|
263 |
+
|
264 |
+
|
265 |
+
def gelu(input_tensor):
|
266 |
+
"""Gaussian Error Linear Unit.
|
267 |
+
|
268 |
+
This is a smoother version of the RELU.
|
269 |
+
Original paper: https://arxiv.org/abs/1606.08415
|
270 |
+
|
271 |
+
Args:
|
272 |
+
input_tensor: float Tensor to perform activation.
|
273 |
+
|
274 |
+
Returns:
|
275 |
+
`input_tensor` with the GELU activation applied.
|
276 |
+
"""
|
277 |
+
cdf = 0.5 * (1.0 + tf.erf(input_tensor / tf.sqrt(2.0)))
|
278 |
+
return input_tensor * cdf
|
279 |
+
|
280 |
+
|
281 |
+
def get_activation(activation_string):
|
282 |
+
"""Maps a string to a Python function, e.g., "relu" => `tf.nn.relu`.
|
283 |
+
|
284 |
+
Args:
|
285 |
+
activation_string: String name of the activation function.
|
286 |
+
|
287 |
+
Returns:
|
288 |
+
A Python function corresponding to the activation function. If
|
289 |
+
`activation_string` is None, empty, or "linear", this will return None.
|
290 |
+
If `activation_string` is not a string, it will return `activation_string`.
|
291 |
+
|
292 |
+
Raises:
|
293 |
+
ValueError: The `activation_string` does not correspond to a known
|
294 |
+
activation.
|
295 |
+
"""
|
296 |
+
|
297 |
+
# We assume that anything that"s not a string is already an activation
|
298 |
+
# function, so we just return it.
|
299 |
+
if not isinstance(activation_string, six.string_types):
|
300 |
+
return activation_string
|
301 |
+
|
302 |
+
if not activation_string:
|
303 |
+
return None
|
304 |
+
|
305 |
+
act = activation_string.lower()
|
306 |
+
if act == "linear":
|
307 |
+
return None
|
308 |
+
elif act == "relu":
|
309 |
+
return tf.nn.relu
|
310 |
+
elif act == "gelu":
|
311 |
+
return gelu
|
312 |
+
elif act == "tanh":
|
313 |
+
return tf.tanh
|
314 |
+
else:
|
315 |
+
raise ValueError("Unsupported activation: %s" % act)
|
316 |
+
|
317 |
+
|
318 |
+
def get_assignment_map_from_checkpoint(tvars, init_checkpoint):
|
319 |
+
"""Compute the union of the current variables and checkpoint variables."""
|
320 |
+
assignment_map = {}
|
321 |
+
initialized_variable_names = {}
|
322 |
+
|
323 |
+
name_to_variable = collections.OrderedDict()
|
324 |
+
for var in tvars:
|
325 |
+
name = var.name
|
326 |
+
m = re.match("^(.*):\\d+$", name)
|
327 |
+
if m is not None:
|
328 |
+
name = m.group(1)
|
329 |
+
name_to_variable[name] = var
|
330 |
+
|
331 |
+
init_vars = tf.train.list_variables(init_checkpoint)
|
332 |
+
|
333 |
+
assignment_map = collections.OrderedDict()
|
334 |
+
for x in init_vars:
|
335 |
+
(name, var) = (x[0], x[1])
|
336 |
+
if name not in name_to_variable:
|
337 |
+
continue
|
338 |
+
assignment_map[name] = name
|
339 |
+
initialized_variable_names[name] = 1
|
340 |
+
initialized_variable_names[name + ":0"] = 1
|
341 |
+
|
342 |
+
return (assignment_map, initialized_variable_names)
|
343 |
+
|
344 |
+
|
345 |
+
def dropout(input_tensor, dropout_prob):
|
346 |
+
"""Perform dropout.
|
347 |
+
|
348 |
+
Args:
|
349 |
+
input_tensor: float Tensor.
|
350 |
+
dropout_prob: Python float. The probability of dropping out a value (NOT of
|
351 |
+
*keeping* a dimension as in `tf.nn.dropout`).
|
352 |
+
|
353 |
+
Returns:
|
354 |
+
A version of `input_tensor` with dropout applied.
|
355 |
+
"""
|
356 |
+
if dropout_prob is None or dropout_prob == 0.0:
|
357 |
+
return input_tensor
|
358 |
+
|
359 |
+
output = tf.nn.dropout(input_tensor, 1.0 - dropout_prob)
|
360 |
+
return output
|
361 |
+
|
362 |
+
|
363 |
+
def layer_norm(input_tensor, name=None):
|
364 |
+
"""Run layer normalization on the last dimension of the tensor."""
|
365 |
+
return tf.contrib.layers.layer_norm(
|
366 |
+
inputs=input_tensor, begin_norm_axis=-1, begin_params_axis=-1, scope=name)
|
367 |
+
|
368 |
+
|
369 |
+
def layer_norm_and_dropout(input_tensor, dropout_prob, name=None):
|
370 |
+
"""Runs layer normalization followed by dropout."""
|
371 |
+
output_tensor = layer_norm(input_tensor, name)
|
372 |
+
output_tensor = dropout(output_tensor, dropout_prob)
|
373 |
+
return output_tensor
|
374 |
+
|
375 |
+
|
376 |
+
def create_initializer(initializer_range=0.02):
|
377 |
+
"""Creates a `truncated_normal_initializer` with the given range."""
|
378 |
+
return tf.truncated_normal_initializer(stddev=initializer_range)
|
379 |
+
|
380 |
+
|
381 |
+
def embedding_lookup(input_ids,
|
382 |
+
vocab_size,
|
383 |
+
embedding_size=128,
|
384 |
+
initializer_range=0.02,
|
385 |
+
word_embedding_name="word_embeddings",
|
386 |
+
use_one_hot_embeddings=False):
|
387 |
+
"""Looks up words embeddings for id tensor.
|
388 |
+
|
389 |
+
Args:
|
390 |
+
input_ids: int32 Tensor of shape [batch_size, seq_length] containing word
|
391 |
+
ids.
|
392 |
+
vocab_size: int. Size of the embedding vocabulary.
|
393 |
+
embedding_size: int. Width of the word embeddings.
|
394 |
+
initializer_range: float. Embedding initialization range.
|
395 |
+
word_embedding_name: string. Name of the embedding table.
|
396 |
+
use_one_hot_embeddings: bool. If True, use one-hot method for word
|
397 |
+
embeddings. If False, use `tf.nn.embedding_lookup()`. One hot is better
|
398 |
+
for TPUs.
|
399 |
+
|
400 |
+
Returns:
|
401 |
+
float Tensor of shape [batch_size, seq_length, embedding_size].
|
402 |
+
"""
|
403 |
+
# This function assumes that the input is of shape [batch_size, seq_length,
|
404 |
+
# num_inputs].
|
405 |
+
#
|
406 |
+
# If the input is a 2D tensor of shape [batch_size, seq_length], we
|
407 |
+
# reshape to [batch_size, seq_length, 1].
|
408 |
+
if input_ids.shape.ndims == 2:
|
409 |
+
input_ids = tf.expand_dims(input_ids, axis=[-1])
|
410 |
+
|
411 |
+
embedding_table = tf.get_variable(
|
412 |
+
name=word_embedding_name,
|
413 |
+
shape=[vocab_size, embedding_size],
|
414 |
+
initializer=create_initializer(initializer_range))
|
415 |
+
|
416 |
+
if use_one_hot_embeddings:
|
417 |
+
flat_input_ids = tf.reshape(input_ids, [-1])
|
418 |
+
one_hot_input_ids = tf.one_hot(flat_input_ids, depth=vocab_size)
|
419 |
+
output = tf.matmul(one_hot_input_ids, embedding_table)
|
420 |
+
else:
|
421 |
+
output = tf.nn.embedding_lookup(embedding_table, input_ids)
|
422 |
+
|
423 |
+
input_shape = get_shape_list(input_ids)
|
424 |
+
|
425 |
+
output = tf.reshape(output,
|
426 |
+
input_shape[0:-1] + [input_shape[-1] * embedding_size])
|
427 |
+
return (output, embedding_table)
|
428 |
+
|
429 |
+
|
430 |
+
def embedding_postprocessor(input_tensor,
|
431 |
+
use_token_type=False,
|
432 |
+
token_type_ids=None,
|
433 |
+
token_type_vocab_size=16,
|
434 |
+
token_type_embedding_name="token_type_embeddings",
|
435 |
+
use_position_embeddings=True,
|
436 |
+
position_embedding_name="position_embeddings",
|
437 |
+
initializer_range=0.02,
|
438 |
+
max_position_embeddings=512,
|
439 |
+
dropout_prob=0.1):
|
440 |
+
"""Performs various post-processing on a word embedding tensor.
|
441 |
+
|
442 |
+
Args:
|
443 |
+
input_tensor: float Tensor of shape [batch_size, seq_length,
|
444 |
+
embedding_size].
|
445 |
+
use_token_type: bool. Whether to add embeddings for `token_type_ids`.
|
446 |
+
token_type_ids: (optional) int32 Tensor of shape [batch_size, seq_length].
|
447 |
+
Must be specified if `use_token_type` is True.
|
448 |
+
token_type_vocab_size: int. The vocabulary size of `token_type_ids`.
|
449 |
+
token_type_embedding_name: string. The name of the embedding table variable
|
450 |
+
for token type ids.
|
451 |
+
use_position_embeddings: bool. Whether to add position embeddings for the
|
452 |
+
position of each token in the sequence.
|
453 |
+
position_embedding_name: string. The name of the embedding table variable
|
454 |
+
for positional embeddings.
|
455 |
+
initializer_range: float. Range of the weight initialization.
|
456 |
+
max_position_embeddings: int. Maximum sequence length that might ever be
|
457 |
+
used with this model. This can be longer than the sequence length of
|
458 |
+
input_tensor, but cannot be shorter.
|
459 |
+
dropout_prob: float. Dropout probability applied to the final output tensor.
|
460 |
+
|
461 |
+
Returns:
|
462 |
+
float tensor with same shape as `input_tensor`.
|
463 |
+
|
464 |
+
Raises:
|
465 |
+
ValueError: One of the tensor shapes or input values is invalid.
|
466 |
+
"""
|
467 |
+
input_shape = get_shape_list(input_tensor, expected_rank=3)
|
468 |
+
batch_size = input_shape[0]
|
469 |
+
seq_length = input_shape[1]
|
470 |
+
width = input_shape[2]
|
471 |
+
|
472 |
+
if seq_length > max_position_embeddings:
|
473 |
+
raise ValueError("The seq length (%d) cannot be greater than "
|
474 |
+
"`max_position_embeddings` (%d)" %
|
475 |
+
(seq_length, max_position_embeddings))
|
476 |
+
|
477 |
+
output = input_tensor
|
478 |
+
|
479 |
+
if use_token_type:
|
480 |
+
if token_type_ids is None:
|
481 |
+
raise ValueError("`token_type_ids` must be specified if"
|
482 |
+
"`use_token_type` is True.")
|
483 |
+
token_type_table = tf.get_variable(
|
484 |
+
name=token_type_embedding_name,
|
485 |
+
shape=[token_type_vocab_size, width],
|
486 |
+
initializer=create_initializer(initializer_range))
|
487 |
+
# This vocab will be small so we always do one-hot here, since it is always
|
488 |
+
# faster for a small vocabulary.
|
489 |
+
flat_token_type_ids = tf.reshape(token_type_ids, [-1])
|
490 |
+
one_hot_ids = tf.one_hot(flat_token_type_ids, depth=token_type_vocab_size)
|
491 |
+
token_type_embeddings = tf.matmul(one_hot_ids, token_type_table)
|
492 |
+
token_type_embeddings = tf.reshape(token_type_embeddings,
|
493 |
+
[batch_size, seq_length, width])
|
494 |
+
output += token_type_embeddings
|
495 |
+
|
496 |
+
if use_position_embeddings:
|
497 |
+
full_position_embeddings = tf.get_variable(
|
498 |
+
name=position_embedding_name,
|
499 |
+
shape=[max_position_embeddings, width],
|
500 |
+
initializer=create_initializer(initializer_range))
|
501 |
+
# Since the position embedding table is a learned variable, we create it
|
502 |
+
# using a (long) sequence length `max_position_embeddings`. The actual
|
503 |
+
# sequence length might be shorter than this, for faster training of
|
504 |
+
# tasks that do not have long sequences.
|
505 |
+
#
|
506 |
+
# So `full_position_embeddings` is effectively an embedding table
|
507 |
+
# for position [0, 1, 2, ..., max_position_embeddings-1], and the current
|
508 |
+
# sequence has positions [0, 1, 2, ... seq_length-1], so we can just
|
509 |
+
# perform a slice.
|
510 |
+
if seq_length < max_position_embeddings:
|
511 |
+
position_embeddings = tf.slice(full_position_embeddings, [0, 0],
|
512 |
+
[seq_length, -1])
|
513 |
+
else:
|
514 |
+
position_embeddings = full_position_embeddings
|
515 |
+
|
516 |
+
num_dims = len(output.shape.as_list())
|
517 |
+
|
518 |
+
# Only the last two dimensions are relevant (`seq_length` and `width`), so
|
519 |
+
# we broadcast among the first dimensions, which is typically just
|
520 |
+
# the batch size.
|
521 |
+
position_broadcast_shape = []
|
522 |
+
for _ in range(num_dims - 2):
|
523 |
+
position_broadcast_shape.append(1)
|
524 |
+
position_broadcast_shape.extend([seq_length, width])
|
525 |
+
position_embeddings = tf.reshape(position_embeddings,
|
526 |
+
position_broadcast_shape)
|
527 |
+
output += position_embeddings
|
528 |
+
|
529 |
+
output = layer_norm_and_dropout(output, dropout_prob)
|
530 |
+
return output
|
531 |
+
|
532 |
+
|
533 |
+
def create_attention_mask_from_input_mask(from_tensor, to_mask):
|
534 |
+
"""Create 3D attention mask from a 2D tensor mask.
|
535 |
+
|
536 |
+
Args:
|
537 |
+
from_tensor: 2D or 3D Tensor of shape [batch_size, from_seq_length, ...].
|
538 |
+
to_mask: int32 Tensor of shape [batch_size, to_seq_length].
|
539 |
+
|
540 |
+
Returns:
|
541 |
+
float Tensor of shape [batch_size, from_seq_length, to_seq_length].
|
542 |
+
"""
|
543 |
+
from_shape = get_shape_list(from_tensor, expected_rank=[2, 3])
|
544 |
+
batch_size = from_shape[0]
|
545 |
+
from_seq_length = from_shape[1]
|
546 |
+
|
547 |
+
to_shape = get_shape_list(to_mask, expected_rank=2)
|
548 |
+
to_seq_length = to_shape[1]
|
549 |
+
|
550 |
+
to_mask = tf.cast(
|
551 |
+
tf.reshape(to_mask, [batch_size, 1, to_seq_length]), tf.float32)
|
552 |
+
|
553 |
+
# We don't assume that `from_tensor` is a mask (although it could be). We
|
554 |
+
# don't actually care if we attend *from* padding tokens (only *to* padding)
|
555 |
+
# tokens so we create a tensor of all ones.
|
556 |
+
#
|
557 |
+
# `broadcast_ones` = [batch_size, from_seq_length, 1]
|
558 |
+
broadcast_ones = tf.ones(
|
559 |
+
shape=[batch_size, from_seq_length, 1], dtype=tf.float32)
|
560 |
+
|
561 |
+
# Here we broadcast along two dimensions to create the mask.
|
562 |
+
mask = broadcast_ones * to_mask
|
563 |
+
|
564 |
+
return mask
|
565 |
+
|
566 |
+
|
567 |
+
def attention_layer(from_tensor,
|
568 |
+
to_tensor,
|
569 |
+
attention_mask=None,
|
570 |
+
num_attention_heads=1,
|
571 |
+
size_per_head=512,
|
572 |
+
query_act=None,
|
573 |
+
key_act=None,
|
574 |
+
value_act=None,
|
575 |
+
attention_probs_dropout_prob=0.0,
|
576 |
+
initializer_range=0.02,
|
577 |
+
do_return_2d_tensor=False,
|
578 |
+
batch_size=None,
|
579 |
+
from_seq_length=None,
|
580 |
+
to_seq_length=None):
|
581 |
+
"""Performs multi-headed attention from `from_tensor` to `to_tensor`.
|
582 |
+
|
583 |
+
This is an implementation of multi-headed attention based on "Attention
|
584 |
+
is all you Need". If `from_tensor` and `to_tensor` are the same, then
|
585 |
+
this is self-attention. Each timestep in `from_tensor` attends to the
|
586 |
+
corresponding sequence in `to_tensor`, and returns a fixed-with vector.
|
587 |
+
|
588 |
+
This function first projects `from_tensor` into a "query" tensor and
|
589 |
+
`to_tensor` into "key" and "value" tensors. These are (effectively) a list
|
590 |
+
of tensors of length `num_attention_heads`, where each tensor is of shape
|
591 |
+
[batch_size, seq_length, size_per_head].
|
592 |
+
|
593 |
+
Then, the query and key tensors are dot-producted and scaled. These are
|
594 |
+
softmaxed to obtain attention probabilities. The value tensors are then
|
595 |
+
interpolated by these probabilities, then concatenated back to a single
|
596 |
+
tensor and returned.
|
597 |
+
|
598 |
+
In practice, the multi-headed attention are done with transposes and
|
599 |
+
reshapes rather than actual separate tensors.
|
600 |
+
|
601 |
+
Args:
|
602 |
+
from_tensor: float Tensor of shape [batch_size, from_seq_length,
|
603 |
+
from_width].
|
604 |
+
to_tensor: float Tensor of shape [batch_size, to_seq_length, to_width].
|
605 |
+
attention_mask: (optional) int32 Tensor of shape [batch_size,
|
606 |
+
from_seq_length, to_seq_length]. The values should be 1 or 0. The
|
607 |
+
attention scores will effectively be set to -infinity for any positions in
|
608 |
+
the mask that are 0, and will be unchanged for positions that are 1.
|
609 |
+
num_attention_heads: int. Number of attention heads.
|
610 |
+
size_per_head: int. Size of each attention head.
|
611 |
+
query_act: (optional) Activation function for the query transform.
|
612 |
+
key_act: (optional) Activation function for the key transform.
|
613 |
+
value_act: (optional) Activation function for the value transform.
|
614 |
+
attention_probs_dropout_prob: (optional) float. Dropout probability of the
|
615 |
+
attention probabilities.
|
616 |
+
initializer_range: float. Range of the weight initializer.
|
617 |
+
do_return_2d_tensor: bool. If True, the output will be of shape [batch_size
|
618 |
+
* from_seq_length, num_attention_heads * size_per_head]. If False, the
|
619 |
+
output will be of shape [batch_size, from_seq_length, num_attention_heads
|
620 |
+
* size_per_head].
|
621 |
+
batch_size: (Optional) int. If the input is 2D, this might be the batch size
|
622 |
+
of the 3D version of the `from_tensor` and `to_tensor`.
|
623 |
+
from_seq_length: (Optional) If the input is 2D, this might be the seq length
|
624 |
+
of the 3D version of the `from_tensor`.
|
625 |
+
to_seq_length: (Optional) If the input is 2D, this might be the seq length
|
626 |
+
of the 3D version of the `to_tensor`.
|
627 |
+
|
628 |
+
Returns:
|
629 |
+
float Tensor of shape [batch_size, from_seq_length,
|
630 |
+
num_attention_heads * size_per_head]. (If `do_return_2d_tensor` is
|
631 |
+
true, this will be of shape [batch_size * from_seq_length,
|
632 |
+
num_attention_heads * size_per_head]).
|
633 |
+
|
634 |
+
Raises:
|
635 |
+
ValueError: Any of the arguments or tensor shapes are invalid.
|
636 |
+
"""
|
637 |
+
|
638 |
+
def transpose_for_scores(input_tensor, batch_size, num_attention_heads,
|
639 |
+
seq_length, width):
|
640 |
+
output_tensor = tf.reshape(
|
641 |
+
input_tensor, [batch_size, seq_length, num_attention_heads, width])
|
642 |
+
|
643 |
+
output_tensor = tf.transpose(output_tensor, [0, 2, 1, 3])
|
644 |
+
return output_tensor
|
645 |
+
|
646 |
+
from_shape = get_shape_list(from_tensor, expected_rank=[2, 3])
|
647 |
+
to_shape = get_shape_list(to_tensor, expected_rank=[2, 3])
|
648 |
+
|
649 |
+
if len(from_shape) != len(to_shape):
|
650 |
+
raise ValueError(
|
651 |
+
"The rank of `from_tensor` must match the rank of `to_tensor`.")
|
652 |
+
|
653 |
+
if len(from_shape) == 3:
|
654 |
+
batch_size = from_shape[0]
|
655 |
+
from_seq_length = from_shape[1]
|
656 |
+
to_seq_length = to_shape[1]
|
657 |
+
elif len(from_shape) == 2:
|
658 |
+
if (batch_size is None or from_seq_length is None or to_seq_length is None):
|
659 |
+
raise ValueError(
|
660 |
+
"When passing in rank 2 tensors to attention_layer, the values "
|
661 |
+
"for `batch_size`, `from_seq_length`, and `to_seq_length` "
|
662 |
+
"must all be specified.")
|
663 |
+
|
664 |
+
# Scalar dimensions referenced here:
|
665 |
+
# B = batch size (number of sequences)
|
666 |
+
# F = `from_tensor` sequence length
|
667 |
+
# T = `to_tensor` sequence length
|
668 |
+
# N = `num_attention_heads`
|
669 |
+
# H = `size_per_head`
|
670 |
+
|
671 |
+
from_tensor_2d = reshape_to_matrix(from_tensor)
|
672 |
+
to_tensor_2d = reshape_to_matrix(to_tensor)
|
673 |
+
|
674 |
+
# `query_layer` = [B*F, N*H]
|
675 |
+
query_layer = tf.layers.dense(
|
676 |
+
from_tensor_2d,
|
677 |
+
num_attention_heads * size_per_head,
|
678 |
+
activation=query_act,
|
679 |
+
name="query",
|
680 |
+
kernel_initializer=create_initializer(initializer_range))
|
681 |
+
|
682 |
+
# `key_layer` = [B*T, N*H]
|
683 |
+
key_layer = tf.layers.dense(
|
684 |
+
to_tensor_2d,
|
685 |
+
num_attention_heads * size_per_head,
|
686 |
+
activation=key_act,
|
687 |
+
name="key",
|
688 |
+
kernel_initializer=create_initializer(initializer_range))
|
689 |
+
|
690 |
+
# `value_layer` = [B*T, N*H]
|
691 |
+
value_layer = tf.layers.dense(
|
692 |
+
to_tensor_2d,
|
693 |
+
num_attention_heads * size_per_head,
|
694 |
+
activation=value_act,
|
695 |
+
name="value",
|
696 |
+
kernel_initializer=create_initializer(initializer_range))
|
697 |
+
|
698 |
+
# `query_layer` = [B, N, F, H]
|
699 |
+
query_layer = transpose_for_scores(query_layer, batch_size,
|
700 |
+
num_attention_heads, from_seq_length,
|
701 |
+
size_per_head)
|
702 |
+
|
703 |
+
# `key_layer` = [B, N, T, H]
|
704 |
+
key_layer = transpose_for_scores(key_layer, batch_size, num_attention_heads,
|
705 |
+
to_seq_length, size_per_head)
|
706 |
+
|
707 |
+
# Take the dot product between "query" and "key" to get the raw
|
708 |
+
# attention scores.
|
709 |
+
# `attention_scores` = [B, N, F, T]
|
710 |
+
attention_scores = tf.matmul(query_layer, key_layer, transpose_b=True)
|
711 |
+
attention_scores = tf.multiply(attention_scores,
|
712 |
+
1.0 / math.sqrt(float(size_per_head)))
|
713 |
+
|
714 |
+
if attention_mask is not None:
|
715 |
+
# `attention_mask` = [B, 1, F, T]
|
716 |
+
attention_mask = tf.expand_dims(attention_mask, axis=[1])
|
717 |
+
|
718 |
+
# Since attention_mask is 1.0 for positions we want to attend and 0.0 for
|
719 |
+
# masked positions, this operation will create a tensor which is 0.0 for
|
720 |
+
# positions we want to attend and -10000.0 for masked positions.
|
721 |
+
adder = (1.0 - tf.cast(attention_mask, tf.float32)) * -10000.0
|
722 |
+
|
723 |
+
# Since we are adding it to the raw scores before the softmax, this is
|
724 |
+
# effectively the same as removing these entirely.
|
725 |
+
attention_scores += adder
|
726 |
+
|
727 |
+
# Normalize the attention scores to probabilities.
|
728 |
+
# `attention_probs` = [B, N, F, T]
|
729 |
+
attention_probs = tf.nn.softmax(attention_scores)
|
730 |
+
|
731 |
+
# This is actually dropping out entire tokens to attend to, which might
|
732 |
+
# seem a bit unusual, but is taken from the original Transformer paper.
|
733 |
+
attention_probs = dropout(attention_probs, attention_probs_dropout_prob)
|
734 |
+
|
735 |
+
# `value_layer` = [B, T, N, H]
|
736 |
+
value_layer = tf.reshape(
|
737 |
+
value_layer,
|
738 |
+
[batch_size, to_seq_length, num_attention_heads, size_per_head])
|
739 |
+
|
740 |
+
# `value_layer` = [B, N, T, H]
|
741 |
+
value_layer = tf.transpose(value_layer, [0, 2, 1, 3])
|
742 |
+
|
743 |
+
# `context_layer` = [B, N, F, H]
|
744 |
+
context_layer = tf.matmul(attention_probs, value_layer)
|
745 |
+
|
746 |
+
# `context_layer` = [B, F, N, H]
|
747 |
+
context_layer = tf.transpose(context_layer, [0, 2, 1, 3])
|
748 |
+
|
749 |
+
if do_return_2d_tensor:
|
750 |
+
# `context_layer` = [B*F, N*V]
|
751 |
+
context_layer = tf.reshape(
|
752 |
+
context_layer,
|
753 |
+
[batch_size * from_seq_length, num_attention_heads * size_per_head])
|
754 |
+
else:
|
755 |
+
# `context_layer` = [B, F, N*V]
|
756 |
+
context_layer = tf.reshape(
|
757 |
+
context_layer,
|
758 |
+
[batch_size, from_seq_length, num_attention_heads * size_per_head])
|
759 |
+
|
760 |
+
return context_layer
|
761 |
+
|
762 |
+
|
763 |
+
def transformer_model(input_tensor,
|
764 |
+
attention_mask=None,
|
765 |
+
hidden_size=768,
|
766 |
+
num_hidden_layers=12,
|
767 |
+
num_attention_heads=12,
|
768 |
+
intermediate_size=3072,
|
769 |
+
intermediate_act_fn=gelu,
|
770 |
+
hidden_dropout_prob=0.1,
|
771 |
+
attention_probs_dropout_prob=0.1,
|
772 |
+
initializer_range=0.02,
|
773 |
+
do_return_all_layers=False):
|
774 |
+
"""Multi-headed, multi-layer Transformer from "Attention is All You Need".
|
775 |
+
|
776 |
+
This is almost an exact implementation of the original Transformer encoder.
|
777 |
+
|
778 |
+
See the original paper:
|
779 |
+
https://arxiv.org/abs/1706.03762
|
780 |
+
|
781 |
+
Also see:
|
782 |
+
https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/models/transformer.py
|
783 |
+
|
784 |
+
Args:
|
785 |
+
input_tensor: float Tensor of shape [batch_size, seq_length, hidden_size].
|
786 |
+
attention_mask: (optional) int32 Tensor of shape [batch_size, seq_length,
|
787 |
+
seq_length], with 1 for positions that can be attended to and 0 in
|
788 |
+
positions that should not be.
|
789 |
+
hidden_size: int. Hidden size of the Transformer.
|
790 |
+
num_hidden_layers: int. Number of layers (blocks) in the Transformer.
|
791 |
+
num_attention_heads: int. Number of attention heads in the Transformer.
|
792 |
+
intermediate_size: int. The size of the "intermediate" (a.k.a., feed
|
793 |
+
forward) layer.
|
794 |
+
intermediate_act_fn: function. The non-linear activation function to apply
|
795 |
+
to the output of the intermediate/feed-forward layer.
|
796 |
+
hidden_dropout_prob: float. Dropout probability for the hidden layers.
|
797 |
+
attention_probs_dropout_prob: float. Dropout probability of the attention
|
798 |
+
probabilities.
|
799 |
+
initializer_range: float. Range of the initializer (stddev of truncated
|
800 |
+
normal).
|
801 |
+
do_return_all_layers: Whether to also return all layers or just the final
|
802 |
+
layer.
|
803 |
+
|
804 |
+
Returns:
|
805 |
+
float Tensor of shape [batch_size, seq_length, hidden_size], the final
|
806 |
+
hidden layer of the Transformer.
|
807 |
+
|
808 |
+
Raises:
|
809 |
+
ValueError: A Tensor shape or parameter is invalid.
|
810 |
+
"""
|
811 |
+
if hidden_size % num_attention_heads != 0:
|
812 |
+
raise ValueError(
|
813 |
+
"The hidden size (%d) is not a multiple of the number of attention "
|
814 |
+
"heads (%d)" % (hidden_size, num_attention_heads))
|
815 |
+
|
816 |
+
attention_head_size = int(hidden_size / num_attention_heads)
|
817 |
+
input_shape = get_shape_list(input_tensor, expected_rank=3)
|
818 |
+
batch_size = input_shape[0]
|
819 |
+
seq_length = input_shape[1]
|
820 |
+
input_width = input_shape[2]
|
821 |
+
|
822 |
+
# The Transformer performs sum residuals on all layers so the input needs
|
823 |
+
# to be the same as the hidden size.
|
824 |
+
if input_width != hidden_size:
|
825 |
+
raise ValueError("The width of the input tensor (%d) != hidden size (%d)" %
|
826 |
+
(input_width, hidden_size))
|
827 |
+
|
828 |
+
# We keep the representation as a 2D tensor to avoid re-shaping it back and
|
829 |
+
# forth from a 3D tensor to a 2D tensor. Re-shapes are normally free on
|
830 |
+
# the GPU/CPU but may not be free on the TPU, so we want to minimize them to
|
831 |
+
# help the optimizer.
|
832 |
+
prev_output = reshape_to_matrix(input_tensor)
|
833 |
+
|
834 |
+
all_layer_outputs = []
|
835 |
+
for layer_idx in range(num_hidden_layers):
|
836 |
+
with tf.variable_scope("layer_%d" % layer_idx):
|
837 |
+
layer_input = prev_output
|
838 |
+
|
839 |
+
with tf.variable_scope("attention"):
|
840 |
+
attention_heads = []
|
841 |
+
with tf.variable_scope("self"):
|
842 |
+
attention_head = attention_layer(
|
843 |
+
from_tensor=layer_input,
|
844 |
+
to_tensor=layer_input,
|
845 |
+
attention_mask=attention_mask,
|
846 |
+
num_attention_heads=num_attention_heads,
|
847 |
+
size_per_head=attention_head_size,
|
848 |
+
attention_probs_dropout_prob=attention_probs_dropout_prob,
|
849 |
+
initializer_range=initializer_range,
|
850 |
+
do_return_2d_tensor=True,
|
851 |
+
batch_size=batch_size,
|
852 |
+
from_seq_length=seq_length,
|
853 |
+
to_seq_length=seq_length)
|
854 |
+
attention_heads.append(attention_head)
|
855 |
+
|
856 |
+
attention_output = None
|
857 |
+
if len(attention_heads) == 1:
|
858 |
+
attention_output = attention_heads[0]
|
859 |
+
else:
|
860 |
+
# In the case where we have other sequences, we just concatenate
|
861 |
+
# them to the self-attention head before the projection.
|
862 |
+
attention_output = tf.concat(attention_heads, axis=-1)
|
863 |
+
|
864 |
+
# Run a linear projection of `hidden_size` then add a residual
|
865 |
+
# with `layer_input`.
|
866 |
+
with tf.variable_scope("output"):
|
867 |
+
attention_output = tf.layers.dense(
|
868 |
+
attention_output,
|
869 |
+
hidden_size,
|
870 |
+
kernel_initializer=create_initializer(initializer_range))
|
871 |
+
attention_output = dropout(attention_output, hidden_dropout_prob)
|
872 |
+
attention_output = layer_norm(attention_output + layer_input)
|
873 |
+
|
874 |
+
# The activation is only applied to the "intermediate" hidden layer.
|
875 |
+
with tf.variable_scope("intermediate"):
|
876 |
+
intermediate_output = tf.layers.dense(
|
877 |
+
attention_output,
|
878 |
+
intermediate_size,
|
879 |
+
activation=intermediate_act_fn,
|
880 |
+
kernel_initializer=create_initializer(initializer_range))
|
881 |
+
|
882 |
+
# Down-project back to `hidden_size` then add the residual.
|
883 |
+
with tf.variable_scope("output"):
|
884 |
+
layer_output = tf.layers.dense(
|
885 |
+
intermediate_output,
|
886 |
+
hidden_size,
|
887 |
+
kernel_initializer=create_initializer(initializer_range))
|
888 |
+
layer_output = dropout(layer_output, hidden_dropout_prob)
|
889 |
+
layer_output = layer_norm(layer_output + attention_output)
|
890 |
+
prev_output = layer_output
|
891 |
+
all_layer_outputs.append(layer_output)
|
892 |
+
|
893 |
+
if do_return_all_layers:
|
894 |
+
final_outputs = []
|
895 |
+
for layer_output in all_layer_outputs:
|
896 |
+
final_output = reshape_from_matrix(layer_output, input_shape)
|
897 |
+
final_outputs.append(final_output)
|
898 |
+
return final_outputs
|
899 |
+
else:
|
900 |
+
final_output = reshape_from_matrix(prev_output, input_shape)
|
901 |
+
return final_output
|
902 |
+
|
903 |
+
|
904 |
+
def get_shape_list(tensor, expected_rank=None, name=None):
|
905 |
+
"""Returns a list of the shape of tensor, preferring static dimensions.
|
906 |
+
|
907 |
+
Args:
|
908 |
+
tensor: A tf.Tensor object to find the shape of.
|
909 |
+
expected_rank: (optional) int. The expected rank of `tensor`. If this is
|
910 |
+
specified and the `tensor` has a different rank, and exception will be
|
911 |
+
thrown.
|
912 |
+
name: Optional name of the tensor for the error message.
|
913 |
+
|
914 |
+
Returns:
|
915 |
+
A list of dimensions of the shape of tensor. All static dimensions will
|
916 |
+
be returned as python integers, and dynamic dimensions will be returned
|
917 |
+
as tf.Tensor scalars.
|
918 |
+
"""
|
919 |
+
if name is None:
|
920 |
+
name = tensor.name
|
921 |
+
|
922 |
+
if expected_rank is not None:
|
923 |
+
assert_rank(tensor, expected_rank, name)
|
924 |
+
|
925 |
+
shape = tensor.shape.as_list()
|
926 |
+
|
927 |
+
non_static_indexes = []
|
928 |
+
for (index, dim) in enumerate(shape):
|
929 |
+
if dim is None:
|
930 |
+
non_static_indexes.append(index)
|
931 |
+
|
932 |
+
if not non_static_indexes:
|
933 |
+
return shape
|
934 |
+
|
935 |
+
dyn_shape = tf.shape(tensor)
|
936 |
+
for index in non_static_indexes:
|
937 |
+
shape[index] = dyn_shape[index]
|
938 |
+
return shape
|
939 |
+
|
940 |
+
|
941 |
+
def reshape_to_matrix(input_tensor):
|
942 |
+
"""Reshapes a >= rank 2 tensor to a rank 2 tensor (i.e., a matrix)."""
|
943 |
+
ndims = input_tensor.shape.ndims
|
944 |
+
if ndims < 2:
|
945 |
+
raise ValueError("Input tensor must have at least rank 2. Shape = %s" %
|
946 |
+
(input_tensor.shape))
|
947 |
+
if ndims == 2:
|
948 |
+
return input_tensor
|
949 |
+
|
950 |
+
width = input_tensor.shape[-1]
|
951 |
+
output_tensor = tf.reshape(input_tensor, [-1, width])
|
952 |
+
return output_tensor
|
953 |
+
|
954 |
+
|
955 |
+
def reshape_from_matrix(output_tensor, orig_shape_list):
|
956 |
+
"""Reshapes a rank 2 tensor back to its original rank >= 2 tensor."""
|
957 |
+
if len(orig_shape_list) == 2:
|
958 |
+
return output_tensor
|
959 |
+
|
960 |
+
output_shape = get_shape_list(output_tensor)
|
961 |
+
|
962 |
+
orig_dims = orig_shape_list[0:-1]
|
963 |
+
width = output_shape[-1]
|
964 |
+
|
965 |
+
return tf.reshape(output_tensor, orig_dims + [width])
|
966 |
+
|
967 |
+
|
968 |
+
def assert_rank(tensor, expected_rank, name=None):
|
969 |
+
"""Raises an exception if the tensor rank is not of the expected rank.
|
970 |
+
|
971 |
+
Args:
|
972 |
+
tensor: A tf.Tensor to check the rank of.
|
973 |
+
expected_rank: Python integer or list of integers, expected rank.
|
974 |
+
name: Optional name of the tensor for the error message.
|
975 |
+
|
976 |
+
Raises:
|
977 |
+
ValueError: If the expected shape doesn't match the actual shape.
|
978 |
+
"""
|
979 |
+
if name is None:
|
980 |
+
name = tensor.name
|
981 |
+
|
982 |
+
expected_rank_dict = {}
|
983 |
+
if isinstance(expected_rank, six.integer_types):
|
984 |
+
expected_rank_dict[expected_rank] = True
|
985 |
+
else:
|
986 |
+
for x in expected_rank:
|
987 |
+
expected_rank_dict[x] = True
|
988 |
+
|
989 |
+
actual_rank = tensor.shape.ndims
|
990 |
+
if actual_rank not in expected_rank_dict:
|
991 |
+
scope_name = tf.get_variable_scope().name
|
992 |
+
raise ValueError(
|
993 |
+
"For the tensor `%s` in scope `%s`, the actual rank "
|
994 |
+
"`%d` (shape = %s) is not equal to the expected rank `%s`" %
|
995 |
+
(name, scope_name, actual_rank, str(tensor.shape), str(expected_rank)))
|
bert/optimization.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""Functions and classes related to optimization (weight updates)."""
|
16 |
+
|
17 |
+
from __future__ import absolute_import
|
18 |
+
from __future__ import division
|
19 |
+
from __future__ import print_function
|
20 |
+
|
21 |
+
import re
|
22 |
+
import tensorflow as tf
|
23 |
+
|
24 |
+
|
25 |
+
def create_optimizer(loss, init_lr, num_train_steps, num_warmup_steps, use_tpu):
|
26 |
+
"""Creates an optimizer training op."""
|
27 |
+
global_step = tf.train.get_or_create_global_step()
|
28 |
+
|
29 |
+
learning_rate = tf.constant(value=init_lr, shape=[], dtype=tf.float32)
|
30 |
+
|
31 |
+
# Implements linear decay of the learning rate.
|
32 |
+
learning_rate = tf.train.polynomial_decay(
|
33 |
+
learning_rate,
|
34 |
+
global_step,
|
35 |
+
num_train_steps,
|
36 |
+
end_learning_rate=0.0,
|
37 |
+
power=1.0,
|
38 |
+
cycle=False)
|
39 |
+
|
40 |
+
# Implements linear warmup. I.e., if global_step < num_warmup_steps, the
|
41 |
+
# learning rate will be `global_step/num_warmup_steps * init_lr`.
|
42 |
+
if num_warmup_steps:
|
43 |
+
global_steps_int = tf.cast(global_step, tf.int32)
|
44 |
+
warmup_steps_int = tf.constant(num_warmup_steps, dtype=tf.int32)
|
45 |
+
|
46 |
+
global_steps_float = tf.cast(global_steps_int, tf.float32)
|
47 |
+
warmup_steps_float = tf.cast(warmup_steps_int, tf.float32)
|
48 |
+
|
49 |
+
warmup_percent_done = global_steps_float / warmup_steps_float
|
50 |
+
warmup_learning_rate = init_lr * warmup_percent_done
|
51 |
+
|
52 |
+
is_warmup = tf.cast(global_steps_int < warmup_steps_int, tf.float32)
|
53 |
+
learning_rate = (
|
54 |
+
(1.0 - is_warmup) * learning_rate + is_warmup * warmup_learning_rate)
|
55 |
+
|
56 |
+
# It is recommended that you use this optimizer for fine tuning, since this
|
57 |
+
# is how the model was trained (note that the Adam m/v variables are NOT
|
58 |
+
# loaded from init_checkpoint.)
|
59 |
+
optimizer = AdamWeightDecayOptimizer(
|
60 |
+
learning_rate=learning_rate,
|
61 |
+
weight_decay_rate=0.01,
|
62 |
+
beta_1=0.9,
|
63 |
+
beta_2=0.999,
|
64 |
+
epsilon=1e-6,
|
65 |
+
exclude_from_weight_decay=["LayerNorm", "layer_norm", "bias"])
|
66 |
+
|
67 |
+
if use_tpu:
|
68 |
+
optimizer = tf.contrib.tpu.CrossShardOptimizer(optimizer)
|
69 |
+
|
70 |
+
tvars = tf.trainable_variables()
|
71 |
+
grads = tf.gradients(loss, tvars)
|
72 |
+
|
73 |
+
# This is how the model was pre-trained.
|
74 |
+
(grads, _) = tf.clip_by_global_norm(grads, clip_norm=1.0)
|
75 |
+
|
76 |
+
train_op = optimizer.apply_gradients(
|
77 |
+
zip(grads, tvars), global_step=global_step)
|
78 |
+
|
79 |
+
new_global_step = global_step + 1
|
80 |
+
train_op = tf.group(train_op, [global_step.assign(new_global_step)])
|
81 |
+
return train_op
|
82 |
+
|
83 |
+
|
84 |
+
class AdamWeightDecayOptimizer(tf.train.Optimizer):
|
85 |
+
"""A basic Adam optimizer that includes "correct" L2 weight decay."""
|
86 |
+
|
87 |
+
def __init__(self,
|
88 |
+
learning_rate,
|
89 |
+
weight_decay_rate=0.0,
|
90 |
+
beta_1=0.9,
|
91 |
+
beta_2=0.999,
|
92 |
+
epsilon=1e-6,
|
93 |
+
exclude_from_weight_decay=None,
|
94 |
+
name="AdamWeightDecayOptimizer"):
|
95 |
+
"""Constructs a AdamWeightDecayOptimizer."""
|
96 |
+
super(AdamWeightDecayOptimizer, self).__init__(False, name)
|
97 |
+
|
98 |
+
self.learning_rate = learning_rate
|
99 |
+
self.weight_decay_rate = weight_decay_rate
|
100 |
+
self.beta_1 = beta_1
|
101 |
+
self.beta_2 = beta_2
|
102 |
+
self.epsilon = epsilon
|
103 |
+
self.exclude_from_weight_decay = exclude_from_weight_decay
|
104 |
+
|
105 |
+
def apply_gradients(self, grads_and_vars, global_step=None, name=None):
|
106 |
+
"""See base class."""
|
107 |
+
assignments = []
|
108 |
+
for (grad, param) in grads_and_vars:
|
109 |
+
if grad is None or param is None:
|
110 |
+
continue
|
111 |
+
|
112 |
+
param_name = self._get_variable_name(param.name)
|
113 |
+
|
114 |
+
m = tf.get_variable(
|
115 |
+
name=param_name + "/adam_m",
|
116 |
+
shape=param.shape.as_list(),
|
117 |
+
dtype=tf.float32,
|
118 |
+
trainable=False,
|
119 |
+
initializer=tf.zeros_initializer())
|
120 |
+
v = tf.get_variable(
|
121 |
+
name=param_name + "/adam_v",
|
122 |
+
shape=param.shape.as_list(),
|
123 |
+
dtype=tf.float32,
|
124 |
+
trainable=False,
|
125 |
+
initializer=tf.zeros_initializer())
|
126 |
+
|
127 |
+
# Standard Adam update.
|
128 |
+
next_m = (
|
129 |
+
tf.multiply(self.beta_1, m) + tf.multiply(1.0 - self.beta_1, grad))
|
130 |
+
next_v = (
|
131 |
+
tf.multiply(self.beta_2, v) + tf.multiply(1.0 - self.beta_2,
|
132 |
+
tf.square(grad)))
|
133 |
+
|
134 |
+
update = next_m / (tf.sqrt(next_v) + self.epsilon)
|
135 |
+
|
136 |
+
# Just adding the square of the weights to the loss function is *not*
|
137 |
+
# the correct way of using L2 regularization/weight decay with Adam,
|
138 |
+
# since that will interact with the m and v parameters in strange ways.
|
139 |
+
#
|
140 |
+
# Instead we want ot decay the weights in a manner that doesn't interact
|
141 |
+
# with the m/v parameters. This is equivalent to adding the square
|
142 |
+
# of the weights to the loss with plain (non-momentum) SGD.
|
143 |
+
if self._do_use_weight_decay(param_name):
|
144 |
+
update += self.weight_decay_rate * param
|
145 |
+
|
146 |
+
update_with_lr = self.learning_rate * update
|
147 |
+
|
148 |
+
next_param = param - update_with_lr
|
149 |
+
|
150 |
+
assignments.extend(
|
151 |
+
[param.assign(next_param),
|
152 |
+
m.assign(next_m),
|
153 |
+
v.assign(next_v)])
|
154 |
+
return tf.group(*assignments, name=name)
|
155 |
+
|
156 |
+
def _do_use_weight_decay(self, param_name):
|
157 |
+
"""Whether to use L2 weight decay for `param_name`."""
|
158 |
+
if not self.weight_decay_rate:
|
159 |
+
return False
|
160 |
+
if self.exclude_from_weight_decay:
|
161 |
+
for r in self.exclude_from_weight_decay:
|
162 |
+
if re.search(r, param_name) is not None:
|
163 |
+
return False
|
164 |
+
return True
|
165 |
+
|
166 |
+
def _get_variable_name(self, param_name):
|
167 |
+
"""Get the variable name from the tensor name."""
|
168 |
+
m = re.match("^(.*):\\d+$", param_name)
|
169 |
+
if m is not None:
|
170 |
+
param_name = m.group(1)
|
171 |
+
return param_name
|
bert/requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow >= 1.11.0 # CPU Version of TensorFlow.
|
2 |
+
|
3 |
+
# tensorflow-gpu >= 1.11.0 # GPU version of TensorFlow.
|
4 |
+
|
5 |
+
sentencepiece==0.0.9
|
bert/run_classifier.py
ADDED
@@ -0,0 +1,976 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""BERT finetuning runner."""
|
16 |
+
|
17 |
+
from __future__ import absolute_import
|
18 |
+
from __future__ import division
|
19 |
+
from __future__ import print_function
|
20 |
+
|
21 |
+
import collections
|
22 |
+
import csv
|
23 |
+
import os
|
24 |
+
import modeling
|
25 |
+
import optimization
|
26 |
+
import tokenization
|
27 |
+
import tensorflow as tf
|
28 |
+
|
29 |
+
flags = tf.flags
|
30 |
+
|
31 |
+
FLAGS = flags.FLAGS
|
32 |
+
|
33 |
+
## Required parameters
|
34 |
+
flags.DEFINE_string(
|
35 |
+
"data_dir", None,
|
36 |
+
"The input data dir. Should contain the .tsv files (or other data files) "
|
37 |
+
"for the task.")
|
38 |
+
|
39 |
+
flags.DEFINE_string(
|
40 |
+
"bert_config_file", None,
|
41 |
+
"The config json file corresponding to the pre-trained BERT model. "
|
42 |
+
"This specifies the model architecture.")
|
43 |
+
|
44 |
+
flags.DEFINE_string("task_name", None, "The name of the task to train.")
|
45 |
+
|
46 |
+
flags.DEFINE_string("vocab_file", None,
|
47 |
+
"The vocabulary file that the BERT model was trained on.")
|
48 |
+
|
49 |
+
flags.DEFINE_string(
|
50 |
+
"output_dir", None,
|
51 |
+
"The output directory where the model checkpoints will be written.")
|
52 |
+
|
53 |
+
## Other parameters
|
54 |
+
|
55 |
+
flags.DEFINE_string(
|
56 |
+
"init_checkpoint", None,
|
57 |
+
"Initial checkpoint (usually from a pre-trained BERT model).")
|
58 |
+
|
59 |
+
flags.DEFINE_bool(
|
60 |
+
"do_lower_case", True,
|
61 |
+
"Whether to lower case the input text. Should be True for uncased "
|
62 |
+
"models and False for cased models.")
|
63 |
+
|
64 |
+
flags.DEFINE_integer(
|
65 |
+
"max_seq_length", 128,
|
66 |
+
"The maximum total input sequence length after WordPiece tokenization. "
|
67 |
+
"Sequences longer than this will be truncated, and sequences shorter "
|
68 |
+
"than this will be padded.")
|
69 |
+
|
70 |
+
flags.DEFINE_bool("do_train", False, "Whether to run training.")
|
71 |
+
|
72 |
+
flags.DEFINE_bool("do_eval", False, "Whether to run eval on the dev set.")
|
73 |
+
|
74 |
+
flags.DEFINE_bool(
|
75 |
+
"do_predict", False,
|
76 |
+
"Whether to run the model in inference mode on the test set.")
|
77 |
+
|
78 |
+
flags.DEFINE_integer("train_batch_size", 32, "Total batch size for training.")
|
79 |
+
|
80 |
+
flags.DEFINE_integer("eval_batch_size", 8, "Total batch size for eval.")
|
81 |
+
|
82 |
+
flags.DEFINE_integer("predict_batch_size", 8, "Total batch size for predict.")
|
83 |
+
|
84 |
+
flags.DEFINE_float("learning_rate", 5e-5, "The initial learning rate for Adam.")
|
85 |
+
|
86 |
+
flags.DEFINE_float("num_train_epochs", 3.0,
|
87 |
+
"Total number of training epochs to perform.")
|
88 |
+
|
89 |
+
flags.DEFINE_float(
|
90 |
+
"warmup_proportion", 0.1,
|
91 |
+
"Proportion of training to perform linear learning rate warmup for. "
|
92 |
+
"E.g., 0.1 = 10% of training.")
|
93 |
+
|
94 |
+
flags.DEFINE_integer("save_checkpoints_steps", 1000,
|
95 |
+
"How often to save the model checkpoint.")
|
96 |
+
|
97 |
+
flags.DEFINE_integer("iterations_per_loop", 1000,
|
98 |
+
"How many steps to make in each estimator call.")
|
99 |
+
|
100 |
+
flags.DEFINE_bool("use_tpu", False, "Whether to use TPU or GPU/CPU.")
|
101 |
+
|
102 |
+
tf.flags.DEFINE_string(
|
103 |
+
"tpu_name", None,
|
104 |
+
"The Cloud TPU to use for training. This should be either the name "
|
105 |
+
"used when creating the Cloud TPU, or a grpc://ip.address.of.tpu:8470 "
|
106 |
+
"url.")
|
107 |
+
|
108 |
+
tf.flags.DEFINE_string(
|
109 |
+
"tpu_zone", None,
|
110 |
+
"[Optional] GCE zone where the Cloud TPU is located in. If not "
|
111 |
+
"specified, we will attempt to automatically detect the GCE project from "
|
112 |
+
"metadata.")
|
113 |
+
|
114 |
+
tf.flags.DEFINE_string(
|
115 |
+
"gcp_project", None,
|
116 |
+
"[Optional] Project name for the Cloud TPU-enabled project. If not "
|
117 |
+
"specified, we will attempt to automatically detect the GCE project from "
|
118 |
+
"metadata.")
|
119 |
+
|
120 |
+
tf.flags.DEFINE_string("master", None, "[Optional] TensorFlow master URL.")
|
121 |
+
|
122 |
+
flags.DEFINE_integer(
|
123 |
+
"num_tpu_cores", 8,
|
124 |
+
"Only used if `use_tpu` is True. Total number of TPU cores to use.")
|
125 |
+
|
126 |
+
flags.DEFINE_string(
|
127 |
+
"spm_file", None,
|
128 |
+
"SentencePiece model file for Thai language.")
|
129 |
+
|
130 |
+
flags.DEFINE_string(
|
131 |
+
"xnli_language", None,
|
132 |
+
"Specified language for XNLI processing.")
|
133 |
+
|
134 |
+
|
135 |
+
class InputExample(object):
|
136 |
+
"""A single training/test example for simple sequence classification."""
|
137 |
+
|
138 |
+
def __init__(self, guid, text_a, text_b=None, label=None):
|
139 |
+
"""Constructs a InputExample.
|
140 |
+
|
141 |
+
Args:
|
142 |
+
guid: Unique id for the example.
|
143 |
+
text_a: string. The untokenized text of the first sequence. For single
|
144 |
+
sequence tasks, only this sequence must be specified.
|
145 |
+
text_b: (Optional) string. The untokenized text of the second sequence.
|
146 |
+
Only must be specified for sequence pair tasks.
|
147 |
+
label: (Optional) string. The label of the example. This should be
|
148 |
+
specified for train and dev examples, but not for test examples.
|
149 |
+
"""
|
150 |
+
self.guid = guid
|
151 |
+
self.text_a = text_a
|
152 |
+
self.text_b = text_b
|
153 |
+
self.label = label
|
154 |
+
|
155 |
+
|
156 |
+
class InputFeatures(object):
|
157 |
+
"""A single set of features of data."""
|
158 |
+
|
159 |
+
def __init__(self, input_ids, input_mask, segment_ids, label_id):
|
160 |
+
self.input_ids = input_ids
|
161 |
+
self.input_mask = input_mask
|
162 |
+
self.segment_ids = segment_ids
|
163 |
+
self.label_id = label_id
|
164 |
+
|
165 |
+
|
166 |
+
class DataProcessor(object):
|
167 |
+
"""Base class for data converters for sequence classification data sets."""
|
168 |
+
|
169 |
+
def get_train_examples(self, data_dir):
|
170 |
+
"""Gets a collection of `InputExample`s for the train set."""
|
171 |
+
raise NotImplementedError()
|
172 |
+
|
173 |
+
def get_dev_examples(self, data_dir):
|
174 |
+
"""Gets a collection of `InputExample`s for the dev set."""
|
175 |
+
raise NotImplementedError()
|
176 |
+
|
177 |
+
def get_test_examples(self, data_dir):
|
178 |
+
"""Gets a collection of `InputExample`s for prediction."""
|
179 |
+
raise NotImplementedError()
|
180 |
+
|
181 |
+
def get_labels(self):
|
182 |
+
"""Gets the list of labels for this data set."""
|
183 |
+
raise NotImplementedError()
|
184 |
+
|
185 |
+
@classmethod
|
186 |
+
def _read_tsv(cls, input_file, quotechar=None):
|
187 |
+
"""Reads a tab separated value file."""
|
188 |
+
with tf.gfile.Open(input_file, "r") as f:
|
189 |
+
reader = csv.reader(f, delimiter="\t", quotechar=quotechar)
|
190 |
+
lines = []
|
191 |
+
for line in reader:
|
192 |
+
lines.append(line)
|
193 |
+
return lines
|
194 |
+
|
195 |
+
|
196 |
+
class XnliProcessor(DataProcessor):
|
197 |
+
"""Processor for the XNLI data set."""
|
198 |
+
|
199 |
+
def __init__(self, language='zh'):
|
200 |
+
self.language = language
|
201 |
+
|
202 |
+
def get_train_examples(self, data_dir):
|
203 |
+
"""See base class."""
|
204 |
+
lines = self._read_tsv(
|
205 |
+
os.path.join(data_dir, "multinli",
|
206 |
+
"multinli.train.%s.tsv" % self.language))
|
207 |
+
examples = []
|
208 |
+
for (i, line) in enumerate(lines):
|
209 |
+
if i == 0:
|
210 |
+
continue
|
211 |
+
guid = "train-%d" % (i)
|
212 |
+
text_a = tokenization.convert_to_unicode(line[0])
|
213 |
+
text_b = tokenization.convert_to_unicode(line[1])
|
214 |
+
label = tokenization.convert_to_unicode(line[2])
|
215 |
+
if label == tokenization.convert_to_unicode("contradictory"):
|
216 |
+
label = tokenization.convert_to_unicode("contradiction")
|
217 |
+
examples.append(
|
218 |
+
InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
|
219 |
+
return examples
|
220 |
+
|
221 |
+
def get_dev_examples(self, data_dir):
|
222 |
+
"""See base class."""
|
223 |
+
#lines = self._read_tsv(os.path.join(data_dir, "xnli.dev.tsv"))
|
224 |
+
lines = self._read_tsv(os.path.join(data_dir, "xnli.test.tsv"))
|
225 |
+
examples = []
|
226 |
+
for (i, line) in enumerate(lines):
|
227 |
+
if i == 0:
|
228 |
+
continue
|
229 |
+
guid = "dev-%d" % (i)
|
230 |
+
language = tokenization.convert_to_unicode(line[0])
|
231 |
+
if language != tokenization.convert_to_unicode(self.language):
|
232 |
+
continue
|
233 |
+
text_a = tokenization.convert_to_unicode(line[6])
|
234 |
+
text_b = tokenization.convert_to_unicode(line[7])
|
235 |
+
label = tokenization.convert_to_unicode(line[1])
|
236 |
+
examples.append(
|
237 |
+
InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
|
238 |
+
return examples
|
239 |
+
|
240 |
+
def get_labels(self):
|
241 |
+
"""See base class."""
|
242 |
+
return ["contradiction", "entailment", "neutral"]
|
243 |
+
|
244 |
+
|
245 |
+
class MnliProcessor(DataProcessor):
|
246 |
+
"""Processor for the MultiNLI data set (GLUE version)."""
|
247 |
+
|
248 |
+
def get_train_examples(self, data_dir):
|
249 |
+
"""See base class."""
|
250 |
+
return self._create_examples(
|
251 |
+
self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
|
252 |
+
|
253 |
+
def get_dev_examples(self, data_dir):
|
254 |
+
"""See base class."""
|
255 |
+
return self._create_examples(
|
256 |
+
self._read_tsv(os.path.join(data_dir, "dev_matched.tsv")),
|
257 |
+
"dev_matched")
|
258 |
+
|
259 |
+
def get_test_examples(self, data_dir):
|
260 |
+
"""See base class."""
|
261 |
+
return self._create_examples(
|
262 |
+
self._read_tsv(os.path.join(data_dir, "test_matched.tsv")), "test")
|
263 |
+
|
264 |
+
def get_labels(self):
|
265 |
+
"""See base class."""
|
266 |
+
return ["contradiction", "entailment", "neutral"]
|
267 |
+
|
268 |
+
def _create_examples(self, lines, set_type):
|
269 |
+
"""Creates examples for the training and dev sets."""
|
270 |
+
examples = []
|
271 |
+
for (i, line) in enumerate(lines):
|
272 |
+
if i == 0:
|
273 |
+
continue
|
274 |
+
guid = "%s-%s" % (set_type, tokenization.convert_to_unicode(line[0]))
|
275 |
+
text_a = tokenization.convert_to_unicode(line[8])
|
276 |
+
text_b = tokenization.convert_to_unicode(line[9])
|
277 |
+
if set_type == "test":
|
278 |
+
label = "contradiction"
|
279 |
+
else:
|
280 |
+
label = tokenization.convert_to_unicode(line[-1])
|
281 |
+
examples.append(
|
282 |
+
InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
|
283 |
+
return examples
|
284 |
+
|
285 |
+
|
286 |
+
class MrpcProcessor(DataProcessor):
|
287 |
+
"""Processor for the MRPC data set (GLUE version)."""
|
288 |
+
|
289 |
+
def get_train_examples(self, data_dir):
|
290 |
+
"""See base class."""
|
291 |
+
return self._create_examples(
|
292 |
+
self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
|
293 |
+
|
294 |
+
def get_dev_examples(self, data_dir):
|
295 |
+
"""See base class."""
|
296 |
+
return self._create_examples(
|
297 |
+
self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
|
298 |
+
|
299 |
+
def get_test_examples(self, data_dir):
|
300 |
+
"""See base class."""
|
301 |
+
return self._create_examples(
|
302 |
+
self._read_tsv(os.path.join(data_dir, "test.tsv")), "test")
|
303 |
+
|
304 |
+
def get_labels(self):
|
305 |
+
"""See base class."""
|
306 |
+
return ["0", "1"]
|
307 |
+
|
308 |
+
def _create_examples(self, lines, set_type):
|
309 |
+
"""Creates examples for the training and dev sets."""
|
310 |
+
examples = []
|
311 |
+
for (i, line) in enumerate(lines):
|
312 |
+
if i == 0:
|
313 |
+
continue
|
314 |
+
guid = "%s-%s" % (set_type, i)
|
315 |
+
text_a = tokenization.convert_to_unicode(line[3])
|
316 |
+
text_b = tokenization.convert_to_unicode(line[4])
|
317 |
+
if set_type == "test":
|
318 |
+
label = "0"
|
319 |
+
else:
|
320 |
+
label = tokenization.convert_to_unicode(line[0])
|
321 |
+
examples.append(
|
322 |
+
InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
|
323 |
+
return examples
|
324 |
+
|
325 |
+
|
326 |
+
class ColaProcessor(DataProcessor):
|
327 |
+
"""Processor for the CoLA data set (GLUE version)."""
|
328 |
+
|
329 |
+
def get_train_examples(self, data_dir):
|
330 |
+
"""See base class."""
|
331 |
+
return self._create_examples(
|
332 |
+
self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
|
333 |
+
|
334 |
+
def get_dev_examples(self, data_dir):
|
335 |
+
"""See base class."""
|
336 |
+
return self._create_examples(
|
337 |
+
self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
|
338 |
+
|
339 |
+
def get_test_examples(self, data_dir):
|
340 |
+
"""See base class."""
|
341 |
+
return self._create_examples(
|
342 |
+
self._read_tsv(os.path.join(data_dir, "test.tsv")), "test")
|
343 |
+
|
344 |
+
def get_labels(self):
|
345 |
+
"""See base class."""
|
346 |
+
return ["0", "1"]
|
347 |
+
|
348 |
+
def _create_examples(self, lines, set_type):
|
349 |
+
"""Creates examples for the training and dev sets."""
|
350 |
+
examples = []
|
351 |
+
for (i, line) in enumerate(lines):
|
352 |
+
# Only the test set has a header
|
353 |
+
if set_type == "test" and i == 0:
|
354 |
+
continue
|
355 |
+
guid = "%s-%s" % (set_type, i)
|
356 |
+
if set_type == "test":
|
357 |
+
text_a = tokenization.convert_to_unicode(line[1])
|
358 |
+
label = "0"
|
359 |
+
else:
|
360 |
+
text_a = tokenization.convert_to_unicode(line[3])
|
361 |
+
label = tokenization.convert_to_unicode(line[1])
|
362 |
+
examples.append(
|
363 |
+
InputExample(guid=guid, text_a=text_a, text_b=None, label=label))
|
364 |
+
return examples
|
365 |
+
|
366 |
+
|
367 |
+
class WongnaiProcessor(DataProcessor):
|
368 |
+
"""Processor for the Wongnai data set."""
|
369 |
+
|
370 |
+
def get_train_examples(self, data_dir):
|
371 |
+
"""See base class."""
|
372 |
+
return self._create_examples(
|
373 |
+
self._read_wongnai(os.path.join(data_dir, "w_review_train.csv")), "train")
|
374 |
+
|
375 |
+
def get_test_examples(self, data_dir):
|
376 |
+
"""See base class."""
|
377 |
+
return self._create_examples(
|
378 |
+
self._read_wongnai(os.path.join(data_dir, "test_file.csv")), "test")
|
379 |
+
|
380 |
+
def get_labels(self):
|
381 |
+
"""See base class."""
|
382 |
+
return ["1", "2", "3", "4", "5"]
|
383 |
+
|
384 |
+
def _read_wongnai(self, input_file):
|
385 |
+
"""Reads a semicolon separated value file."""
|
386 |
+
with tf.gfile.Open(input_file, "r") as f:
|
387 |
+
reader = csv.reader(f, delimiter=";")
|
388 |
+
lines = []
|
389 |
+
for line in reader:
|
390 |
+
lines.append(line)
|
391 |
+
return lines
|
392 |
+
|
393 |
+
def _create_examples(self, lines, set_type):
|
394 |
+
"""Creates examples for the training and dev sets."""
|
395 |
+
examples = []
|
396 |
+
for (i, line) in enumerate(lines):
|
397 |
+
# Only the test set has a header
|
398 |
+
if set_type == "test" and i == 0:
|
399 |
+
continue
|
400 |
+
guid = "%s-%s" % (set_type, i)
|
401 |
+
if set_type == "test":
|
402 |
+
text_a = tokenization.convert_to_unicode(line[1])
|
403 |
+
label = "3"
|
404 |
+
else:
|
405 |
+
text_a = tokenization.convert_to_unicode(line[0])
|
406 |
+
label = tokenization.convert_to_unicode(line[1])
|
407 |
+
examples.append(
|
408 |
+
InputExample(guid=guid, text_a=text_a, text_b=None, label=label))
|
409 |
+
return examples
|
410 |
+
|
411 |
+
|
412 |
+
def convert_single_example(ex_index, example, label_list, max_seq_length,
|
413 |
+
tokenizer):
|
414 |
+
"""Converts a single `InputExample` into a single `InputFeatures`."""
|
415 |
+
label_map = {}
|
416 |
+
for (i, label) in enumerate(label_list):
|
417 |
+
label_map[label] = i
|
418 |
+
|
419 |
+
tokens_a = tokenizer.tokenize(example.text_a)
|
420 |
+
tokens_b = None
|
421 |
+
if example.text_b:
|
422 |
+
tokens_b = tokenizer.tokenize(example.text_b)
|
423 |
+
|
424 |
+
if tokens_b:
|
425 |
+
# Modifies `tokens_a` and `tokens_b` in place so that the total
|
426 |
+
# length is less than the specified length.
|
427 |
+
# Account for [CLS], [SEP], [SEP] with "- 3"
|
428 |
+
_truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3)
|
429 |
+
else:
|
430 |
+
# Account for [CLS] and [SEP] with "- 2"
|
431 |
+
if len(tokens_a) > max_seq_length - 2:
|
432 |
+
tokens_a = tokens_a[0:(max_seq_length - 2)]
|
433 |
+
|
434 |
+
# The convention in BERT is:
|
435 |
+
# (a) For sequence pairs:
|
436 |
+
# tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
|
437 |
+
# type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1
|
438 |
+
# (b) For single sequences:
|
439 |
+
# tokens: [CLS] the dog is hairy . [SEP]
|
440 |
+
# type_ids: 0 0 0 0 0 0 0
|
441 |
+
#
|
442 |
+
# Where "type_ids" are used to indicate whether this is the first
|
443 |
+
# sequence or the second sequence. The embedding vectors for `type=0` and
|
444 |
+
# `type=1` were learned during pre-training and are added to the wordpiece
|
445 |
+
# embedding vector (and position vector). This is not *strictly* necessary
|
446 |
+
# since the [SEP] token unambiguously separates the sequences, but it makes
|
447 |
+
# it easier for the model to learn the concept of sequences.
|
448 |
+
#
|
449 |
+
# For classification tasks, the first vector (corresponding to [CLS]) is
|
450 |
+
# used as as the "sentence vector". Note that this only makes sense because
|
451 |
+
# the entire model is fine-tuned.
|
452 |
+
tokens = []
|
453 |
+
segment_ids = []
|
454 |
+
tokens.append("[CLS]")
|
455 |
+
segment_ids.append(0)
|
456 |
+
for token in tokens_a:
|
457 |
+
tokens.append(token)
|
458 |
+
segment_ids.append(0)
|
459 |
+
tokens.append("[SEP]")
|
460 |
+
segment_ids.append(0)
|
461 |
+
|
462 |
+
if tokens_b:
|
463 |
+
for token in tokens_b:
|
464 |
+
tokens.append(token)
|
465 |
+
segment_ids.append(1)
|
466 |
+
tokens.append("[SEP]")
|
467 |
+
segment_ids.append(1)
|
468 |
+
|
469 |
+
input_ids = tokenizer.convert_tokens_to_ids(tokens)
|
470 |
+
|
471 |
+
# The mask has 1 for real tokens and 0 for padding tokens. Only real
|
472 |
+
# tokens are attended to.
|
473 |
+
input_mask = [1] * len(input_ids)
|
474 |
+
|
475 |
+
# Zero-pad up to the sequence length.
|
476 |
+
while len(input_ids) < max_seq_length:
|
477 |
+
input_ids.append(0)
|
478 |
+
input_mask.append(0)
|
479 |
+
segment_ids.append(0)
|
480 |
+
|
481 |
+
assert len(input_ids) == max_seq_length
|
482 |
+
assert len(input_mask) == max_seq_length
|
483 |
+
assert len(segment_ids) == max_seq_length
|
484 |
+
|
485 |
+
label_id = label_map[example.label]
|
486 |
+
if ex_index < 5:
|
487 |
+
tf.logging.info("*** Example ***")
|
488 |
+
tf.logging.info("guid: %s" % (example.guid))
|
489 |
+
tf.logging.info("tokens: %s" % " ".join(
|
490 |
+
[tokenization.printable_text(x) for x in tokens]))
|
491 |
+
tf.logging.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
|
492 |
+
tf.logging.info("input_mask: %s" % " ".join([str(x) for x in input_mask]))
|
493 |
+
tf.logging.info("segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
|
494 |
+
tf.logging.info("label: %s (id = %d)" % (example.label, label_id))
|
495 |
+
|
496 |
+
feature = InputFeatures(
|
497 |
+
input_ids=input_ids,
|
498 |
+
input_mask=input_mask,
|
499 |
+
segment_ids=segment_ids,
|
500 |
+
label_id=label_id)
|
501 |
+
return feature
|
502 |
+
|
503 |
+
|
504 |
+
def file_based_convert_examples_to_features(
|
505 |
+
examples, label_list, max_seq_length, tokenizer, output_file):
|
506 |
+
"""Convert a set of `InputExample`s to a TFRecord file."""
|
507 |
+
|
508 |
+
writer = tf.python_io.TFRecordWriter(output_file)
|
509 |
+
|
510 |
+
for (ex_index, example) in enumerate(examples):
|
511 |
+
if ex_index % 10000 == 0:
|
512 |
+
tf.logging.info("Writing example %d of %d" % (ex_index, len(examples)))
|
513 |
+
|
514 |
+
feature = convert_single_example(ex_index, example, label_list,
|
515 |
+
max_seq_length, tokenizer)
|
516 |
+
|
517 |
+
def create_int_feature(values):
|
518 |
+
f = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values)))
|
519 |
+
return f
|
520 |
+
|
521 |
+
features = collections.OrderedDict()
|
522 |
+
features["input_ids"] = create_int_feature(feature.input_ids)
|
523 |
+
features["input_mask"] = create_int_feature(feature.input_mask)
|
524 |
+
features["segment_ids"] = create_int_feature(feature.segment_ids)
|
525 |
+
features["label_ids"] = create_int_feature([feature.label_id])
|
526 |
+
|
527 |
+
tf_example = tf.train.Example(features=tf.train.Features(feature=features))
|
528 |
+
writer.write(tf_example.SerializeToString())
|
529 |
+
|
530 |
+
|
531 |
+
def file_based_input_fn_builder(input_file, seq_length, is_training,
|
532 |
+
drop_remainder):
|
533 |
+
"""Creates an `input_fn` closure to be passed to TPUEstimator."""
|
534 |
+
|
535 |
+
name_to_features = {
|
536 |
+
"input_ids": tf.FixedLenFeature([seq_length], tf.int64),
|
537 |
+
"input_mask": tf.FixedLenFeature([seq_length], tf.int64),
|
538 |
+
"segment_ids": tf.FixedLenFeature([seq_length], tf.int64),
|
539 |
+
"label_ids": tf.FixedLenFeature([], tf.int64),
|
540 |
+
}
|
541 |
+
|
542 |
+
def _decode_record(record, name_to_features):
|
543 |
+
"""Decodes a record to a TensorFlow example."""
|
544 |
+
example = tf.parse_single_example(record, name_to_features)
|
545 |
+
|
546 |
+
# tf.Example only supports tf.int64, but the TPU only supports tf.int32.
|
547 |
+
# So cast all int64 to int32.
|
548 |
+
for name in list(example.keys()):
|
549 |
+
t = example[name]
|
550 |
+
if t.dtype == tf.int64:
|
551 |
+
t = tf.to_int32(t)
|
552 |
+
example[name] = t
|
553 |
+
|
554 |
+
return example
|
555 |
+
|
556 |
+
def input_fn(params):
|
557 |
+
"""The actual input function."""
|
558 |
+
batch_size = params["batch_size"]
|
559 |
+
|
560 |
+
# For training, we want a lot of parallel reading and shuffling.
|
561 |
+
# For eval, we want no shuffling and parallel reading doesn't matter.
|
562 |
+
d = tf.data.TFRecordDataset(input_file)
|
563 |
+
if is_training:
|
564 |
+
d = d.repeat()
|
565 |
+
d = d.shuffle(buffer_size=100)
|
566 |
+
|
567 |
+
d = d.apply(
|
568 |
+
tf.contrib.data.map_and_batch(
|
569 |
+
lambda record: _decode_record(record, name_to_features),
|
570 |
+
batch_size=batch_size,
|
571 |
+
drop_remainder=drop_remainder))
|
572 |
+
|
573 |
+
return d
|
574 |
+
|
575 |
+
return input_fn
|
576 |
+
|
577 |
+
|
578 |
+
def _truncate_seq_pair(tokens_a, tokens_b, max_length):
|
579 |
+
"""Truncates a sequence pair in place to the maximum length."""
|
580 |
+
|
581 |
+
# This is a simple heuristic which will always truncate the longer sequence
|
582 |
+
# one token at a time. This makes more sense than truncating an equal percent
|
583 |
+
# of tokens from each, since if one sequence is very short then each token
|
584 |
+
# that's truncated likely contains more information than a longer sequence.
|
585 |
+
while True:
|
586 |
+
total_length = len(tokens_a) + len(tokens_b)
|
587 |
+
if total_length <= max_length:
|
588 |
+
break
|
589 |
+
if len(tokens_a) > len(tokens_b):
|
590 |
+
tokens_a.pop()
|
591 |
+
else:
|
592 |
+
tokens_b.pop()
|
593 |
+
|
594 |
+
|
595 |
+
def create_model(bert_config, is_training, input_ids, input_mask, segment_ids,
|
596 |
+
labels, num_labels, use_one_hot_embeddings):
|
597 |
+
"""Creates a classification model."""
|
598 |
+
model = modeling.BertModel(
|
599 |
+
config=bert_config,
|
600 |
+
is_training=is_training,
|
601 |
+
input_ids=input_ids,
|
602 |
+
input_mask=input_mask,
|
603 |
+
token_type_ids=segment_ids,
|
604 |
+
use_one_hot_embeddings=use_one_hot_embeddings)
|
605 |
+
|
606 |
+
# In the demo, we are doing a simple classification task on the entire
|
607 |
+
# segment.
|
608 |
+
#
|
609 |
+
# If you want to use the token-level output, use model.get_sequence_output()
|
610 |
+
# instead.
|
611 |
+
output_layer = model.get_pooled_output()
|
612 |
+
|
613 |
+
hidden_size = output_layer.shape[-1].value
|
614 |
+
|
615 |
+
output_weights = tf.get_variable(
|
616 |
+
"output_weights", [num_labels, hidden_size],
|
617 |
+
initializer=tf.truncated_normal_initializer(stddev=0.02))
|
618 |
+
|
619 |
+
output_bias = tf.get_variable(
|
620 |
+
"output_bias", [num_labels], initializer=tf.zeros_initializer())
|
621 |
+
|
622 |
+
with tf.variable_scope("loss"):
|
623 |
+
if is_training:
|
624 |
+
# I.e., 0.1 dropout
|
625 |
+
output_layer = tf.nn.dropout(output_layer, keep_prob=0.9)
|
626 |
+
|
627 |
+
logits = tf.matmul(output_layer, output_weights, transpose_b=True)
|
628 |
+
logits = tf.nn.bias_add(logits, output_bias)
|
629 |
+
probabilities = tf.nn.softmax(logits, axis=-1)
|
630 |
+
log_probs = tf.nn.log_softmax(logits, axis=-1)
|
631 |
+
|
632 |
+
one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)
|
633 |
+
|
634 |
+
per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)
|
635 |
+
loss = tf.reduce_mean(per_example_loss)
|
636 |
+
|
637 |
+
return (loss, per_example_loss, logits, probabilities)
|
638 |
+
|
639 |
+
|
640 |
+
def model_fn_builder(bert_config, num_labels, init_checkpoint, learning_rate,
|
641 |
+
num_train_steps, num_warmup_steps, use_tpu,
|
642 |
+
use_one_hot_embeddings):
|
643 |
+
"""Returns `model_fn` closure for TPUEstimator."""
|
644 |
+
|
645 |
+
def model_fn(features, labels, mode, params): # pylint: disable=unused-argument
|
646 |
+
"""The `model_fn` for TPUEstimator."""
|
647 |
+
|
648 |
+
tf.logging.info("*** Features ***")
|
649 |
+
for name in sorted(features.keys()):
|
650 |
+
tf.logging.info(" name = %s, shape = %s" % (name, features[name].shape))
|
651 |
+
|
652 |
+
input_ids = features["input_ids"]
|
653 |
+
input_mask = features["input_mask"]
|
654 |
+
segment_ids = features["segment_ids"]
|
655 |
+
label_ids = features["label_ids"]
|
656 |
+
|
657 |
+
is_training = (mode == tf.estimator.ModeKeys.TRAIN)
|
658 |
+
|
659 |
+
(total_loss, per_example_loss, logits, probabilities) = create_model(
|
660 |
+
bert_config, is_training, input_ids, input_mask, segment_ids, label_ids,
|
661 |
+
num_labels, use_one_hot_embeddings)
|
662 |
+
|
663 |
+
tvars = tf.trainable_variables()
|
664 |
+
|
665 |
+
scaffold_fn = None
|
666 |
+
if init_checkpoint:
|
667 |
+
(assignment_map, initialized_variable_names
|
668 |
+
) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint)
|
669 |
+
if use_tpu:
|
670 |
+
|
671 |
+
def tpu_scaffold():
|
672 |
+
tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
|
673 |
+
return tf.train.Scaffold()
|
674 |
+
|
675 |
+
scaffold_fn = tpu_scaffold
|
676 |
+
else:
|
677 |
+
tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
|
678 |
+
|
679 |
+
tf.logging.info("**** Trainable Variables ****")
|
680 |
+
for var in tvars:
|
681 |
+
init_string = ""
|
682 |
+
if var.name in initialized_variable_names:
|
683 |
+
init_string = ", *INIT_FROM_CKPT*"
|
684 |
+
tf.logging.info(" name = %s, shape = %s%s", var.name, var.shape,
|
685 |
+
init_string)
|
686 |
+
|
687 |
+
output_spec = None
|
688 |
+
if mode == tf.estimator.ModeKeys.TRAIN:
|
689 |
+
|
690 |
+
train_op = optimization.create_optimizer(
|
691 |
+
total_loss, learning_rate, num_train_steps, num_warmup_steps, use_tpu)
|
692 |
+
|
693 |
+
output_spec = tf.contrib.tpu.TPUEstimatorSpec(
|
694 |
+
mode=mode,
|
695 |
+
loss=total_loss,
|
696 |
+
train_op=train_op,
|
697 |
+
scaffold_fn=scaffold_fn)
|
698 |
+
elif mode == tf.estimator.ModeKeys.EVAL:
|
699 |
+
|
700 |
+
def metric_fn(per_example_loss, label_ids, logits):
|
701 |
+
predictions = tf.argmax(logits, axis=-1, output_type=tf.int32)
|
702 |
+
accuracy = tf.metrics.accuracy(label_ids, predictions)
|
703 |
+
loss = tf.metrics.mean(per_example_loss)
|
704 |
+
return {
|
705 |
+
"eval_accuracy": accuracy,
|
706 |
+
"eval_loss": loss,
|
707 |
+
}
|
708 |
+
|
709 |
+
eval_metrics = (metric_fn, [per_example_loss, label_ids, logits])
|
710 |
+
output_spec = tf.contrib.tpu.TPUEstimatorSpec(
|
711 |
+
mode=mode,
|
712 |
+
loss=total_loss,
|
713 |
+
eval_metrics=eval_metrics,
|
714 |
+
scaffold_fn=scaffold_fn)
|
715 |
+
else:
|
716 |
+
output_spec = tf.contrib.tpu.TPUEstimatorSpec(
|
717 |
+
mode=mode, predictions=probabilities, scaffold_fn=scaffold_fn)
|
718 |
+
return output_spec
|
719 |
+
|
720 |
+
return model_fn
|
721 |
+
|
722 |
+
|
723 |
+
# This function is not used by this file but is still used by the Colab and
|
724 |
+
# people who depend on it.
|
725 |
+
def input_fn_builder(features, seq_length, is_training, drop_remainder):
|
726 |
+
"""Creates an `input_fn` closure to be passed to TPUEstimator."""
|
727 |
+
|
728 |
+
all_input_ids = []
|
729 |
+
all_input_mask = []
|
730 |
+
all_segment_ids = []
|
731 |
+
all_label_ids = []
|
732 |
+
|
733 |
+
for feature in features:
|
734 |
+
all_input_ids.append(feature.input_ids)
|
735 |
+
all_input_mask.append(feature.input_mask)
|
736 |
+
all_segment_ids.append(feature.segment_ids)
|
737 |
+
all_label_ids.append(feature.label_id)
|
738 |
+
|
739 |
+
def input_fn(params):
|
740 |
+
"""The actual input function."""
|
741 |
+
batch_size = params["batch_size"]
|
742 |
+
|
743 |
+
num_examples = len(features)
|
744 |
+
|
745 |
+
# This is for demo purposes and does NOT scale to large data sets. We do
|
746 |
+
# not use Dataset.from_generator() because that uses tf.py_func which is
|
747 |
+
# not TPU compatible. The right way to load data is with TFRecordReader.
|
748 |
+
d = tf.data.Dataset.from_tensor_slices({
|
749 |
+
"input_ids":
|
750 |
+
tf.constant(
|
751 |
+
all_input_ids, shape=[num_examples, seq_length],
|
752 |
+
dtype=tf.int32),
|
753 |
+
"input_mask":
|
754 |
+
tf.constant(
|
755 |
+
all_input_mask,
|
756 |
+
shape=[num_examples, seq_length],
|
757 |
+
dtype=tf.int32),
|
758 |
+
"segment_ids":
|
759 |
+
tf.constant(
|
760 |
+
all_segment_ids,
|
761 |
+
shape=[num_examples, seq_length],
|
762 |
+
dtype=tf.int32),
|
763 |
+
"label_ids":
|
764 |
+
tf.constant(all_label_ids, shape=[num_examples], dtype=tf.int32),
|
765 |
+
})
|
766 |
+
|
767 |
+
if is_training:
|
768 |
+
d = d.repeat()
|
769 |
+
d = d.shuffle(buffer_size=100)
|
770 |
+
|
771 |
+
d = d.batch(batch_size=batch_size, drop_remainder=drop_remainder)
|
772 |
+
return d
|
773 |
+
|
774 |
+
return input_fn
|
775 |
+
|
776 |
+
|
777 |
+
# This function is not used by this file but is still used by the Colab and
|
778 |
+
# people who depend on it.
|
779 |
+
def convert_examples_to_features(examples, label_list, max_seq_length,
|
780 |
+
tokenizer):
|
781 |
+
"""Convert a set of `InputExample`s to a list of `InputFeatures`."""
|
782 |
+
|
783 |
+
features = []
|
784 |
+
for (ex_index, example) in enumerate(examples):
|
785 |
+
if ex_index % 10000 == 0:
|
786 |
+
tf.logging.info("Writing example %d of %d" % (ex_index, len(examples)))
|
787 |
+
|
788 |
+
feature = convert_single_example(ex_index, example, label_list,
|
789 |
+
max_seq_length, tokenizer)
|
790 |
+
|
791 |
+
features.append(feature)
|
792 |
+
return features
|
793 |
+
|
794 |
+
|
795 |
+
def main(_):
|
796 |
+
tf.logging.set_verbosity(tf.logging.INFO)
|
797 |
+
|
798 |
+
processors = {
|
799 |
+
"cola": ColaProcessor,
|
800 |
+
"mnli": MnliProcessor,
|
801 |
+
"mrpc": MrpcProcessor,
|
802 |
+
"xnli": XnliProcessor,
|
803 |
+
"wongnai": WongnaiProcessor,
|
804 |
+
}
|
805 |
+
|
806 |
+
if not FLAGS.do_train and not FLAGS.do_eval and not FLAGS.do_predict:
|
807 |
+
raise ValueError(
|
808 |
+
"At least one of `do_train`, `do_eval` or `do_predict' must be True.")
|
809 |
+
|
810 |
+
bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file)
|
811 |
+
|
812 |
+
if FLAGS.max_seq_length > bert_config.max_position_embeddings:
|
813 |
+
raise ValueError(
|
814 |
+
"Cannot use sequence length %d because the BERT model "
|
815 |
+
"was only trained up to sequence length %d" %
|
816 |
+
(FLAGS.max_seq_length, bert_config.max_position_embeddings))
|
817 |
+
|
818 |
+
tf.gfile.MakeDirs(FLAGS.output_dir)
|
819 |
+
|
820 |
+
task_name = FLAGS.task_name.lower()
|
821 |
+
|
822 |
+
if task_name not in processors:
|
823 |
+
raise ValueError("Task not found: %s" % (task_name))
|
824 |
+
|
825 |
+
if task_name == 'xnli' and FLAGS.xnli_language:
|
826 |
+
processor = processors[task_name](FLAGS.xnli_language)
|
827 |
+
else:
|
828 |
+
processor = processors[task_name]()
|
829 |
+
|
830 |
+
label_list = processor.get_labels()
|
831 |
+
|
832 |
+
if (task_name == 'xnli' and FLAGS.xnli_language == 'th') or task_name == 'wongnai':
|
833 |
+
if not FLAGS.spm_file:
|
834 |
+
print("Please specify the SentencePiece model file by using --spm_file.")
|
835 |
+
return
|
836 |
+
tokenizer = tokenization.ThaiTokenizer(vocab_file=FLAGS.vocab_file, spm_file=FLAGS.spm_file)
|
837 |
+
else:
|
838 |
+
tokenizer = tokenization.FullTokenizer(
|
839 |
+
vocab_file=FLAGS.vocab_file, do_lower_case=FLAGS.do_lower_case)
|
840 |
+
|
841 |
+
tpu_cluster_resolver = None
|
842 |
+
if FLAGS.use_tpu and FLAGS.tpu_name:
|
843 |
+
tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
|
844 |
+
FLAGS.tpu_name, zone=FLAGS.tpu_zone, project=FLAGS.gcp_project)
|
845 |
+
|
846 |
+
is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2
|
847 |
+
run_config = tf.contrib.tpu.RunConfig(
|
848 |
+
cluster=tpu_cluster_resolver,
|
849 |
+
master=FLAGS.master,
|
850 |
+
model_dir=FLAGS.output_dir,
|
851 |
+
save_checkpoints_steps=FLAGS.save_checkpoints_steps,
|
852 |
+
tpu_config=tf.contrib.tpu.TPUConfig(
|
853 |
+
iterations_per_loop=FLAGS.iterations_per_loop,
|
854 |
+
num_shards=FLAGS.num_tpu_cores,
|
855 |
+
per_host_input_for_training=is_per_host))
|
856 |
+
|
857 |
+
train_examples = None
|
858 |
+
num_train_steps = None
|
859 |
+
num_warmup_steps = None
|
860 |
+
if FLAGS.do_train:
|
861 |
+
train_examples = processor.get_train_examples(FLAGS.data_dir)
|
862 |
+
num_train_steps = int(
|
863 |
+
len(train_examples) / FLAGS.train_batch_size * FLAGS.num_train_epochs)
|
864 |
+
num_warmup_steps = int(num_train_steps * FLAGS.warmup_proportion)
|
865 |
+
|
866 |
+
model_fn = model_fn_builder(
|
867 |
+
bert_config=bert_config,
|
868 |
+
num_labels=len(label_list),
|
869 |
+
init_checkpoint=FLAGS.init_checkpoint,
|
870 |
+
learning_rate=FLAGS.learning_rate,
|
871 |
+
num_train_steps=num_train_steps,
|
872 |
+
num_warmup_steps=num_warmup_steps,
|
873 |
+
use_tpu=FLAGS.use_tpu,
|
874 |
+
use_one_hot_embeddings=FLAGS.use_tpu)
|
875 |
+
|
876 |
+
# If TPU is not available, this will fall back to normal Estimator on CPU
|
877 |
+
# or GPU.
|
878 |
+
estimator = tf.contrib.tpu.TPUEstimator(
|
879 |
+
use_tpu=FLAGS.use_tpu,
|
880 |
+
model_fn=model_fn,
|
881 |
+
config=run_config,
|
882 |
+
train_batch_size=FLAGS.train_batch_size,
|
883 |
+
eval_batch_size=FLAGS.eval_batch_size,
|
884 |
+
predict_batch_size=FLAGS.predict_batch_size)
|
885 |
+
|
886 |
+
if FLAGS.do_train:
|
887 |
+
train_file = os.path.join(FLAGS.output_dir, "train.tf_record")
|
888 |
+
file_based_convert_examples_to_features(
|
889 |
+
train_examples, label_list, FLAGS.max_seq_length, tokenizer, train_file)
|
890 |
+
tf.logging.info("***** Running training *****")
|
891 |
+
tf.logging.info(" Num examples = %d", len(train_examples))
|
892 |
+
tf.logging.info(" Batch size = %d", FLAGS.train_batch_size)
|
893 |
+
tf.logging.info(" Num steps = %d", num_train_steps)
|
894 |
+
train_input_fn = file_based_input_fn_builder(
|
895 |
+
input_file=train_file,
|
896 |
+
seq_length=FLAGS.max_seq_length,
|
897 |
+
is_training=True,
|
898 |
+
drop_remainder=True)
|
899 |
+
estimator.train(input_fn=train_input_fn, max_steps=num_train_steps)
|
900 |
+
|
901 |
+
if FLAGS.do_eval:
|
902 |
+
eval_examples = processor.get_dev_examples(FLAGS.data_dir)
|
903 |
+
eval_file = os.path.join(FLAGS.output_dir, "eval.tf_record")
|
904 |
+
file_based_convert_examples_to_features(
|
905 |
+
eval_examples, label_list, FLAGS.max_seq_length, tokenizer, eval_file)
|
906 |
+
|
907 |
+
tf.logging.info("***** Running evaluation *****")
|
908 |
+
tf.logging.info(" Num examples = %d", len(eval_examples))
|
909 |
+
tf.logging.info(" Batch size = %d", FLAGS.eval_batch_size)
|
910 |
+
|
911 |
+
# This tells the estimator to run through the entire set.
|
912 |
+
eval_steps = None
|
913 |
+
# However, if running eval on the TPU, you will need to specify the
|
914 |
+
# number of steps.
|
915 |
+
if FLAGS.use_tpu:
|
916 |
+
# Eval will be slightly WRONG on the TPU because it will truncate
|
917 |
+
# the last batch.
|
918 |
+
eval_steps = int(len(eval_examples) / FLAGS.eval_batch_size)
|
919 |
+
|
920 |
+
eval_drop_remainder = True if FLAGS.use_tpu else False
|
921 |
+
eval_input_fn = file_based_input_fn_builder(
|
922 |
+
input_file=eval_file,
|
923 |
+
seq_length=FLAGS.max_seq_length,
|
924 |
+
is_training=False,
|
925 |
+
drop_remainder=eval_drop_remainder)
|
926 |
+
|
927 |
+
result = estimator.evaluate(input_fn=eval_input_fn, steps=eval_steps)
|
928 |
+
|
929 |
+
output_eval_file = os.path.join(FLAGS.output_dir, "eval_results.txt")
|
930 |
+
with tf.gfile.GFile(output_eval_file, "w") as writer:
|
931 |
+
tf.logging.info("***** Eval results *****")
|
932 |
+
for key in sorted(result.keys()):
|
933 |
+
tf.logging.info(" %s = %s", key, str(result[key]))
|
934 |
+
writer.write("%s = %s\n" % (key, str(result[key])))
|
935 |
+
|
936 |
+
if FLAGS.do_predict:
|
937 |
+
predict_examples = processor.get_test_examples(FLAGS.data_dir)
|
938 |
+
predict_file = os.path.join(FLAGS.output_dir, "predict.tf_record")
|
939 |
+
file_based_convert_examples_to_features(predict_examples, label_list,
|
940 |
+
FLAGS.max_seq_length, tokenizer,
|
941 |
+
predict_file)
|
942 |
+
|
943 |
+
tf.logging.info("***** Running prediction*****")
|
944 |
+
tf.logging.info(" Num examples = %d", len(predict_examples))
|
945 |
+
tf.logging.info(" Batch size = %d", FLAGS.predict_batch_size)
|
946 |
+
|
947 |
+
if FLAGS.use_tpu:
|
948 |
+
# Warning: According to tpu_estimator.py Prediction on TPU is an
|
949 |
+
# experimental feature and hence not supported here
|
950 |
+
raise ValueError("Prediction in TPU not supported")
|
951 |
+
|
952 |
+
predict_drop_remainder = True if FLAGS.use_tpu else False
|
953 |
+
predict_input_fn = file_based_input_fn_builder(
|
954 |
+
input_file=predict_file,
|
955 |
+
seq_length=FLAGS.max_seq_length,
|
956 |
+
is_training=False,
|
957 |
+
drop_remainder=predict_drop_remainder)
|
958 |
+
|
959 |
+
result = estimator.predict(input_fn=predict_input_fn)
|
960 |
+
|
961 |
+
output_predict_file = os.path.join(FLAGS.output_dir, "test_results.tsv")
|
962 |
+
with tf.gfile.GFile(output_predict_file, "w") as writer:
|
963 |
+
tf.logging.info("***** Predict results *****")
|
964 |
+
for prediction in result:
|
965 |
+
output_line = "\t".join(
|
966 |
+
str(class_probability) for class_probability in prediction) + "\n"
|
967 |
+
writer.write(output_line)
|
968 |
+
|
969 |
+
|
970 |
+
if __name__ == "__main__":
|
971 |
+
flags.mark_flag_as_required("data_dir")
|
972 |
+
flags.mark_flag_as_required("task_name")
|
973 |
+
flags.mark_flag_as_required("vocab_file")
|
974 |
+
flags.mark_flag_as_required("bert_config_file")
|
975 |
+
flags.mark_flag_as_required("output_dir")
|
976 |
+
tf.app.run()
|
bert/run_pretraining.py
ADDED
@@ -0,0 +1,493 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""Run masked LM/next sentence masked_lm pre-training for BERT."""
|
16 |
+
|
17 |
+
from __future__ import absolute_import
|
18 |
+
from __future__ import division
|
19 |
+
from __future__ import print_function
|
20 |
+
|
21 |
+
import os
|
22 |
+
import modeling
|
23 |
+
import optimization
|
24 |
+
import tensorflow as tf
|
25 |
+
|
26 |
+
flags = tf.flags
|
27 |
+
|
28 |
+
FLAGS = flags.FLAGS
|
29 |
+
|
30 |
+
## Required parameters
|
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 |
+
|
36 |
+
flags.DEFINE_string(
|
37 |
+
"input_file", None,
|
38 |
+
"Input TF example files (can be a glob or comma separated).")
|
39 |
+
|
40 |
+
flags.DEFINE_string(
|
41 |
+
"output_dir", None,
|
42 |
+
"The output directory where the model checkpoints will be written.")
|
43 |
+
|
44 |
+
## Other parameters
|
45 |
+
flags.DEFINE_string(
|
46 |
+
"init_checkpoint", None,
|
47 |
+
"Initial checkpoint (usually from a pre-trained BERT model).")
|
48 |
+
|
49 |
+
flags.DEFINE_integer(
|
50 |
+
"max_seq_length", 128,
|
51 |
+
"The maximum total input sequence length after WordPiece tokenization. "
|
52 |
+
"Sequences longer than this will be truncated, and sequences shorter "
|
53 |
+
"than this will be padded. Must match data generation.")
|
54 |
+
|
55 |
+
flags.DEFINE_integer(
|
56 |
+
"max_predictions_per_seq", 20,
|
57 |
+
"Maximum number of masked LM predictions per sequence. "
|
58 |
+
"Must match data generation.")
|
59 |
+
|
60 |
+
flags.DEFINE_bool("do_train", False, "Whether to run training.")
|
61 |
+
|
62 |
+
flags.DEFINE_bool("do_eval", False, "Whether to run eval on the dev set.")
|
63 |
+
|
64 |
+
flags.DEFINE_integer("train_batch_size", 32, "Total batch size for training.")
|
65 |
+
|
66 |
+
flags.DEFINE_integer("eval_batch_size", 8, "Total batch size for eval.")
|
67 |
+
|
68 |
+
flags.DEFINE_float("learning_rate", 5e-5, "The initial learning rate for Adam.")
|
69 |
+
|
70 |
+
flags.DEFINE_integer("num_train_steps", 100000, "Number of training steps.")
|
71 |
+
|
72 |
+
flags.DEFINE_integer("num_warmup_steps", 10000, "Number of warmup steps.")
|
73 |
+
|
74 |
+
flags.DEFINE_integer("save_checkpoints_steps", 1000,
|
75 |
+
"How often to save the model checkpoint.")
|
76 |
+
|
77 |
+
flags.DEFINE_integer("iterations_per_loop", 1000,
|
78 |
+
"How many steps to make in each estimator call.")
|
79 |
+
|
80 |
+
flags.DEFINE_integer("max_eval_steps", 100, "Maximum number of eval steps.")
|
81 |
+
|
82 |
+
flags.DEFINE_bool("use_tpu", False, "Whether to use TPU or GPU/CPU.")
|
83 |
+
|
84 |
+
tf.flags.DEFINE_string(
|
85 |
+
"tpu_name", None,
|
86 |
+
"The Cloud TPU to use for training. This should be either the name "
|
87 |
+
"used when creating the Cloud TPU, or a grpc://ip.address.of.tpu:8470 "
|
88 |
+
"url.")
|
89 |
+
|
90 |
+
tf.flags.DEFINE_string(
|
91 |
+
"tpu_zone", None,
|
92 |
+
"[Optional] GCE zone where the Cloud TPU is located in. If not "
|
93 |
+
"specified, we will attempt to automatically detect the GCE project from "
|
94 |
+
"metadata.")
|
95 |
+
|
96 |
+
tf.flags.DEFINE_string(
|
97 |
+
"gcp_project", None,
|
98 |
+
"[Optional] Project name for the Cloud TPU-enabled project. If not "
|
99 |
+
"specified, we will attempt to automatically detect the GCE project from "
|
100 |
+
"metadata.")
|
101 |
+
|
102 |
+
tf.flags.DEFINE_string("master", None, "[Optional] TensorFlow master URL.")
|
103 |
+
|
104 |
+
flags.DEFINE_integer(
|
105 |
+
"num_tpu_cores", 8,
|
106 |
+
"Only used if `use_tpu` is True. Total number of TPU cores to use.")
|
107 |
+
|
108 |
+
|
109 |
+
def model_fn_builder(bert_config, init_checkpoint, learning_rate,
|
110 |
+
num_train_steps, num_warmup_steps, use_tpu,
|
111 |
+
use_one_hot_embeddings):
|
112 |
+
"""Returns `model_fn` closure for TPUEstimator."""
|
113 |
+
|
114 |
+
def model_fn(features, labels, mode, params): # pylint: disable=unused-argument
|
115 |
+
"""The `model_fn` for TPUEstimator."""
|
116 |
+
|
117 |
+
tf.logging.info("*** Features ***")
|
118 |
+
for name in sorted(features.keys()):
|
119 |
+
tf.logging.info(" name = %s, shape = %s" % (name, features[name].shape))
|
120 |
+
|
121 |
+
input_ids = features["input_ids"]
|
122 |
+
input_mask = features["input_mask"]
|
123 |
+
segment_ids = features["segment_ids"]
|
124 |
+
masked_lm_positions = features["masked_lm_positions"]
|
125 |
+
masked_lm_ids = features["masked_lm_ids"]
|
126 |
+
masked_lm_weights = features["masked_lm_weights"]
|
127 |
+
next_sentence_labels = features["next_sentence_labels"]
|
128 |
+
|
129 |
+
is_training = (mode == tf.estimator.ModeKeys.TRAIN)
|
130 |
+
|
131 |
+
model = modeling.BertModel(
|
132 |
+
config=bert_config,
|
133 |
+
is_training=is_training,
|
134 |
+
input_ids=input_ids,
|
135 |
+
input_mask=input_mask,
|
136 |
+
token_type_ids=segment_ids,
|
137 |
+
use_one_hot_embeddings=use_one_hot_embeddings)
|
138 |
+
|
139 |
+
(masked_lm_loss,
|
140 |
+
masked_lm_example_loss, masked_lm_log_probs) = get_masked_lm_output(
|
141 |
+
bert_config, model.get_sequence_output(), model.get_embedding_table(),
|
142 |
+
masked_lm_positions, masked_lm_ids, masked_lm_weights)
|
143 |
+
|
144 |
+
(next_sentence_loss, next_sentence_example_loss,
|
145 |
+
next_sentence_log_probs) = get_next_sentence_output(
|
146 |
+
bert_config, model.get_pooled_output(), next_sentence_labels)
|
147 |
+
|
148 |
+
total_loss = masked_lm_loss + next_sentence_loss
|
149 |
+
|
150 |
+
tvars = tf.trainable_variables()
|
151 |
+
|
152 |
+
initialized_variable_names = {}
|
153 |
+
scaffold_fn = None
|
154 |
+
if init_checkpoint:
|
155 |
+
(assignment_map, initialized_variable_names
|
156 |
+
) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint)
|
157 |
+
if use_tpu:
|
158 |
+
|
159 |
+
def tpu_scaffold():
|
160 |
+
tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
|
161 |
+
return tf.train.Scaffold()
|
162 |
+
|
163 |
+
scaffold_fn = tpu_scaffold
|
164 |
+
else:
|
165 |
+
tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
|
166 |
+
|
167 |
+
tf.logging.info("**** Trainable Variables ****")
|
168 |
+
for var in tvars:
|
169 |
+
init_string = ""
|
170 |
+
if var.name in initialized_variable_names:
|
171 |
+
init_string = ", *INIT_FROM_CKPT*"
|
172 |
+
tf.logging.info(" name = %s, shape = %s%s", var.name, var.shape,
|
173 |
+
init_string)
|
174 |
+
|
175 |
+
output_spec = None
|
176 |
+
if mode == tf.estimator.ModeKeys.TRAIN:
|
177 |
+
train_op = optimization.create_optimizer(
|
178 |
+
total_loss, learning_rate, num_train_steps, num_warmup_steps, use_tpu)
|
179 |
+
|
180 |
+
output_spec = tf.contrib.tpu.TPUEstimatorSpec(
|
181 |
+
mode=mode,
|
182 |
+
loss=total_loss,
|
183 |
+
train_op=train_op,
|
184 |
+
scaffold_fn=scaffold_fn)
|
185 |
+
elif mode == tf.estimator.ModeKeys.EVAL:
|
186 |
+
|
187 |
+
def metric_fn(masked_lm_example_loss, masked_lm_log_probs, masked_lm_ids,
|
188 |
+
masked_lm_weights, next_sentence_example_loss,
|
189 |
+
next_sentence_log_probs, next_sentence_labels):
|
190 |
+
"""Computes the loss and accuracy of the model."""
|
191 |
+
masked_lm_log_probs = tf.reshape(masked_lm_log_probs,
|
192 |
+
[-1, masked_lm_log_probs.shape[-1]])
|
193 |
+
masked_lm_predictions = tf.argmax(
|
194 |
+
masked_lm_log_probs, axis=-1, output_type=tf.int32)
|
195 |
+
masked_lm_example_loss = tf.reshape(masked_lm_example_loss, [-1])
|
196 |
+
masked_lm_ids = tf.reshape(masked_lm_ids, [-1])
|
197 |
+
masked_lm_weights = tf.reshape(masked_lm_weights, [-1])
|
198 |
+
masked_lm_accuracy = tf.metrics.accuracy(
|
199 |
+
labels=masked_lm_ids,
|
200 |
+
predictions=masked_lm_predictions,
|
201 |
+
weights=masked_lm_weights)
|
202 |
+
masked_lm_mean_loss = tf.metrics.mean(
|
203 |
+
values=masked_lm_example_loss, weights=masked_lm_weights)
|
204 |
+
|
205 |
+
next_sentence_log_probs = tf.reshape(
|
206 |
+
next_sentence_log_probs, [-1, next_sentence_log_probs.shape[-1]])
|
207 |
+
next_sentence_predictions = tf.argmax(
|
208 |
+
next_sentence_log_probs, axis=-1, output_type=tf.int32)
|
209 |
+
next_sentence_labels = tf.reshape(next_sentence_labels, [-1])
|
210 |
+
next_sentence_accuracy = tf.metrics.accuracy(
|
211 |
+
labels=next_sentence_labels, predictions=next_sentence_predictions)
|
212 |
+
next_sentence_mean_loss = tf.metrics.mean(
|
213 |
+
values=next_sentence_example_loss)
|
214 |
+
|
215 |
+
return {
|
216 |
+
"masked_lm_accuracy": masked_lm_accuracy,
|
217 |
+
"masked_lm_loss": masked_lm_mean_loss,
|
218 |
+
"next_sentence_accuracy": next_sentence_accuracy,
|
219 |
+
"next_sentence_loss": next_sentence_mean_loss,
|
220 |
+
}
|
221 |
+
|
222 |
+
eval_metrics = (metric_fn, [
|
223 |
+
masked_lm_example_loss, masked_lm_log_probs, masked_lm_ids,
|
224 |
+
masked_lm_weights, next_sentence_example_loss,
|
225 |
+
next_sentence_log_probs, next_sentence_labels
|
226 |
+
])
|
227 |
+
output_spec = tf.contrib.tpu.TPUEstimatorSpec(
|
228 |
+
mode=mode,
|
229 |
+
loss=total_loss,
|
230 |
+
eval_metrics=eval_metrics,
|
231 |
+
scaffold_fn=scaffold_fn)
|
232 |
+
else:
|
233 |
+
raise ValueError("Only TRAIN and EVAL modes are supported: %s" % (mode))
|
234 |
+
|
235 |
+
return output_spec
|
236 |
+
|
237 |
+
return model_fn
|
238 |
+
|
239 |
+
|
240 |
+
def get_masked_lm_output(bert_config, input_tensor, output_weights, positions,
|
241 |
+
label_ids, label_weights):
|
242 |
+
"""Get loss and log probs for the masked LM."""
|
243 |
+
input_tensor = gather_indexes(input_tensor, positions)
|
244 |
+
|
245 |
+
with tf.variable_scope("cls/predictions"):
|
246 |
+
# We apply one more non-linear transformation before the output layer.
|
247 |
+
# This matrix is not used after pre-training.
|
248 |
+
with tf.variable_scope("transform"):
|
249 |
+
input_tensor = tf.layers.dense(
|
250 |
+
input_tensor,
|
251 |
+
units=bert_config.hidden_size,
|
252 |
+
activation=modeling.get_activation(bert_config.hidden_act),
|
253 |
+
kernel_initializer=modeling.create_initializer(
|
254 |
+
bert_config.initializer_range))
|
255 |
+
input_tensor = modeling.layer_norm(input_tensor)
|
256 |
+
|
257 |
+
# The output weights are the same as the input embeddings, but there is
|
258 |
+
# an output-only bias for each token.
|
259 |
+
output_bias = tf.get_variable(
|
260 |
+
"output_bias",
|
261 |
+
shape=[bert_config.vocab_size],
|
262 |
+
initializer=tf.zeros_initializer())
|
263 |
+
logits = tf.matmul(input_tensor, output_weights, transpose_b=True)
|
264 |
+
logits = tf.nn.bias_add(logits, output_bias)
|
265 |
+
log_probs = tf.nn.log_softmax(logits, axis=-1)
|
266 |
+
|
267 |
+
label_ids = tf.reshape(label_ids, [-1])
|
268 |
+
label_weights = tf.reshape(label_weights, [-1])
|
269 |
+
|
270 |
+
one_hot_labels = tf.one_hot(
|
271 |
+
label_ids, depth=bert_config.vocab_size, dtype=tf.float32)
|
272 |
+
|
273 |
+
# The `positions` tensor might be zero-padded (if the sequence is too
|
274 |
+
# short to have the maximum number of predictions). The `label_weights`
|
275 |
+
# tensor has a value of 1.0 for every real prediction and 0.0 for the
|
276 |
+
# padding predictions.
|
277 |
+
per_example_loss = -tf.reduce_sum(log_probs * one_hot_labels, axis=[-1])
|
278 |
+
numerator = tf.reduce_sum(label_weights * per_example_loss)
|
279 |
+
denominator = tf.reduce_sum(label_weights) + 1e-5
|
280 |
+
loss = numerator / denominator
|
281 |
+
|
282 |
+
return (loss, per_example_loss, log_probs)
|
283 |
+
|
284 |
+
|
285 |
+
def get_next_sentence_output(bert_config, input_tensor, labels):
|
286 |
+
"""Get loss and log probs for the next sentence prediction."""
|
287 |
+
|
288 |
+
# Simple binary classification. Note that 0 is "next sentence" and 1 is
|
289 |
+
# "random sentence". This weight matrix is not used after pre-training.
|
290 |
+
with tf.variable_scope("cls/seq_relationship"):
|
291 |
+
output_weights = tf.get_variable(
|
292 |
+
"output_weights",
|
293 |
+
shape=[2, bert_config.hidden_size],
|
294 |
+
initializer=modeling.create_initializer(bert_config.initializer_range))
|
295 |
+
output_bias = tf.get_variable(
|
296 |
+
"output_bias", shape=[2], initializer=tf.zeros_initializer())
|
297 |
+
|
298 |
+
logits = tf.matmul(input_tensor, output_weights, transpose_b=True)
|
299 |
+
logits = tf.nn.bias_add(logits, output_bias)
|
300 |
+
log_probs = tf.nn.log_softmax(logits, axis=-1)
|
301 |
+
labels = tf.reshape(labels, [-1])
|
302 |
+
one_hot_labels = tf.one_hot(labels, depth=2, dtype=tf.float32)
|
303 |
+
per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)
|
304 |
+
loss = tf.reduce_mean(per_example_loss)
|
305 |
+
return (loss, per_example_loss, log_probs)
|
306 |
+
|
307 |
+
|
308 |
+
def gather_indexes(sequence_tensor, positions):
|
309 |
+
"""Gathers the vectors at the specific positions over a minibatch."""
|
310 |
+
sequence_shape = modeling.get_shape_list(sequence_tensor, expected_rank=3)
|
311 |
+
batch_size = sequence_shape[0]
|
312 |
+
seq_length = sequence_shape[1]
|
313 |
+
width = sequence_shape[2]
|
314 |
+
|
315 |
+
flat_offsets = tf.reshape(
|
316 |
+
tf.range(0, batch_size, dtype=tf.int32) * seq_length, [-1, 1])
|
317 |
+
flat_positions = tf.reshape(positions + flat_offsets, [-1])
|
318 |
+
flat_sequence_tensor = tf.reshape(sequence_tensor,
|
319 |
+
[batch_size * seq_length, width])
|
320 |
+
output_tensor = tf.gather(flat_sequence_tensor, flat_positions)
|
321 |
+
return output_tensor
|
322 |
+
|
323 |
+
|
324 |
+
def input_fn_builder(input_files,
|
325 |
+
max_seq_length,
|
326 |
+
max_predictions_per_seq,
|
327 |
+
is_training,
|
328 |
+
num_cpu_threads=4):
|
329 |
+
"""Creates an `input_fn` closure to be passed to TPUEstimator."""
|
330 |
+
|
331 |
+
def input_fn(params):
|
332 |
+
"""The actual input function."""
|
333 |
+
batch_size = params["batch_size"]
|
334 |
+
|
335 |
+
name_to_features = {
|
336 |
+
"input_ids":
|
337 |
+
tf.FixedLenFeature([max_seq_length], tf.int64),
|
338 |
+
"input_mask":
|
339 |
+
tf.FixedLenFeature([max_seq_length], tf.int64),
|
340 |
+
"segment_ids":
|
341 |
+
tf.FixedLenFeature([max_seq_length], tf.int64),
|
342 |
+
"masked_lm_positions":
|
343 |
+
tf.FixedLenFeature([max_predictions_per_seq], tf.int64),
|
344 |
+
"masked_lm_ids":
|
345 |
+
tf.FixedLenFeature([max_predictions_per_seq], tf.int64),
|
346 |
+
"masked_lm_weights":
|
347 |
+
tf.FixedLenFeature([max_predictions_per_seq], tf.float32),
|
348 |
+
"next_sentence_labels":
|
349 |
+
tf.FixedLenFeature([1], tf.int64),
|
350 |
+
}
|
351 |
+
|
352 |
+
# For training, we want a lot of parallel reading and shuffling.
|
353 |
+
# For eval, we want no shuffling and parallel reading doesn't matter.
|
354 |
+
if is_training:
|
355 |
+
d = tf.data.Dataset.from_tensor_slices(tf.constant(input_files))
|
356 |
+
d = d.repeat()
|
357 |
+
d = d.shuffle(buffer_size=len(input_files))
|
358 |
+
|
359 |
+
# `cycle_length` is the number of parallel files that get read.
|
360 |
+
cycle_length = min(num_cpu_threads, len(input_files))
|
361 |
+
|
362 |
+
# `sloppy` mode means that the interleaving is not exact. This adds
|
363 |
+
# even more randomness to the training pipeline.
|
364 |
+
d = d.apply(
|
365 |
+
tf.contrib.data.parallel_interleave(
|
366 |
+
tf.data.TFRecordDataset,
|
367 |
+
sloppy=is_training,
|
368 |
+
cycle_length=cycle_length))
|
369 |
+
d = d.shuffle(buffer_size=100)
|
370 |
+
else:
|
371 |
+
d = tf.data.TFRecordDataset(input_files)
|
372 |
+
# Since we evaluate for a fixed number of steps we don't want to encounter
|
373 |
+
# out-of-range exceptions.
|
374 |
+
d = d.repeat()
|
375 |
+
|
376 |
+
# We must `drop_remainder` on training because the TPU requires fixed
|
377 |
+
# size dimensions. For eval, we assume we are evaluating on the CPU or GPU
|
378 |
+
# and we *don't* want to drop the remainder, otherwise we wont cover
|
379 |
+
# every sample.
|
380 |
+
d = d.apply(
|
381 |
+
tf.contrib.data.map_and_batch(
|
382 |
+
lambda record: _decode_record(record, name_to_features),
|
383 |
+
batch_size=batch_size,
|
384 |
+
num_parallel_batches=num_cpu_threads,
|
385 |
+
drop_remainder=True))
|
386 |
+
return d
|
387 |
+
|
388 |
+
return input_fn
|
389 |
+
|
390 |
+
|
391 |
+
def _decode_record(record, name_to_features):
|
392 |
+
"""Decodes a record to a TensorFlow example."""
|
393 |
+
example = tf.parse_single_example(record, name_to_features)
|
394 |
+
|
395 |
+
# tf.Example only supports tf.int64, but the TPU only supports tf.int32.
|
396 |
+
# So cast all int64 to int32.
|
397 |
+
for name in list(example.keys()):
|
398 |
+
t = example[name]
|
399 |
+
if t.dtype == tf.int64:
|
400 |
+
t = tf.to_int32(t)
|
401 |
+
example[name] = t
|
402 |
+
|
403 |
+
return example
|
404 |
+
|
405 |
+
|
406 |
+
def main(_):
|
407 |
+
tf.logging.set_verbosity(tf.logging.INFO)
|
408 |
+
|
409 |
+
if not FLAGS.do_train and not FLAGS.do_eval:
|
410 |
+
raise ValueError("At least one of `do_train` or `do_eval` must be True.")
|
411 |
+
|
412 |
+
bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file)
|
413 |
+
|
414 |
+
tf.gfile.MakeDirs(FLAGS.output_dir)
|
415 |
+
|
416 |
+
input_files = []
|
417 |
+
for input_pattern in FLAGS.input_file.split(","):
|
418 |
+
input_files.extend(tf.gfile.Glob(input_pattern))
|
419 |
+
|
420 |
+
tf.logging.info("*** Input Files ***")
|
421 |
+
for input_file in input_files:
|
422 |
+
tf.logging.info(" %s" % input_file)
|
423 |
+
|
424 |
+
tpu_cluster_resolver = None
|
425 |
+
if FLAGS.use_tpu and FLAGS.tpu_name:
|
426 |
+
tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
|
427 |
+
FLAGS.tpu_name, zone=FLAGS.tpu_zone, project=FLAGS.gcp_project)
|
428 |
+
|
429 |
+
is_per_host = tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2
|
430 |
+
run_config = tf.contrib.tpu.RunConfig(
|
431 |
+
cluster=tpu_cluster_resolver,
|
432 |
+
master=FLAGS.master,
|
433 |
+
model_dir=FLAGS.output_dir,
|
434 |
+
save_checkpoints_steps=FLAGS.save_checkpoints_steps,
|
435 |
+
tpu_config=tf.contrib.tpu.TPUConfig(
|
436 |
+
iterations_per_loop=FLAGS.iterations_per_loop,
|
437 |
+
num_shards=FLAGS.num_tpu_cores,
|
438 |
+
per_host_input_for_training=is_per_host))
|
439 |
+
|
440 |
+
model_fn = model_fn_builder(
|
441 |
+
bert_config=bert_config,
|
442 |
+
init_checkpoint=FLAGS.init_checkpoint,
|
443 |
+
learning_rate=FLAGS.learning_rate,
|
444 |
+
num_train_steps=FLAGS.num_train_steps,
|
445 |
+
num_warmup_steps=FLAGS.num_warmup_steps,
|
446 |
+
use_tpu=FLAGS.use_tpu,
|
447 |
+
use_one_hot_embeddings=FLAGS.use_tpu)
|
448 |
+
|
449 |
+
# If TPU is not available, this will fall back to normal Estimator on CPU
|
450 |
+
# or GPU.
|
451 |
+
estimator = tf.contrib.tpu.TPUEstimator(
|
452 |
+
use_tpu=FLAGS.use_tpu,
|
453 |
+
model_fn=model_fn,
|
454 |
+
config=run_config,
|
455 |
+
train_batch_size=FLAGS.train_batch_size,
|
456 |
+
eval_batch_size=FLAGS.eval_batch_size)
|
457 |
+
|
458 |
+
if FLAGS.do_train:
|
459 |
+
tf.logging.info("***** Running training *****")
|
460 |
+
tf.logging.info(" Batch size = %d", FLAGS.train_batch_size)
|
461 |
+
train_input_fn = input_fn_builder(
|
462 |
+
input_files=input_files,
|
463 |
+
max_seq_length=FLAGS.max_seq_length,
|
464 |
+
max_predictions_per_seq=FLAGS.max_predictions_per_seq,
|
465 |
+
is_training=True)
|
466 |
+
estimator.train(input_fn=train_input_fn, max_steps=FLAGS.num_train_steps)
|
467 |
+
|
468 |
+
if FLAGS.do_eval:
|
469 |
+
tf.logging.info("***** Running evaluation *****")
|
470 |
+
tf.logging.info(" Batch size = %d", FLAGS.eval_batch_size)
|
471 |
+
|
472 |
+
eval_input_fn = input_fn_builder(
|
473 |
+
input_files=input_files,
|
474 |
+
max_seq_length=FLAGS.max_seq_length,
|
475 |
+
max_predictions_per_seq=FLAGS.max_predictions_per_seq,
|
476 |
+
is_training=False)
|
477 |
+
|
478 |
+
result = estimator.evaluate(
|
479 |
+
input_fn=eval_input_fn, steps=FLAGS.max_eval_steps)
|
480 |
+
|
481 |
+
output_eval_file = os.path.join(FLAGS.output_dir, "eval_results.txt")
|
482 |
+
with tf.gfile.GFile(output_eval_file, "w") as writer:
|
483 |
+
tf.logging.info("***** Eval results *****")
|
484 |
+
for key in sorted(result.keys()):
|
485 |
+
tf.logging.info(" %s = %s", key, str(result[key]))
|
486 |
+
writer.write("%s = %s\n" % (key, str(result[key])))
|
487 |
+
|
488 |
+
|
489 |
+
if __name__ == "__main__":
|
490 |
+
flags.mark_flag_as_required("input_file")
|
491 |
+
flags.mark_flag_as_required("bert_config_file")
|
492 |
+
flags.mark_flag_as_required("output_dir")
|
493 |
+
tf.app.run()
|
bert/tokenization.py
ADDED
@@ -0,0 +1,393 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""Tokenization classes."""
|
16 |
+
|
17 |
+
from __future__ import absolute_import
|
18 |
+
from __future__ import division
|
19 |
+
from __future__ import print_function
|
20 |
+
|
21 |
+
import collections
|
22 |
+
import unicodedata
|
23 |
+
import six
|
24 |
+
import tensorflow as tf
|
25 |
+
|
26 |
+
|
27 |
+
def convert_to_unicode(text):
|
28 |
+
"""Converts `text` to Unicode (if it's not already), assuming utf-8 input."""
|
29 |
+
if six.PY3:
|
30 |
+
if isinstance(text, str):
|
31 |
+
return text
|
32 |
+
elif isinstance(text, bytes):
|
33 |
+
return text.decode("utf-8", "ignore")
|
34 |
+
else:
|
35 |
+
raise ValueError("Unsupported string type: %s" % (type(text)))
|
36 |
+
elif six.PY2:
|
37 |
+
if isinstance(text, str):
|
38 |
+
return text.decode("utf-8", "ignore")
|
39 |
+
elif isinstance(text, unicode):
|
40 |
+
return text
|
41 |
+
else:
|
42 |
+
raise ValueError("Unsupported string type: %s" % (type(text)))
|
43 |
+
else:
|
44 |
+
raise ValueError("Not running on Python2 or Python 3?")
|
45 |
+
|
46 |
+
|
47 |
+
def printable_text(text):
|
48 |
+
"""Returns text encoded in a way suitable for print or `tf.logging`."""
|
49 |
+
|
50 |
+
# These functions want `str` for both Python2 and Python3, but in one case
|
51 |
+
# it's a Unicode string and in the other it's a byte string.
|
52 |
+
if six.PY3:
|
53 |
+
if isinstance(text, str):
|
54 |
+
return text
|
55 |
+
elif isinstance(text, bytes):
|
56 |
+
return text.decode("utf-8", "ignore")
|
57 |
+
else:
|
58 |
+
raise ValueError("Unsupported string type: %s" % (type(text)))
|
59 |
+
elif six.PY2:
|
60 |
+
if isinstance(text, str):
|
61 |
+
return text
|
62 |
+
elif isinstance(text, unicode):
|
63 |
+
return text.encode("utf-8")
|
64 |
+
else:
|
65 |
+
raise ValueError("Unsupported string type: %s" % (type(text)))
|
66 |
+
else:
|
67 |
+
raise ValueError("Not running on Python2 or Python 3?")
|
68 |
+
|
69 |
+
|
70 |
+
def load_vocab(vocab_file):
|
71 |
+
"""Loads a vocabulary file into a dictionary."""
|
72 |
+
vocab = collections.OrderedDict()
|
73 |
+
index = 0
|
74 |
+
with tf.gfile.GFile(vocab_file, "r") as reader:
|
75 |
+
while True:
|
76 |
+
token = reader.readline()
|
77 |
+
if token.split(): token = token.split()[0] # to support SentencePiece vocab file
|
78 |
+
token = convert_to_unicode(token)
|
79 |
+
if not token:
|
80 |
+
break
|
81 |
+
token = token.strip()
|
82 |
+
vocab[token] = index
|
83 |
+
index += 1
|
84 |
+
return vocab
|
85 |
+
|
86 |
+
|
87 |
+
def convert_by_vocab(vocab, items):
|
88 |
+
"""Converts a sequence of [tokens|ids] using the vocab."""
|
89 |
+
output = []
|
90 |
+
for item in items:
|
91 |
+
output.append(vocab[item])
|
92 |
+
return output
|
93 |
+
|
94 |
+
|
95 |
+
def convert_tokens_to_ids(vocab, tokens):
|
96 |
+
return convert_by_vocab(vocab, tokens)
|
97 |
+
|
98 |
+
|
99 |
+
def convert_ids_to_tokens(inv_vocab, ids):
|
100 |
+
return convert_by_vocab(inv_vocab, ids)
|
101 |
+
|
102 |
+
|
103 |
+
def whitespace_tokenize(text):
|
104 |
+
"""Runs basic whitespace cleaning and splitting on a peice of text."""
|
105 |
+
text = text.strip()
|
106 |
+
if not text:
|
107 |
+
return []
|
108 |
+
tokens = text.split()
|
109 |
+
return tokens
|
110 |
+
|
111 |
+
|
112 |
+
class FullTokenizer(object):
|
113 |
+
"""Runs end-to-end tokenziation."""
|
114 |
+
|
115 |
+
def __init__(self, vocab_file, do_lower_case=True):
|
116 |
+
self.vocab = load_vocab(vocab_file)
|
117 |
+
self.inv_vocab = {v: k for k, v in self.vocab.items()}
|
118 |
+
self.basic_tokenizer = BasicTokenizer(do_lower_case=do_lower_case)
|
119 |
+
self.wordpiece_tokenizer = WordpieceTokenizer(vocab=self.vocab)
|
120 |
+
|
121 |
+
def tokenize(self, text):
|
122 |
+
split_tokens = []
|
123 |
+
for token in self.basic_tokenizer.tokenize(text):
|
124 |
+
for sub_token in self.wordpiece_tokenizer.tokenize(token):
|
125 |
+
split_tokens.append(sub_token)
|
126 |
+
|
127 |
+
return split_tokens
|
128 |
+
|
129 |
+
def convert_tokens_to_ids(self, tokens):
|
130 |
+
return convert_by_vocab(self.vocab, tokens)
|
131 |
+
|
132 |
+
def convert_ids_to_tokens(self, ids):
|
133 |
+
return convert_by_vocab(self.inv_vocab, ids)
|
134 |
+
|
135 |
+
|
136 |
+
from bpe_helper import BPE
|
137 |
+
import sentencepiece as spm
|
138 |
+
|
139 |
+
class ThaiTokenizer(object):
|
140 |
+
"""Tokenizes Thai texts."""
|
141 |
+
|
142 |
+
def __init__(self, vocab_file, spm_file):
|
143 |
+
self.vocab = load_vocab(vocab_file)
|
144 |
+
self.inv_vocab = {v: k for k, v in self.vocab.items()}
|
145 |
+
|
146 |
+
self.bpe = BPE(vocab_file)
|
147 |
+
self.s = spm.SentencePieceProcessor()
|
148 |
+
self.s.Load(spm_file)
|
149 |
+
|
150 |
+
def tokenize(self, text):
|
151 |
+
bpe_tokens = self.bpe.encode(text).split(' ')
|
152 |
+
spm_tokens = self.s.EncodeAsPieces(text)
|
153 |
+
|
154 |
+
tokens = bpe_tokens if len(bpe_tokens) < len(spm_tokens) else spm_tokens
|
155 |
+
|
156 |
+
split_tokens = []
|
157 |
+
|
158 |
+
for token in tokens:
|
159 |
+
new_token = token
|
160 |
+
|
161 |
+
if token.startswith('_') and not token in self.vocab:
|
162 |
+
split_tokens.append('_')
|
163 |
+
new_token = token[1:]
|
164 |
+
|
165 |
+
if not new_token in self.vocab:
|
166 |
+
split_tokens.append('<unk>')
|
167 |
+
else:
|
168 |
+
split_tokens.append(new_token)
|
169 |
+
|
170 |
+
return split_tokens
|
171 |
+
|
172 |
+
def convert_tokens_to_ids(self, tokens):
|
173 |
+
return convert_by_vocab(self.vocab, tokens)
|
174 |
+
|
175 |
+
def convert_ids_to_tokens(self, ids):
|
176 |
+
return convert_by_vocab(self.inv_vocab, ids)
|
177 |
+
|
178 |
+
|
179 |
+
class BasicTokenizer(object):
|
180 |
+
"""Runs basic tokenization (punctuation splitting, lower casing, etc.)."""
|
181 |
+
|
182 |
+
def __init__(self, do_lower_case=True):
|
183 |
+
"""Constructs a BasicTokenizer.
|
184 |
+
|
185 |
+
Args:
|
186 |
+
do_lower_case: Whether to lower case the input.
|
187 |
+
"""
|
188 |
+
self.do_lower_case = do_lower_case
|
189 |
+
|
190 |
+
def tokenize(self, text):
|
191 |
+
"""Tokenizes a piece of text."""
|
192 |
+
text = convert_to_unicode(text)
|
193 |
+
text = self._clean_text(text)
|
194 |
+
|
195 |
+
# This was added on November 1st, 2018 for the multilingual and Chinese
|
196 |
+
# models. This is also applied to the English models now, but it doesn't
|
197 |
+
# matter since the English models were not trained on any Chinese data
|
198 |
+
# and generally don't have any Chinese data in them (there are Chinese
|
199 |
+
# characters in the vocabulary because Wikipedia does have some Chinese
|
200 |
+
# words in the English Wikipedia.).
|
201 |
+
text = self._tokenize_chinese_chars(text)
|
202 |
+
|
203 |
+
orig_tokens = whitespace_tokenize(text)
|
204 |
+
split_tokens = []
|
205 |
+
for token in orig_tokens:
|
206 |
+
if self.do_lower_case:
|
207 |
+
token = token.lower()
|
208 |
+
token = self._run_strip_accents(token)
|
209 |
+
split_tokens.extend(self._run_split_on_punc(token))
|
210 |
+
|
211 |
+
output_tokens = whitespace_tokenize(" ".join(split_tokens))
|
212 |
+
return output_tokens
|
213 |
+
|
214 |
+
def _run_strip_accents(self, text):
|
215 |
+
"""Strips accents from a piece of text."""
|
216 |
+
text = unicodedata.normalize("NFD", text)
|
217 |
+
output = []
|
218 |
+
for char in text:
|
219 |
+
cat = unicodedata.category(char)
|
220 |
+
if cat == "Mn":
|
221 |
+
continue
|
222 |
+
output.append(char)
|
223 |
+
return "".join(output)
|
224 |
+
|
225 |
+
def _run_split_on_punc(self, text):
|
226 |
+
"""Splits punctuation on a piece of text."""
|
227 |
+
chars = list(text)
|
228 |
+
i = 0
|
229 |
+
start_new_word = True
|
230 |
+
output = []
|
231 |
+
while i < len(chars):
|
232 |
+
char = chars[i]
|
233 |
+
if _is_punctuation(char):
|
234 |
+
output.append([char])
|
235 |
+
start_new_word = True
|
236 |
+
else:
|
237 |
+
if start_new_word:
|
238 |
+
output.append([])
|
239 |
+
start_new_word = False
|
240 |
+
output[-1].append(char)
|
241 |
+
i += 1
|
242 |
+
|
243 |
+
return ["".join(x) for x in output]
|
244 |
+
|
245 |
+
def _tokenize_chinese_chars(self, text):
|
246 |
+
"""Adds whitespace around any CJK character."""
|
247 |
+
output = []
|
248 |
+
for char in text:
|
249 |
+
cp = ord(char)
|
250 |
+
if self._is_chinese_char(cp):
|
251 |
+
output.append(" ")
|
252 |
+
output.append(char)
|
253 |
+
output.append(" ")
|
254 |
+
else:
|
255 |
+
output.append(char)
|
256 |
+
return "".join(output)
|
257 |
+
|
258 |
+
def _is_chinese_char(self, cp):
|
259 |
+
"""Checks whether CP is the codepoint of a CJK character."""
|
260 |
+
# This defines a "chinese character" as anything in the CJK Unicode block:
|
261 |
+
# https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
|
262 |
+
#
|
263 |
+
# Note that the CJK Unicode block is NOT all Japanese and Korean characters,
|
264 |
+
# despite its name. The modern Korean Hangul alphabet is a different block,
|
265 |
+
# as is Japanese Hiragana and Katakana. Those alphabets are used to write
|
266 |
+
# space-separated words, so they are not treated specially and handled
|
267 |
+
# like the all of the other languages.
|
268 |
+
if ((cp >= 0x4E00 and cp <= 0x9FFF) or #
|
269 |
+
(cp >= 0x3400 and cp <= 0x4DBF) or #
|
270 |
+
(cp >= 0x20000 and cp <= 0x2A6DF) or #
|
271 |
+
(cp >= 0x2A700 and cp <= 0x2B73F) or #
|
272 |
+
(cp >= 0x2B740 and cp <= 0x2B81F) or #
|
273 |
+
(cp >= 0x2B820 and cp <= 0x2CEAF) or
|
274 |
+
(cp >= 0xF900 and cp <= 0xFAFF) or #
|
275 |
+
(cp >= 0x2F800 and cp <= 0x2FA1F)): #
|
276 |
+
return True
|
277 |
+
|
278 |
+
return False
|
279 |
+
|
280 |
+
def _clean_text(self, text):
|
281 |
+
"""Performs invalid character removal and whitespace cleanup on text."""
|
282 |
+
output = []
|
283 |
+
for char in text:
|
284 |
+
cp = ord(char)
|
285 |
+
if cp == 0 or cp == 0xfffd or _is_control(char):
|
286 |
+
continue
|
287 |
+
if _is_whitespace(char):
|
288 |
+
output.append(" ")
|
289 |
+
else:
|
290 |
+
output.append(char)
|
291 |
+
return "".join(output)
|
292 |
+
|
293 |
+
|
294 |
+
class WordpieceTokenizer(object):
|
295 |
+
"""Runs WordPiece tokenziation."""
|
296 |
+
|
297 |
+
def __init__(self, vocab, unk_token="[UNK]", max_input_chars_per_word=100):
|
298 |
+
self.vocab = vocab
|
299 |
+
self.unk_token = unk_token
|
300 |
+
self.max_input_chars_per_word = max_input_chars_per_word
|
301 |
+
|
302 |
+
def tokenize(self, text):
|
303 |
+
"""Tokenizes a piece of text into its word pieces.
|
304 |
+
|
305 |
+
This uses a greedy longest-match-first algorithm to perform tokenization
|
306 |
+
using the given vocabulary.
|
307 |
+
|
308 |
+
For example:
|
309 |
+
input = "unaffable"
|
310 |
+
output = ["un", "##aff", "##able"]
|
311 |
+
|
312 |
+
Args:
|
313 |
+
text: A single token or whitespace separated tokens. This should have
|
314 |
+
already been passed through `BasicTokenizer.
|
315 |
+
|
316 |
+
Returns:
|
317 |
+
A list of wordpiece tokens.
|
318 |
+
"""
|
319 |
+
|
320 |
+
text = convert_to_unicode(text)
|
321 |
+
|
322 |
+
output_tokens = []
|
323 |
+
for token in whitespace_tokenize(text):
|
324 |
+
chars = list(token)
|
325 |
+
if len(chars) > self.max_input_chars_per_word:
|
326 |
+
output_tokens.append(self.unk_token)
|
327 |
+
continue
|
328 |
+
|
329 |
+
is_bad = False
|
330 |
+
start = 0
|
331 |
+
sub_tokens = []
|
332 |
+
while start < len(chars):
|
333 |
+
end = len(chars)
|
334 |
+
cur_substr = None
|
335 |
+
while start < end:
|
336 |
+
substr = "".join(chars[start:end])
|
337 |
+
if start > 0:
|
338 |
+
substr = "##" + substr
|
339 |
+
if substr in self.vocab:
|
340 |
+
cur_substr = substr
|
341 |
+
break
|
342 |
+
end -= 1
|
343 |
+
if cur_substr is None:
|
344 |
+
is_bad = True
|
345 |
+
break
|
346 |
+
sub_tokens.append(cur_substr)
|
347 |
+
start = end
|
348 |
+
|
349 |
+
if is_bad:
|
350 |
+
output_tokens.append(self.unk_token)
|
351 |
+
else:
|
352 |
+
output_tokens.extend(sub_tokens)
|
353 |
+
return output_tokens
|
354 |
+
|
355 |
+
|
356 |
+
def _is_whitespace(char):
|
357 |
+
"""Checks whether `chars` is a whitespace character."""
|
358 |
+
# \t, \n, and \r are technically contorl characters but we treat them
|
359 |
+
# as whitespace since they are generally considered as such.
|
360 |
+
if char == " " or char == "\t" or char == "\n" or char == "\r":
|
361 |
+
return True
|
362 |
+
cat = unicodedata.category(char)
|
363 |
+
if cat == "Zs":
|
364 |
+
return True
|
365 |
+
return False
|
366 |
+
|
367 |
+
|
368 |
+
def _is_control(char):
|
369 |
+
"""Checks whether `chars` is a control character."""
|
370 |
+
# These are technically control characters but we count them as whitespace
|
371 |
+
# characters.
|
372 |
+
if char == "\t" or char == "\n" or char == "\r":
|
373 |
+
return False
|
374 |
+
cat = unicodedata.category(char)
|
375 |
+
if cat.startswith("C"):
|
376 |
+
return True
|
377 |
+
return False
|
378 |
+
|
379 |
+
|
380 |
+
def _is_punctuation(char):
|
381 |
+
"""Checks whether `chars` is a punctuation character."""
|
382 |
+
cp = ord(char)
|
383 |
+
# We treat all non-letter/number ASCII as punctuation.
|
384 |
+
# Characters such as "^", "$", and "`" are not in the Unicode
|
385 |
+
# Punctuation class but we treat them as punctuation anyways, for
|
386 |
+
# consistency.
|
387 |
+
if ((cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or
|
388 |
+
(cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126)):
|
389 |
+
return True
|
390 |
+
cat = unicodedata.category(char)
|
391 |
+
if cat.startswith("P"):
|
392 |
+
return True
|
393 |
+
return False
|