by kwonmha

Builds wordpiece(subword) vocabulary compatible for Google Research's BERT

153 Stars 36 Forks Last release: Not found 30 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Vocabulary builder for BERT

Modified, simplified version of and its dependencies included in tensor2tensor library, making its output fits to google research's open-sourced BERT project.

Although google opened pre-trained BERT and training scripts, they didn't open source to generate wordpiece(subword) vocabulary matches to

in opened model.
And the libraries they suggested to use were not compatible with their
of BERT as they mentioned.
So I modified of tensor2tensor library that is one of the suggestions google mentioned to generate wordpiece vocabulary.


  • Original SubwordTextEncoder adds \"_\" at the end of subwords appear on the first position of words. So I changed to add \"_\" at the beginning of subwords that follow other subwords, using myescape_token() function, and later substitued \"_\" with "##"

  • Generated vocabulary contains all characters and all characters having "##" in front of them. For example,

  • Made standard special characters like

    [email protected]~
    and special tokens used for BERT, ex :
    [SEP], [CLS], [MASK], [UNK]
    to be added.
  • Removed irrelevant classes in, commented unused functions some of which seem to exist for decoding, and removed mlperflog module to make this project independent to tensor2tensor library.


The environment I made this project in consists of : - python3.6 - tensorflow 1.11

Basic usage

python \
--corpus_filepattern "{corpus_for_vocab}" \
--output_filename {name_of_vocab}
--min_count {minimum_subtoken_counts}

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.