LoginSignup
0
0

More than 3 years have passed since last update.

fastTextで単語の分散表現を獲得する

Last updated at Posted at 2019-09-26

準備

下記のページに書いてあるので,詳しくはここを見てください.
https://qiita.com/m__k/items/68a149139532bc8cf657

$ git clone https://github.com/facebookresearch/fastText.git
$ cd fastText
$ make

学習

なお,前回の“Word2Vecで単語の分散表現を獲得する”と同様,TravelBlogのテキスト情報を用いて学習を行います.

$ ./fasttext skipgram -input [textファイル] -output [modelの名前] -dim 300
Read 528M words
Number of words:  807037
Number of labels: 0
Progress: 100.0% words/sec/thread:  118286 lr:  0.000000 avg.loss:  0.503195 ETA:   0h 0m 0s

確認

king - man + woman = ?

confirm.py
import gensim
model = gensim.models.KeyedVectors.load_word2vec_format('[modelの名称].vec', binary = False)
print(model.most_similar(positive=["king", "woman"], negative=["man"], topn=9))
[(‘kings’, 0.6394046545028687), (‘king窶冱’, 0.6300824284553528), (‘king-sized’, 0.617986261844635), (‘king/queen’, 0.6178181171417236), (‘pharaoh窶冱’, 0.6167725324630737), (‘king’’, 0.6152504682540894), (‘birthrate’, 0.6043733358383179), (‘queen-sized’, 0.6003711819648743), (‘pharaoh)’, 0.5966148376464844)]

うまくいかない...
Lossが下がりきっていないかも.
調べてみると,epochのデフォルトは5とのこと.
epochを30にしてみよう

$ ./fasttext skipgram -input [textファイル] -output [modelの名前] -dim 300 -epoch 30
Read 528M words
Number of words:  807037
Number of labels: 0
Progress: 100.0% words/sec/thread:   94129 lr:  0.000000 avg.loss:  0.097892 ETA:   0h 0m 0s
[('queen', 0.5857873558998108), ('king,', 0.5639103055000305), ('King', 0.5392391681671143), ('kings', 0.532159686088562), ('princess', 0.510100781917572), ('king.', 0.4975261688232422), ('kingly', 0.49258339405059814), ('kings,', 0.4881439208984375), ('throne', 0.48232918977737427)]

うまくいきました.
やはり,学習が足りなかったようです.

追加実験(さらにepochを変更してみると...)

$ ./fasttext skipgram -input ../sentences -output fastText_travelblog_300dim_epoch50_model -dim 300 -epoch 50
Read 528M words
Number of words:  807037
Number of labels: 0
Progress: 100.0% words/sec/thread:   99613 lr:  0.000000 avg.loss:  0.071412 ETA:   0h 0m 0s

Lossが下がっていることを確認.
実際に演算をしてみる.

[('king,', 0.5747531652450562), ('queen', 0.5343612432479858), ('kings', 0.5206679105758667), ('king.', 0.5016391277313232), ('princess', 0.4929012656211853), ('princess,', 0.48218420147895813), ('queen.', 0.47622746229171753), ('King', 0.4598667025566101), ('queen。ヌs', 0.4520658254623413)]

まさかのking

関連のある投稿

Word2Vecで単語の分散表現を獲得する

参考ページ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0