Swift からword2vecを 使ってみる

Swift からword2vecを 使ってみる

5287c9b9576749092d3170ec738338b8?s=128

Yusuke Ito

May 26, 2017
Tweet

Transcript

  1. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE 4XJGU͔ΒXPSEWFDΛ ࢖ͬͯΈΔ ϦʔυΞʔΩςΫτ
 ҏ౻༞ี :VTVLF*UP !OPWJ@ നϠΪίʔϙϨʔγϣϯ 5PLZP4FSWFS4JEF4XJGU.FFUVQ
 

  2. 5PLZP݄೔ʙ೔

  3. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE 4FF63- • https://github.com/shiroyagicorp/swift-seeurl • ݩʑࣾ಺ϥΠϒϥϦΛެ։ • cURL(curlίϚϯυ), libcurlϕʔε •

    ಉظॲཧ(RunLoopෆཁ)  let result = try HTTPClient.sendRequest("GET", "https://google.com") print(result.statusCode, result.body) ϑΥϩʔΞοϓهࣄUSZ4XJGU5PLZPͱͦͷޙ
 IUUQBJBMTIJSPZBHJDPKQNZUSZTXJGUBOEGPMMPXVQ
  4. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE ϝϯςφϯε͍ͯ͠ΔϥΠϒ ϥϦ • novi/mysql-swift: MySQLΫϥΠΞϯτ (όΠϯσΟϯά) • novi/nkf-swift: Network

    Kanji FilterͷόΠϯσΟϯά • novi/mecab-swift: MeCabܗଶૉղੳͷόΠϯσΟϯά • novi/i2c-swift: I2CόεϥΠϒϥϦ • shiroyagicorp/swift-seeurl: cURL(HTTPS)ΫϥΠΞϯτ 
  5. None
  6. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE OVNTX • https://github.com/sonsongithub/numsw • try! Swift TokyoͷϋοΧιϯͰ࡞ͬͨ • Pythonʹ͋Δ਺஋ܭࢉϥΠϒϥϦ(numsw)ΛSwift

    Ͱ΋࢖͍͍ͨ • ࠓ΋ͨ·ʹϝϯςφϯε͍ͯ͠Δ 
  7. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE XPSEWFD • 2013೥͘Β͍ʹެ։ • C࣮૷ • word2vecͷίϯηϓτ͸͜ͷล • https://deepage.net/bigdata/machine_learning/

    2016/09/02/ word2vec_power_of_word_vector.html 
  8. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE XPSEWFDͷιʔείʔυ • https://code.google.com/archive/p/word2vec/ • ͕ͩɺGoogle Codeऴྃʹ൐͍μ΢ϯϩʔυͰ͖ͳ͍ • https://github.com/svn2github/word2vec.git •

    ͜ͷลͷϛϥʔΛ࢖͏ • make Ͱ࣮ߦϑΝΠϧΛϏϧυ • macOSͩͱmalloc.hϔομʔ͕ແ͍ͱ͍͏Τϥʔ͕ग़ΔͷͰɺͦͷߦ Λίϝϯτ͢Δ • demo-*.sh͔Β࢝·ΔγΣϧεΫϦϓτͰσϞΛ࣮ߦ 
  9. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE XPSEWFDD • ςΩετϑΝΠϧΛೖྗͯ͠ɺτϨʔχϯάɺϞσ ϧΛग़ྗ • Cݴޠ 700ߦఔ౓ͷ CLI •

    ϚϧνεϨουରԠ • ίʔυ͕ಡΈʹ͍͘ 
  10. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE EJTUBODFD • ϞσϧϑΝΠϧͱ୯ޠ(ෳ਺)Λೖྗͯ͠ɺίαΠϯ ྨࣅ౓(ίαΠϯڑ཭)Λܭࢉͯ͠ग़ྗ  Enter word or sentence

    (EXIT to break): cat dog Word Cosine distance ------------------------------------------------------------------------ goat 0.827205 cow 0.796463 bee 0.788516 blonde 0.784559 hamster 0.784402 ass 0.779677 rat 0.774975
  11. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE 4XJGU͔Β࣮ߦ͢Δʹ͸ 1. .cͷιʔεΛSwift͔ΒݺͿ • ͍ͭ΋ͷCͷϥΠϒϥϦΛSwift͔ΒಡΈࠐΉํ๏ • ࠓճ͸ϥΠϒϥϦͰ͸ͳ͘ɺmain()ͷܗʹͳͬͯ ͍ΔͷͰɺϥούʔؔ਺ͳͲΛఆٛ 2.

    ͕Μ͹ͬͯSwiftʹҠ২ 
  12. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE ϞσϧϑΝΠϧ • ୯ޠͷϦετ(char*)ͱ୯ޠΠϯσοΫε(Int) • ୯ޠΠϯσοΫεͱ୯ޠϕΫτϧ(Float) • σϞͩͱαΠζ͕30ϝΨόΠτ͋Δ  m0

    m1 m2 i0 the -0.455356 1.908487 1.365852 i1 of 0.266191 1.750760 0.486601 i2 and 0.889470 1.455499 0.380907 i3 one -2.213606 0.914580 -0.512441
  13. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE EJTUBODFDͷ΍Δ͜ͱ • ϞσϧΛύʔεͯ͠ڊେͳ(30MBఔ౓ͷ)ϝϞϦ্ʹಡΈࠐΉ • ͋Β͔͡Ί୯ޠͱ୯ޠΠϯσοΫεʹ෼͚Δ • ୯ޠΠϯσοΫεͱ୯ޠϕΫτϧΛϝϞϦ্ʹಡΈࠐΉ • ༩͑ΒΕͨɺ୯ޠʹରͯ͠ɺ୯ޠϕΫτϧ͔ΒίαΠϯڑ

    ཭Λܭࢉͭͭ͠ɺڑ཭͕͍ۙ΋ͷΛιʔτͯ͠ग़ྗ(දࣔ) • https://ja.stackoverflow.com/questions/2001/word2vecͷ distance͸ͳͥߴ଎ʹಈ࡞͢Δͷ͔ 
  14. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE 4XJGUʹҠ২ • Ϟσϧ(୯ޠϕΫτϧ)ΛಡΈࠐΉϝϞϦۭؒ͸
 UnsafeMutablePointer<Float>.allocate(capacity:)
 Λ࢖༻ • Swift3͔Βಋೖ͞ΕͨϙΠϯλͱϝϞϦۭؒΛ҆શʹޮ཰తʹ ѻ͏࢓૊Έ(݁ߏ೉͍͠) •

    ૉ੖Β͍͠SwiftͷϙΠϯλܕͷղઆ: http://qiita.com/ omochimetaru/items/c95e0d36ae7f1b1a9052 • UnsafeRawPointer Migration: https://swift.org/migration- guide/se-0107-migrate.html 
  15. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE ΍ͬͨ͜ͱ • ୯ޠͱ୯ޠΠϯσοΫεͷରԠΛ഑ྻͰ͸ͳࣙ͘ॻʹมߋ • จࣈྻͷѻ͍ΛStringωΠςΟϒʹมߋ • ݩʑchar*΍strcmpͳͲͷCϨϕϧͩͬͨͷͰɺՄಡੑ ΍ѻ͍͕େม •

    (͓ͦΒ͘)ɺϢχίʔυ΁ͷରԠ͕Մೳ • (·ͩ΍͍ͬͯͳ͍͕)ɺιʔτपΓͷվળ 
  16. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE ࠓճͷϦϙδτϦ • https://github.com/novi/word2vec-swift 

  17. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE ·ͱΊ • C͔ΒͷҠ২͸ָ • ߏจ΋͍ۙ • ϙΠϯλपΓ΋ಉ͡࢓૊Έ͕࢖͑Δ • word2vecʹ͍ͭͯΑ͘ཧղͰ͖ͯΕ͹͞Βʹ༰қ

    
  18. $PQZSJHIU4IJSPZBHJ$PSQPSBUJPO"MMSJHIUTSFTFSWFE • ࣍ճ: നϠΪͰӡ༻͍ͯ͠ΔαʔόʔαΠυSwift • ൃදऀืूʂτϐοΫ͸ͳΜͰ΋OKʂ