- 追加された行はこの色です。
- 削除された行はこの色です。
#topicpath
----
#contents
Word2vec とは、単語をベクトル化する技術で、各単語を何百次元にもなるベクトルで表現し、それらの内積をとることで単語が似通ってるなどを判定する技術のようです。
これによって
king - man + woman = queen
などを実現する事が出来るようです。すばらしい。
**ダウンロードとインストール [#r93d71f6]
$ 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
**デモ [#jf725258]
$ 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
手動で順次やってみます。
*** 学習データをダウンロード [#ofb8b9c5]
$ wget http://mattmahoney.net/dc/text8.zip -O text8.gz
text8.gz 100%[============>] 29.89M 377KB/s 時間 82s
$ gzip -d text8.gz -f
*** 学習処理。 [#n45b94e3]
$ 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
学習出来たようです
*** ベクトル的に近しい文字を探す [#taaa3cd8]
$ ./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
なるほど。。
*** ベクトル演算[#r151e593]
ベクトルの加減を行ってみます。
$ ./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のデータを学習させる [#y2a861e8]
やってみます。
*** まず、Rubyなどの準備 [#be33bf26]
$ 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データのダウンロードと変換 [#wdb47da5]
日本語データは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
*** 学習 [#xe270072]
さっき英語で学習処理をしましたが、今度はこの学習データを使って学習してみます。
$ 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
日本語の学習は以上です。
*** 動かしてみる。 [#n8b66f14]
英語でやったときのようにやってみます。
$ ./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
うん、ヨシとしよう!。
**関連リンク [#rdc91f50]
-[[word2vecを使って、日本語wikipediaのデータを学習する - Qiita>https://qiita.com/tsuruchan/items/7d3af5c5e9182230db4e]]
-[[Word2Vec:発明した本人も驚く単語ベクトルの驚異的な力 - DeepAge>https://deepage.net/bigdata/machine_learning/2016/09/02/word2vec_power_of_word_vector.html]]
-[[[機械学習] Word2VecをMacで使ってみる - YoheiM .NET>https://www.yoheim.net/blog.php?q=20160305]]
-[[Ubuntu + word2vecで日本語版wikipediaを自然言語処理してみた | from umentu import stupid>https://www.blog.umentu.work/ubuntu-word2vec%E3%81%A7%E6%97%A5%E6%9C%AC%E8%AA%9E%E7%89%88wikipedia%E3%82%92%E8%87%AA%E7%84%B6%E8%A8%80%E8%AA%9E%E5%87%A6%E7%90%86%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F/]]
-[[OS Xでword2vecを試してみた - amberfrog.log>http://b.amberfrog.net/post/105527194822/os-x%E3%81%A7word2vec%E3%82%92%E8%A9%A6%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F]]
-[[挑戦! word2vecで自然言語処理(Keras+TensorFlow使用) - Deep Insider>https://deepinsider.jp/issue/deeplearningnext/word2vec]]
-[[単純な単語のベクトル表現: word2vec, GloVe - Qiita>https://qiita.com/yuku_t/items/483b56be83a3a5423b09]]
-[[Word2Vec, MeCab, ComeJisyo で病気の症状類似語を出してみた - Qiita>https://qiita.com/quvo/items/9ef250d58971eadf6e1a]]
----
この記事は
#vote(おもしろかった,そうでもない)
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}