Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
word2vec
Search
asako
August 16, 2016
Programming
42
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
word2vec
word2vec with Japanese wiki
asako
August 16, 2016
More Decks by asako
See All by asako
quantum computer
asakoraven
0
54
Introduce Jupyter Notebook
asakoraven
0
34
Load csv by Cypher
asakoraven
0
49
Query by cypher
asakoraven
0
23
time series database
asakoraven
0
31
word2vec accuracy
asakoraven
0
78
Other Decks in Programming
See All in Programming
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.2k
The Rails Doctrine Decade
koic
2
590
Domain-Driven Transformation
hschwentner
2
2.3k
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
170
contenteditable と日本語入力に向き合う
colorful12
0
200
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
6.6k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
190
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
160
[JA] Domain Modeling with Vanilla Rails: 「操作」から再考するRailsの設計論
yasaichi
0
140
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
140
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
28
32k
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
2
670
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
Designing for Performance
lara
612
70k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
640
Ethics towards AI in product and experience design
skipperchong
2
430
Building a Scalable Design System with Sketch
lauravandoore
464
34k
It's Worth the Effort
3n
188
29k
Bash Introduction
62gerente
615
220k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
400
Building Applications with DynamoDB
mza
96
7.2k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
570
RailsConf 2023
tenderlove
30
1.6k
Transcript
word2vec A report by Asako TOYAMA
Summary word2vec の意味は word to vector 単語をベクトルに変換する ベクトルが近い同⼠は、類語 与えたテクストを元にコーパスを作成できる (これまでのロジックより速く)
ベクトルなので、単語の演算ができる
Summary word2vecはTomas Mikolovらによって提案されたニューラルネットワーク(CBOW, Skip-gram)の実装 https://en.wikipedia.org/wiki/Word2vec 読んでも、よくわからない TensorFlowの 説明書だけれど ⽇本語翻訳ページがあった! http://tensorflow.classcat.com/2016/03/12/tensorflow-cc-word2vec/
読んでも、よくわからない
Summary king - man + woman = queen
Implementations C⾔語 (元祖) https://code.google.com/archive/p/word2vec 昔はちゃんとgoogle codeのプロジェクトだったのに、今は、アーカイブ? 下のgithubに移動したらしい https://github.com/svn2github/word2vec
Implementations Java http://deeplearning4j.org/word2vec
Implementations Python (gensim) http://radimrehurek.com/gensim/models/word2vec.html
Implementations Python (TensorFlow) word2vec は Googleの中の⼈が論⽂発表し、実装したロジックなので、 TensorFlowにも⼊っている。 動かしたら、遅いっぽい、⽂字コードの扱いが雑で、Decode/Encode関係 でException吐きまくり →
⾯倒くさくなる https://www.tensorflow.org/versions/r0.10/tutorials/ word2vec/index.html
Setting $ git clone https://github/svn2github/word2vec $ cd word2vec $ ./demo-word.sh
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
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のとき、出⼒はバイナリ形式 トレーニング回数
$ 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)
ಉ෧͞ΕͨEFNP༻TIFMMTDSJQUT corpus 距離表⽰ word 演算 word のクラスタリング 処理 word vector
トレーニ ング評価 demo-word- accuracy.sh demo-word.sh demo-classes.sh demo-analogy.sh
Demo with text8 Word2vecに同封されているデータセット オリジナルは以下のページにある http://mattmahoney.net/dc/textdata 英単語 約 17,000,000 words
⽇本語 wiki いろいろ探したけれど、フリーで 使⽤できる⽇本語のデータセット が少ない! Next try
Input data Word2vecはスペースで区切られた単語を⼊⼒とします。 Text8もカンマやピリオドを除いた単語の羅列です。
ຊޠͷσʔληοτΛ༻ҙ͢Δલʹ
ܗଶૉղੳ 形態素解析とは、⾃然⾔語を形態素(単語の最⼩要素)に分解すること。 ⽇本語はスペースデリミタがありません。 なので、句読点や辞書をつきあわせて、分解します。
ͳͷͰɺ۟ಡࣙॻΛ͖߹Θͤͯɺղ͠·͢ɻ こんなふうに・・・ なので 句読点 や 辞書 を 付き合わ せ て
分解 し ます 。 わかち書き という
ܗଶૉղੳΤϯδϯ MeCab Mecabを使うことに。 Javaで作成されたkuromojiというラ イブラリにも興味があったが、 kuromoji を動作させるには、mcab の辞書が必要だったので、 もう、mecabでいいや。。。
Mecab $ brew install mecab mecab-ipadic ຊޠܗଶૉղੳΤϯδϯ http://taku910.github.io/mecab/ ՃͰ৽ޠࣙॻ͍Ε͓ͯ͘ https://github.com/neologd/mecab-ipadic-neologd
format Wiki dump Wiki dump はxml形式。 いらないtagがいっぱいついている が、記事の本⽂だけの情報が欲し い。
Wiki dump https://dumps.wikimedia.org/jawi/latest/
WP2TXT よく使われている(らしい)rubyのツール 「word2vecやってみた」系ブログを⾒ると、ほとんど⼈がこのツールで 不要 tag削除を実施している
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 ࣌ؒ @
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 =
WP2TXT Infoboxの内容いらないのに。。。 画像のリンクURLいらないのに。。。 参照のリンクURLいらないのに。。。 なにより、処理実⾏が遅すぎる!
Wikipedia Extractor http://medialab.di.unipi.it/wiki/Wikipedia_Extractor https://github.com/attardi/wikiextractor Wiki のデータからテキストのみを抽出するスクリプト $ ./WikiExtractor.py -b 500k
-o extracted jawiki-latest-pages-articles.xml.bz2
Wikipedia Extractor ⽣成されたファイルの中⾝はこんな感じ。いい感じ。 <doc id="3455820" url="https://ja.wikipedia.org/wiki?curid=3455820" title="ߦಙϞεΫ"> ߦಙϞεΫ ߦಙϞεΫ·ͨ ώϥɾϚεδυʢ
ʣɺઍ༿ݝࢢࢢߦಙӺલʹॴࡏ͢ΔϞεΫɻ1997ʹݐͯΒΕͨɻΠεϥϛοΫɾαʔΫϧɾΦϒɾδϟύϯ ʢICOJʣͷҰ෦ہͰ͋ΔຊϞεΫϑΝϯσʔγϣϯʢJMFʣʹΑͬͯӡӦ͞Ε͍ͯΔɻ ຊޠදهʹ͍ͭͯʮώϥϚεδουʯʢ ) ͷ΄͔ɺʮߦಙϞεΫʯ ͱ༻͍ΒΕΔɻ ϞεΫɺ࣍ͷجຊతͳαʔϏεΛఏڙ͍ͯ͠Δ ݐ̐֊ݐͯͰɺ ֤֊ͷׂΓͯɺҎԼͷ௨ΓͰ͋Δɻ ࠷دΓӺ౦ژϝτϩ౦ઢߦಙӺɻ(ెาͰ7) </doc>
Wikipedia Extractor WikiExtractor.py は <doc>タグだけ残るので <doc>タグを削除するスクリプトを作成
wakati インストールしたmecabを実⾏して、わかち書きを実⾏ Mecabで input-buffer overflow. The line is split. use
-b #SIZE option. のエラーがでたので、バッファサイズ指定
wakati Extractedディレクトリの下のファイルの 全ての わかち書き⽣成にトライ! Word Extractorの出⼒は500K毎の出⼒に分割してしまったので、これをword2vec⽤に ひとつのファイルにまとめる。 $ python a.py
extracted/*/* | ¥ mecab -b 1000000 ¥ -F"%f[6] " -U"%m " -E"\n" ¥ -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd ¥ > ./jawiki-wakati.txt
word2vec Wiki dump やっと、word2vecに⼊⼒できる データが準備完了。
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
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
Demo with jawiki dump
⽇本語 アンサイクロペディア http://ja.uncyclopedia.info/ wiki Next try
uncyclopedia dump http://download.uncyc.org ja-wiki.zip をダウンロード
Wikipedia Extractor for uncyclopedia Uncyclopediaのダンプファイルは Zip形式のファイルだった Wikipedia Extractorの⼊⼒に合わせてbz2形式にする(解凍して、再圧縮) Wikipedia Extractor実⾏
$ ./WikiExtractor.py -b 500k -o extracted ja-pages_current.xml.bz2
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
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
word2vec for uncyclopedia $ ./word2vec -train ./data/uncyclopedia/jawiki-uncyclopedia-wakati.txt -output jawiki-uncyclopedia.bin -size
200 -window 8 -negative 25 -hs 0 -sample 1e-4 - thread 2 -binary 1 -iter 15
Demo with uncyclopedia dump
Title: word2vecによる⾃然⾔語処理 Author: ⻄尾泰和 Publisher: OʼReilly Japan URL: https://www.oreilly.co.jp/books/ 9784873116839/
ࢀߟจݙ