スクリーンショット_2019-08-27_12

word2vecをmacにインストールして、愛って何か、聞いてみた。

結構古い情報が多く、意外とインストールするだけで一苦労だったword2vec。特にMacにインストールしようとして、苦労したという人が多いようなので、最新(2019年8月27日)に試して成功したプロセスを書いておきます。

(1)まずはword2vecのcloneをしよう。

ターミナルを立ち上げ、word2vecのフォルダを保存したい場所まで行き、以下のコードを入力します。

git clone https://github.com/svn2github/word2vec.git

そうすると、以下のような出力結果になり、成功します。

Cloning into 'word2vec'...
remote: Enumerating objects: 136, done.
remote: Total 136 (delta 0), reused 0 (delta 0), pack-reused 136
Receiving objects: 100% (136/136), 115.80 KiB | 224.00 KiB/s, done.
Resolving deltas: 100% (106/106), done.

ちなみに公式のhttp://word2vec.googlecode.com/svn/trunk/を叩くと失敗することに注意。

(2)まずはword2vecに移動します。

$cd word2vec/ 

まずは上のような感じで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:18:10: fatal error: 'malloc.h' file not found
#include <malloc.h>
        ^~~~~~~~~~
1 error generated.

Mac版だとmalloc.hがstdlib.hになっているらしいんですね。そのため、以下のように修正してください。

$ vim distance.c
18行目を変更
#include <malloc.h>
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
#include <stdlib.h>

$ vim word-analogy.c
18行目を変更
#include <malloc.h>
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
#include <stdlib.h>

$ vim compute-accuracy.c 
19行目を変更
#include <malloc.h>
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
#include <stdlib.h>

ここではvimで編集をしていますが、vimに慣れていない場合は直接テキストを開いて、上書き保存するのでもいいです。

(3) 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:45:19: warning: implicitly declaring library function 'malloc' with type 'void *(unsigned long)'
     [-Wimplicit-function-declaration]
 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
                 ^
distance.c:45:19: note: include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
distance.c:30:8: warning: unused variable 'ch' [-Wunused-variable]
 char ch;
      ^
2 warnings generated.
gcc word-analogy.c -o word-analogy -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-result
word-analogy.c:45:19: warning: implicitly declaring library function 'malloc' with type 'void *(unsigned long)'
     [-Wimplicit-function-declaration]
 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
                 ^
word-analogy.c:45:19: note: include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
word-analogy.c:30:8: warning: unused variable 'ch' [-Wunused-variable]
 char ch;
      ^
2 warnings generated.
gcc compute-accuracy.c -o compute-accuracy -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-result
compute-accuracy.c:28: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;
                                                                                                           ^
1 warning generated.
chmod +x *.sh
kubotanozomunoMacBook-Air:word2vec kubotanozomu$ word2vec
-bash: word2vec: command not found
kubotanozomunoMacBook-Air:word2vec kubotanozomu$ make
gcc distance.c -o distance -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-result
distance.c:31:8: warning: unused variable 'ch' [-Wunused-variable]
 char ch;
      ^
1 warning generated.
gcc word-analogy.c -o word-analogy -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-result
word-analogy.c:31:8: warning: unused variable 'ch' [-Wunused-variable]
 char ch;
      ^
1 warning generated.
gcc compute-accuracy.c -o compute-accuracy -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-result
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;
                                                                                                           ^
1 warning generated.
chmod +x *.sh

エラーは出るのですが、これでmakeはできます。

$chmod +x *.sh

(4)データを入れてみよう

curl http://mattmahoney.net/dc/text8.zip >text8.zip

このように入れます。その次は解凍をします。上の命令でtext8.zipをダウンロードした状態のため、以下のように解凍しましょう。

unzip text8.zip

解凍をした時に以下のようなエラーが出ることがあります。

  End-of-central-directory signature not found.  Either this file is not
 a zipfile, or it constitutes one disk of a multi-part archive.  In the
 latter case the central directory and zipfile comment will be found on
 the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of text8.zip or
       text8.zip.zip, and cannot find text8.zip.ZIP, period.

これはzipのダウンロードが途中で終わってしまっているときなどにおきます。僕も同じ体験をしました。その場合は、もう一度、 `curl http://mattmahoney.net/dc/text8.zip >text8.zip` をしてから `unzip text8.zip` をすると良いでしょう。

Archive:  text8.zip
 inflating: text8     

このようになって、成功します。では、早速

bash demo-word.sh

以下のようにトレーニングが開始されます。

Starting training using file text8
Vocab size: 71291
Words in train file: 16718843
Alpha: 0.043312  Progress: 13.38%  Words/thread/sec: 79.32k  

こんな風にカウントアップされていきます。Progressとなっている箇所が100%になると、完了です。完了すると、

chmod +x *.sh
Starting training using file text8
Vocab size: 71291
Words in train file: 16718843
Alpha: 0.000005  Progress: 100.10%  Words/thread/sec: 78.63k  
real	16m46.621s
user	53m7.187s
sys	0m19.179s
Enter word or sentence (EXIT to break): 

と出ます。では、早速、word2vecに愛って何か、聞いてみましょう。

(5)loveを入力してみる

さて、早速、loveという単語を入れてみると、以下のようになります。

Word: love  Position in vocabulary: 611

                                             Word       Cosine distance
------------------------------------------------------------------------
                                       unrequited		0.635158
                                           loving		0.561034
                                            loves		0.547625
                                     heartbreaker		0.515660
                                          longing		0.504907
                                        brotherly		0.503582
                                       melancholy		0.500771
                                               my		0.497416
                                               me		0.493608
                                       passionate		0.490497
                                       tenderness		0.488820
                                           philia		0.488741
                                            lover		0.484336
                                         conjugal		0.483491
                                        affection		0.483430
                                           akhana		0.483353
                                          sensual		0.480112
                                          despair		0.478886
                                             lust		0.473581
                                       loneliness		0.472902
                                             pity		0.465127
                                          passion		0.465012
                                         intimacy		0.462090
                                             kiss		0.458822
                                     faithfulness		0.458746
                                             duet		0.457456
                                          delight		0.456441
                                            gloom		0.454172
                                             behn		0.453928
                                       unfaithful		0.452306
                                             girl		0.450969
                                            songs		0.447318
                                        pseudolus		0.446422
                                        boyfriend		0.446385
                                           behold		0.445813
                                        sleepless		0.445576
                                          courtly		0.444662
                                         feelings		0.444635
                                          beloved		0.444139
                                           thighs		0.443309

もっとも類似している言葉をword2vecが探し出してくれました。上から見ていきましょう。

                                        unrequited		0.635158
                                           loving		0.561034
                                            loves		0.547625
                                     heartbreaker		0.515660
                                          longing		0.504907
                                        brotherly		0.503582
                                       melancholy		0.500771
                                               my		0.497416
                                               me		0.493608
                                       passionate		0.490497

これを日本語にすると、

                                            片思い            	0.635158
                                           愛すること		0.561034
                                            愛する人		0.547625
                                     	    失恋               0.515660
                                              憧れ		0.504907
                                          兄弟同様の		0.503582
                                              憂鬱		0.500771
                                              私の		0.497416
                                               私		0.493608
                                            情熱的		0.490497

激しく切ない・・・。

憧れている人がいて、その人に対していつしか片思いになってしまい、片思いしている相手に情熱的に恋愛するも、相手は兄弟同様だと思っていて、失恋して憂鬱。みたいなドラマのワンシーンが思い浮かぶかのようです。

愛って切ないものなのかもしれませんね・・・。(どういうまとめ!)

(6)likeと入力してみる

では、loveとlikeはどう違うのでしょうか。likeを叩いてみましょう。

Word: like  Position in vocabulary: 132

                                             Word       Cosine distance
------------------------------------------------------------------------
                                         resemble		0.544820
                                             such		0.535207
                                          similar		0.508876
                                           unlike		0.508397
                                       resembling		0.494937
                                             look		0.472386
                                            other		0.457360
                                            looks		0.433398
                                               do		0.428802
                                        similarly		0.427285
                                          confuse		0.424743
                                         familiar		0.420907
                                             feel		0.420001
                                              etc		0.415029
                                           depict		0.410830
                                          imagine		0.408885
                                           pointy		0.406764
                                           pretty		0.404030
                                           behave		0.400227
                                             many		0.397723
                                              say		0.394682
                                        flattened		0.390301
                                           prefer		0.388824
                                           exotic		0.388110
                                        pronounce		0.387057
                                          whereas		0.386854
                                             akin		0.386525
                                  anthropomorphic		0.385220
                                            sucks		0.384073
                                             seem		0.383606
                                           lizard		0.383105
                                       disgusting		0.379794
                                          screams		0.379031
                                    distinctively		0.378156
                                          imitate		0.376723
                                             ears		0.375655
                                      commendable		0.372813
                                        imitating		0.372683
                                    superficially		0.367880
                                           kindly		0.367712

これも上位のキーワードを分析していきます。

                                         resemble		0.544820
                                             such		0.535207
                                          similar		0.508876
                                           unlike		0.508397
                                       resembling		0.494937
                                             look		0.472386
                                            other		0.457360
                                            looks		0.433398
                                               do		0.428802
                                        similarly		0.427285

これを日本語にすると、以下のようになります。

                                           似ている		0.544820
                                            そんな		0.535207
                                            類似性		0.508876
                                        好きではない		0.508397
                                              似る		0.494937
                                             見た目		0.472386
                                               他		0.457360
                                              見る		0.433398
                                              する		0.428802
                                            同様に		0.427285

残念ながら、like は英語表現の場合、「Like A Rolling Stone」みたいに、「ーのような」という表現で使われるため、愛に近いニュアンスのlikeを抽出できませんでした。

サポートされた費用は、また別のカンファレンス参加費などに当てようと思います。