Upgrade to Pro — share decks privately, control downloads, hide ads and more …

word2vec

Avatar for asako asako
August 16, 2016

 word2vec

word2vec with Japanese wiki

Avatar for asako

asako

August 16, 2016

More Decks by asako

Other Decks in Programming

Transcript

  1. 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
  2. Word2vec command option -train text8 -output vectors.bin -cbow 1 -size

    200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 20 -binary 1 -iter 15 ⼊⼒テキストの指定(トレーニング対象のファイル) 出⼒ファイル指定 学習モデルとして、CBOWを利⽤する(0はCBOWを使⽤しない) ベクトルの次元は200次元を指定 ⽂脈の調査は5単語(最⼤)とする ネガティブサンプリングを使⽤する(0指定のときはネガティブサンプリングを指定しない) 階層的softmax関数を使⽤回数(0のときはsoftmaxを使⽤しない) 頻出⽤語をランダムに削除する頻度 学習時に使⽤するthread数 1のとき、出⼒はバイナリ形式 トレーニング回数
  3. $ grep dog vectors.txt | head -n 1 dog 1.497807

    -2.207421 1.287924 -1.169853 -1.995175 -4.605156 -0.983736 0.906955 3.729887 1.213424 1.757983 0.863675 -1.238099 -0.507045 1.653404 -1.053151 -2.823349 0.262739 -0.634917 -1.403906 1.428062 -1.258658 0.112231 -3.481446 -3.019106 1.006109 -1.114303 0.057690 0.320875 0.159142 3.323169 1.795204 -0.362317 0.486087 4.609807 -1.925961 -1.607635 -0.388860 1.131185 -1.378571 0.671292 0.365952 2.174920 -3.415972 -0.190324 0.720923 -2.365446 -1.382508 -0.763290 -3.521616 2.024997 -1.769034 0.674375 0.044683 -0.660109 -0.205586 1.421317 1.490870 0.978147 -1.149919 -0.746359 1.602159 -1.783502 0.942932 -3.061677 2.502873 0.056530 3.202985 0.868384 0.679455 2.370200 0.445737 -3.815015 0.681365 -2.014009 1.676140 -1.600600 -1.248753 -1.761046 0.483791 -0.026427 -0.841599 1.943773 1.090983 -1.118057 2.346817 -1.654284 -1.131028 -0.672592 1.226551 -1.714415 1.823260 1.352924 2.683858 -2.178489 1.691334 1.612181 -0.502800 0.178338 3.108812 -0.292714 0.481172 0.038127 -1.313673 -1.836519 -0.284010 -0.757307 -0.731006 -1.096164 0.521179 -0.492003 0.609147 0.305544 -1.391514 -1.100750 -0.760810 -1.618806 -3.181895 0.301359 3.423227 -0.401323 -0.381855 -0.282400 1.415674 -1.586130 -2.737540 -2.445797 -0.836525 -3.755567 1.121143 0.676512 -0.304506 0.389946 0.529214 -2.556710 0.598955 1.882520 -2.998037 -2.498373 0.519162 2.250599 -0.680106 1.274055 -0.199514 2.112689 -1.716358 0.883779 -2.536912 1.840344 0.699085 -1.286275 -0.429717 0.516426 2.571573 1.852433 -0.957952 -0.719093 0.777511 0.749733 1.018177 1.380973 -0.995046 0.065024 -1.572428 -1.128072 -1.034657 2.088688 -0.339937 -0.493523 -1.690455 0.103554 0.921699 0.615035 2.535376 -0.241644 4.374370 0.393955 1.277425 1.626089 -0.829838 -1.327664 -4.095205 -2.151237 2.126780 -1.605928 -0.885592 0.494912 1.272462 -2.053224 -1.488817 4.861637 -1.539893 0.553005 0.330127 0.039959 -0.352139 -1.357162 -1.851695 -2.056664 -0.675292 Output file (text file)
  4. ಉ෧͞ΕͨEFNP༻TIFMMTDSJQUT corpus 距離表⽰ word 演算 word のクラスタリング 処理 word vector

    トレーニ ング評価 demo-word- accuracy.sh demo-word.sh demo-classes.sh demo-analogy.sh
  5. WP2TXT 使ってみる。私の環境は、rbenv を使っているので。。。 $ rbenv exec gem install bundle $

    rbenv exec bundle init $ echo ‘gem “wp2txt”’ > Gemfile $ rbenv exec bundle init $ rbenv exec wp2txt --input-file jawiki-latest-pages-articles.xml.bz2 --output-dir jawiki_wp2txt --no-heading --no-title --nomarker ࣌ؒ @
  6. WP2TXT ⽣成されたファイルの中⾝はこんな感じ。 {{Infobox religious building |image = |caption = |building_name

    = Masjid Hira Gyotoku |location = ˟272-0133 ઍ༿ݝࢢ઒ࢢ̏ʵ̍̕ߦಙӺલ̏ஸ໨9−12 Tel (Fax): +81 47-395-8449 (047-398-0261) |latitude = 35.6815058 |longitude = 139.9184761 |religious_affiliation = ΠεϥϜڭ εϯχ೿ |website =
  7. Wikipedia Extractor ⽣成されたファイルの中⾝はこんな感じ。いい感じ。 <doc id="3455820" url="https://ja.wikipedia.org/wiki?curid=3455820" title="ߦಙϞεΫ"> ߦಙϞεΫ ߦಙϞεΫ·ͨ͸ ώϥɾϚεδυʢ

    ʣ͸ɺઍ༿ݝࢢ઒ࢢߦಙӺલʹॴࡏ͢ΔϞεΫɻ1997೥ʹݐͯΒΕͨɻΠεϥϛοΫɾαʔΫϧɾΦϒɾδϟύϯ ʢICOJʣͷҰ෦ہͰ͋Δ೔ຊϞεΫϑΝ΢ϯσʔγϣϯʢJMFʣʹΑͬͯӡӦ͞Ε͍ͯΔɻ ೔ຊޠදهʹ͍ͭͯ͸ʮώϥϚεδουʯʢ ) ͷ΄͔ɺʮߦಙϞεΫʯ ͱ༻͍ΒΕΔɻ ϞεΫ͸ɺ࣍ͷجຊతͳαʔϏεΛఏڙ͍ͯ͠Δ ݐ෺͸̐֊ݐͯͰɺ ֤֊ͷׂΓ౰ͯ͸ɺҎԼͷ௨ΓͰ͋Δɻ ࠷دΓӺ͸౦ژϝτϩ౦੢ઢߦಙӺɻ(ెาͰ7෼) </doc>
  8. jawiki-watati.txt $ ls -lh jawiki-wakati.txt -rw-r--r-- 1 asako staff 2.5G

    8 7 14:35 jawiki-wakati.txt $ wc jawiki-wakati.txt 7543274 417027641 2706398402 jawiki-wakati.txt
  9. word2vec $ ./word2vec -train ./data/jawiki-wakati.txt -output jawiki.bin -size 200 -window

    8 -negative 25 -hs 0 -sample 1e-4 -thread 2 -binary 1 -iter 15
  10. wakati for uncyclopedia Extractedディレクトリの下のファイルの 全ての わかち書き⽣成にトライ! Word Extractorの出⼒は500K毎の出⼒に分割してしまったので、これをword2vec⽤に ひとつのファイルにまとめる。 $

    python a.py ./uncyclopedia/extracted/*/* | ¥ mecab -b 1000000 ¥ -F"%f[6] " -U"%m " -E"\n" ¥ -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd ¥ > ./uncyclopedia/jawiki-uncyclopedia-wakati.txt
  11. jawiki-uncyclopedia-watati.txt $ ls -lh jawiki-uncyclopedia-wakati.txt -rw-r--r-- 1 asako staff 60M

    8 15 14:36 jawiki-uncyclopedia-wakati.txt $ wc jawiki-uncyclopedia-wakati.txt 200546 10136172 62522042 jawiki-uncyclopedia-wakati.txt