事前学習済みのBERTから、文のベクトルを抽出します。
今回は、Googleの多言語モデル(multilingual)を利用します。
まずはmultilingual model(cased)をダウンロード。
1 |
wget "https://storage.googleapis.com/bert_models/2018_11_23/multi_cased_L-12_H-768_A-12.zip" |
解凍
1 |
unzip multi_cased_L-12_H-768_A-12.zip |
この後必要なbert_configや学習済みモデル、語彙ファイルなどが入っています。
1 2 3 4 5 6 7 |
ls -l multi_cased_L-12_H-768_A-12 # total 1410608 # -rw-r--r-- 1 user staff 521 11 24 2018 bert_config.json # -rw-r--r-- 1 user staff 714266612 11 24 2018 bert_model.ckpt.data-00000-of-00001 # -rw-r--r-- 1 user staff 8631 11 24 2018 bert_model.ckpt.index # -rw-r--r-- 1 user staff 908833 11 24 2018 bert_model.ckpt.meta # -rw-r--r-- 1 user staff 995526 11 24 2018 vocab.txt |
gitからプログラム類をダウンロード
1 |
git clone https://github.com/google-research/bert.git |
ベクトルの取得には、extract_features.pyを利用します。
ベクトルを取得するテキストファイルを作成。複数文ある場合は|||で区切ります。
1 |
echo 'Who was Jim Henson ? ||| Jim Henson was a puppeteer' > /tmp/input.txt |
BERTベクトルの取得。output_fileに指定したPATHにベクトルが出力されます。
1 2 3 4 5 6 7 8 9 10 11 12 |
cd ~/bert BERT_BASE_DIR=~/multi_cased_L-12_H-768_A-12 python extract_features.py \ --input_file=/tmp/input.txt \ --output_file=/tmp/output.jsonl \ --vocab_file=$BERT_BASE_DIR/vocab.txt \ --bert_config_file=$BERT_BASE_DIR/bert_config.json \ --init_checkpoint=$BERT_BASE_DIR/bert_model.ckpt \ --layers=-1,-2,-3,-4 \ --max_seq_length=128 \ --batch_size=8 |
確認
1 |
less /tmp/output.jsonl |
以上です。