|
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で分かち書きした日本語を渡す事で、日本語を使用する事も出来るようです。 日本語Wikipediaのデータを学習させる †やってみます。 まず、Rubyなどの準備 †$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv $ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build $ vi ~/.bashrc export PATH="$HOME/.rbenv/bin:$PATH" を追加して $ source ~/.bashrc 途中エラーが出ましたが、よく見ると Try running `apt-get install -y libssl-dev libreadline-dev zlib1g-dev` to fetch missing dependencies. などが書いてあるのでいわれたとおり対処。 $ rbenv install 2.5.0 Downloading ruby-2.5.0.tar.bz2... -> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.bz2 Installing ruby-2.5.0... Installed ruby-2.5.0 to /home/ubuntu/.rbenv/versions/2.5.0 $ rbenv local 2.5.0 $ rbenv global 2.5.0 $ rbenv exec gem install wp2txt bundler Ruby関連の準備は完了 日本語Wikipediaデータのダウンロードと変換 †日本語データはWikipediaのデータを使う事にします。 $ mkdir wikipedia && cd $_ $ curl https://dumps.wikimedia.org/jawiki/latest/jawiki-latest-pages-articles.xml.bz2 \ -o jawiki-latest-pages-articles.xml.bz2 2G以上あるのでご注意。 ダウンロードしたデータをただのテキストファイルに変換します。 $ rbenv exec wp2txt --input-file jawiki-latest-pages-articles.xml.bz2 WP2TXT is spawming 2 threads to process data Preparing ... This may take several minutes or more ca... Done. jawiki-latest: 8% |oooooooooooooo … Processing finished $ たくさんのファイルが出来るので、ファイルをMecabで分かち書きしながら、連結します $ cat jawiki-latest-pages-articles.xml*.txt | \ mecab -Owakati -b 81920 > jawiki-latest-pages-articles-all_wakati.txt 学習 †さっき英語で学習処理をしましたが、今度はこの学習データを使って学習してみます。 $ cd ../word2vec/ $ ./word2vec -train ../wikipedia/jawiki-latest-pages-articles-all_wakati.txt \ -output jawiki_wakati.bin -size 200 -window 5 -sample 1e-3 -negative 5 -hs 0 -binary 1 日本語の学習は以上です。 動かしてみる。 †英語でやったときのようにやってみます。 $ ./distance jawiki2.bin
Enter word or sentence (EXIT to break): 東京
Word: 東京 Position in vocabulary: 323
Word Cosine distance
------------------------------------------------------------------------
大阪 0.792531
名古屋 0.723404
横浜 0.711129
札幌 0.674038
京都 0.660005
関西 0.659068
神奈川 0.655541
仙台 0.654896
...
Enter word or sentence (EXIT to break): ボルダリング
Word: ボルダリング Position in vocabulary: 106315
Word Cosine distance
------------------------------------------------------------------------
クライミング 0.741020
フリークライミング 0.737473
スケートボード 0.644138
ロッククライミング 0.643847
IFSC 0.639246
スノーボード 0.629231
ペタンク 0.614713
ウェイクボード 0.614535
ビリヤード 0.607626
ノルディックウォーキング 0.605544
$ ./word-analogy jawiki2.bin
Enter three words (EXIT to break): 男 王様 女
Word: 男 Position in vocabulary: 514
Word: 王様 Position in vocabulary: 13866
Word: 女 Position in vocabulary: 534
Word Distance
------------------------------------------------------------------------
お姫様 0.682706
お姫さま 0.616227
王さま 0.612430
花嫁 0.588165
わたし 0.586670
シンデレラ 0.583221
パパ 0.581027
魔女 0.575280
うーん、女王 でなくてお姫さまにorz。。 Enter three words (EXIT to break): 男性 王様 女性
Word: 男性 Position in vocabulary: 873
Word: 王様 Position in vocabulary: 13866
Word: 女性 Position in vocabulary: 368
Word Distance
------------------------------------------------------------------------
お姫様 0.696252
王さま 0.634749
神様 0.630194
お姫さま 0.614321
女王 0.603935
パパ 0.603559
魔女 0.599964
妖精 0.594210
うん、ヨシとしよう!。 関連リンク †
この記事は 現在のアクセス:4377 |