Top / 自然言語処理 / Word2vec

Word2vec とは、単語をベクトル化する技術で、各単語を何百次元にもなるベクトルで表現し、それらの内積をとることで単語が似通ってるなどを判定する技術のようです。

これによって

king  -  man + woman = queen 

などを実現する事が出来るようです。すばらしい。

ダウンロードとインストール

$ git clone https://github.com/svn2github/word2vec.git
$ cd word2vec/
$ make
gcc word2vec.c -o word2vec -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-result
gcc word2phrase.c -o word2phrase -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-result
gcc distance.c -o distance -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-result
distance.c: In function ‘main’:
distance.c:31:8: warning: unused variable ‘ch’ [-Wunused-variable]
   char ch;
        ^
gcc word-analogy.c -o word-analogy -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-result
word-analogy.c: In function ‘main’:
word-analogy.c:31:8: warning: unused variable ‘ch’ [-Wunused-variable]
   char ch;
        ^
gcc compute-accuracy.c -o compute-accuracy -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-result
compute-accuracy.c: In function ‘main’:
compute-accuracy.c:29:109: warning: unused variable ‘ch’ [-Wunused-variable]
   char st1[max_size], st2[max_size], st3[max_size], st4[max_size], bestw[N][max_size], file_name[max_size], ch;
                                                                                                             ^
chmod +x *.sh

デモ

$ cat demo-word.sh
make
if [ ! -e text8 ]; then
  wget http://mattmahoney.net/dc/text8.zip -O text8.gz
  gzip -d text8.gz -f
fi
time ./word2vec -train text8 -output vectors.bin -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 20 -binary 1 -iter 15
./distance vectors.bin

手動で順次やってみます。

学習データをダウンロード

$ wget http://mattmahoney.net/dc/text8.zip -O text8.gz
text8.gz                                               100%[============>]  29.89M   377KB/s    時間 82s
$ gzip -d text8.gz -f

学習処理。

$ time ./word2vec -train text8 -output vectors.bin -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 20 -binary 1 -iter 15
Starting training using file text8
Vocab size: 71291
Words in train file: 16718843
Alpha: 0.000005  Progress: 100.10%  Words/thread/sec: 146.19k
real	14m24.749s
user	28m38.580s
sys	0m2.920s

学習出来たようです

ベクトル的に近しい文字を探す

$ ./distance vectors.bin
Enter word or sentence (EXIT to break): tokyo

Word: tokyo  Position in vocabulary: 4915

                                              Word       Cosine distance
------------------------------------------------------------------------
                                             osaka		0.715582
                                            nagoya		0.687835
                                             chiba		0.646952
                                          yokohama		0.640354
                                             kanto		0.637495
                                              kobe		0.633834
                                           niigata		0.610898
                                           ...
Enter word or sentence (EXIT to break): bouldering

Word: bouldering  Position in vocabulary: 20827

                                              Word       Cosine distance
------------------------------------------------------------------------
                                          climbing		0.559630
                                    mountaineering		0.551720
                                       paragliding		0.476230
                                            diving		0.475066
                                          climbers		0.466647
                                           ....
Enter word or sentence (EXIT to break):EXIT

なるほど。。

ベクトル演算

ベクトルの加減を行ってみます。

$ ./word-analogy vectors.bin
Enter three words (EXIT to break): man king woman

Word: man  Position in vocabulary: 243

Word: king  Position in vocabulary: 187

Word: woman  Position in vocabulary: 1013

                                              Word              Distance
------------------------------------------------------------------------
                                             queen		0.580811
                                          daughter		0.483520
                                           heiress		0.477779
                                          burgundy		0.472203
                                               vii		0.471096
                                           marries		0.470184
                                         ahasuerus		0.469802
                                           infanta		0.469698
                                             anjou		0.464855

Queenが出力されましたね。

今回、Word2vecで、英語の単語のベクトル化を行いましたが、辞書ファイル(text8)を、Mecabで分かち書きした日本語を渡す事で、日本語を使用する事も出来るようです。

関連リンク


この記事は

選択肢 投票
おもしろかった 0  
そうでもない 0  

Top / 自然言語処理 / Word2vec

現在のアクセス:3663


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS