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

Python初心者がPyTorchをいじって機械学習の計算してみた PyCon mini Hiroshima 2018 / Python-newbies machine learning learning with PyTorch

Python初心者がPyTorchをいじって機械学習の計算してみた PyCon mini Hiroshima 2018 / Python-newbies machine learning learning with PyTorch

PyCon mini Hiroshima 2018 (2018年10月6日(土)) での「Python初心者がPyTorchをいじって機械学習の計算してみた」という稚拙な発表のスライドです。

853cd87b5c317b8c16b0e78c2efbf8d1?s=128

北䑓如法

October 08, 2018
Tweet

More Decks by 北䑓如法

Other Decks in Technology

Transcript

  1. Pythonॳ৺ऀ͕PyTorchΛ͍ͬͯ͡ ػցֶशͷܭࢉΛͯ͠Έͨ Python-newbie’s machine learning learning with PyTorch ๺㝳೗๏ PyCon

    mini Hiroshima 2018 @޿ౡେֶ ະདྷ૑ੜηϯλʔ @2018೥10݄6೔(౔)
  2. ࣗݾ঺հ

  3. None
  4. ๺㝳೗๏ ▸ ͖͍ͨͩΏ͖ͷΓ ▸ ʮ೗๏ʯΛԻಡΈ(༗৬ಡΈ)ͯ͠ňʹΐ΄͏ʼn ▸ ϋΠύɾϝσΟΞɾτϥϯεϨʔλ ▸ ೔༵ϓϩάϥϚ

  5. iPad ޿ౡห ݕࡧ ৄ͘͠͸

  6. ʮ͜Μͳײ͡ͷ͕ग़͖ͯ·͢ɻʯ

  7. Pythonॳ৺ऀ͕PyTorchΛ ͍ͬͯ͡ ػցֶशͷܭࢉΛͯ͠Έͨ ݁Ռwwwwwwwwww

  8. Pythonॳ৺ऀ͕PyTorchΛ ͍ͬͯ͡ ػցֶशͷܭࢉΛͯ͠Έͨ ݁ՌΛൃද͠·͢ɻ

  9. ʢɾ㱼ɾʣŘŘ!!

  10. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ

  11. ΋͏ͪΐͬͱ ৄ͘͠

  12. ຊ೔ͷϝχϡʔ ػցֶश 1Z5PSDI ΍ͬͯΈͨྫ

  13. Pythonॳ৺ऀ?

  14. Pythonॳ৺ऀ౓߹͍ ▸ for ͱ͔ if ͱ͔ with ͱ͔ϨϕϧͰຊɾυΩϡϝϯτɾωοτΛݟͳ͕ΒͰ ͳ͍ͱॻ͚ͳ͍ ▸

    Ψνͷॳ৺ऀ
  15. ػցֶश͸?

  16. CourseraͷNgઌੜͷ ػցֶशͷίʔε

  17. मྃ! (2016೥5݄͝Ζ)

  18. ͔͠΋मྃ࣌ ʹNgઌੜ͔Β

  19. ͔͠΋मྃ࣌ʹ Andrew Ng ઌੜ͔Β ▸ Those were the topics of

    this class and if you worked all the way through this course you should now consider yourself an expert in machine learning. ▸ As you know, machine learning is a technology that's having huge impact on science, technology and industry. ▸ And you're now well qualified to use these tools of machine learning to great effect. ▸ I hope that many of you in this class will find ways to use machine learning to build cool systems and cool applications and cool products. ▸ And I hope that you find ways to use machine learning not only to make your life better but maybe someday to use it to make many other people's life better as well.
  20. Ngઌੜ͔Βઐ໳Ո ೝఆ͞ΕͱΔ

  21. ͜Γ΋͏ؒҧ͍ͳ͘Θ͠ ػցֶशͷઐ໳Ո Ͱ͢(ތுදݱ)

  22. 1. ػցֶश

  23. ػցֶशͱ͸ ▸ ͬ͘͟Γ ▸ ڭࢣ͋Γֶश (supervised learning) ▸ ڭࢣͳֶ͠श (unsupervised

    learning)
  24. ػցֶशͱ͸ ▸ ͍ΖΜͳσʔλͷഎޙʹજΉ๏ଇͱ͔ߏ଄ΛɺऔΓग़ͦ͏ͱ͢Δܭࢉ ▸ ͬ͘͟Γ ▸ ڭࢣ͋Γֶश (supervised learning) ▸

    ڭࢣͳֶ͠श (unsupervised learning) ▸ ͷ2छྨ͕͋Δ
  25. ڭࢣ͋Γֶश — ػցֶश ▸ σʔλʹରԠ͢Δσʔλ͕͋Δ ▸ ͱ͍͍ͩͨͳΓͦ͏ͳؔ਺ f Λ୳Γ౰͍ͯͨ ▸

    ͔͠΋͜Ε͔ΒಘΔະ஌ͷσʔλʹ͍ͭͯ΋ͦ͏ͳͬͯཉ͍͠ x1 , x2 , …, xN (N͸ී௨େ͖͍) y1 , y2 , …, yN xn ↦ yn yn = f(xn )
  26. ྫ͑͹

  27. ը૾Λೖྗͨ͠Βɺ ͍ࣸͬͯΔ΋ͷ(ೣɾ ص…)Λ౴͑Δؔ਺

  28. ΍Γํ

  29. શવҧ͏ؔ਺͔Β࢝Ί ͯɺ ͡Θ͡Θ͚͍ۙͮͯ͘

  30. தֶ2೥਺ֶՊ

  31. தֶߍ2೥ੜΛࢥ͍ग़͢ σʔλʹҰ൪߹͏௚ઢ 0.2 0.4 0.6 0.8 1.0 1.1 1.2 1.3

    1.4 1.5 1.6 1.7 y = ax + b a, bΛܾΊΔ
  32. ͜͏͍͏ͷ΋ ཱ೿ͳػցֶश

  33. ͞Βʹผͷ ؔ਺Λ߹੒

  34. ▸ Կ͔ʹ൓Ԡ͢Δײ͡ͷؔ਺͕࡞ΕΔ ͞Βʹผͷؔ਺Λ߹੒ -1.0 -0.5 0.5 1.0 1.5 2.0 0.5

    1.0 1.5 2.0 -10 -5 5 10 0.2 0.4 0.6 0.8 1.0 s(x) = 1 1 + e−x , s(f(x))
  35. ࡢࠓྲྀߦͬͯ ͍Δͷ͸

  36. ਂ૚ֶश Deep learning

  37. ͖ͬ͞ͷؔ਺Λ ͨ͘͞Μ༻ҙͯ͠ ͭͳ͛·͘Δ

  38. ͭͳ͛·ͬͨ͘ͷ = χϡʔϥϧɾωοτϫʔΫ ▸ ◦1ͭ1͕͖ͭͬ͞ͷؔ਺ ▸ ΍͡Δ͠ͷΑ͏ʹ ▸ ग़ྗΛ࣍ͷೖྗʹ

  39. 2. PyTorch

  40. None
  41. PyTorch ▸ ਂ૚ֶश༻ͷϑϨʔϜϫʔΫ ▸ theano, Caffe, TensorFlow, mxnet, Chainer ▸

    ͍ͬͺ͍͋Δ͏ͪͷҰͭ ▸ Torch ΑΓ (Torch ͸ LuaJIT) ▸ ChainerΛେ͍ʹࢀߟʹ͍ͯ͠Δ ▸ (ʮforkͱݺͼ͚ͨΕ͹Ͳ͏ͧʯhttps://www.reddit.com/r/MachineLearning/comments/ 74md00/n_how_to_use_chainer_for_theano_users/dnzkba1/ )
  42. PyTorch ▸ ಛ௃ ▸ υΩϡϝϯτɾνϡʔτϦΞϧ͕݁ߏৄ͍͠ ▸ ಈతʹܭࢉάϥϑ࡞੒ ▸ ࣗಈඍ෼ ▸

    χϡʔϥϧɾωοτϫʔΫ͕ॻ͖΍͍͢ (Chainerͱࣅ͍ͯΔ)
  43. ॻ͖΍ͦ͢͏ͳงғؾΛݟΔ class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1,

    10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10) def forward(self, x): x = F.relu(F.max_pool2d(self.conv1(x), 2)) x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(-1, 320) x = F.relu(self.fc1(x)) x = F.dropout(x, training=self.training) x = self.fc2(x) return F.log_softmax(x, dim=1) ‏͖ͬ͞ͷ˓Λ Կݸ༻ҙͯ͠ ͭͳ͙͔ ‏ͭͳ͛Δͱ͖ʹ߹੒͢Δؔ਺
  44. ॻ͖΍ͦ͢͏ͳงғؾΛݟΔ ▸ random_split ▸ σʔλΛγϟοϑϧͭͭ͠෼ׂͯ͘͠ΕΔศརϝιου train_size = int(0.8 * len(dataset))

    test_size = len(dataset) - train_size trainset, testset = torch.utils.data.random_split(dataset, [train_size, test_size]) NEW 0.4.1
  45. PyTorchདྷͱΔ ▸ Frameworks mentioned @ICLR (International Conference on Learning Representations)

    2018➡2019 ▸ TensorFlow 228 ➡ 266 ▸ Keras: 42 ➡ 56 ▸ PyTorch 87 ➡ 252 ▸ https://www.reddit.com/r/MachineLearning/comments/9kys38/ r_frameworks_mentioned_iclr_20182019_tensorflow/ ▸ ݁ߏݚڀͷίʔυ͕PyTorchͰॻ͔Ε͍ͯΔ ▸ ΦϯϥΠϯߨٛ΋PyTorch͕૿͖͑ͯͨ
  46. PyTorch 1.0.0.dev20181002 (1.0 developer preview!) ΋͏ͪΐͬͱͰ ver.1.0 https://code.fb.com/ai-research/facebook-accelerates-ai-development-with-new-partners-and-production-capabilities-for-pytorch-1-0/

  47. 3. ΍ͬͯΈͨྫ

  48. ᶃ΢Σϒϖʔδ෼ྨ

  49. ΢Σϒϖʔδ෼ྨ?

  50. ΢Σϒϖʔδ෼ྨ? ▸ ΢ΣϒϖʔδͷςΩετσʔλΛೖྗͱ͢Δ ▸ ͦͷϖʔδͷ಺༰ͷछྨΛਪఆ͢Δ ▸ ·ͣ͸࿅शͷͨΊʹʮ2஋෼ྨʯ

  51. ΍Γํ ▸ ࣗ෼ͷʮ͸ͯͳϒοΫϚʔΫʯ4ສҎ্ϒοΫϚʔΫ͍ͯ͠Δ ▸ ϒοΫϚʔΫλάΛͤͬͤͱ෇͚͍ͯΔ ▸ ʮػցֶशʯλάΛ෇͚͍ͯΔ͔Ͳ͏͔Ͱ෼ྨ ▸ 1000Ҏ্͋Δ ▸

    ࣗ෼ͷϒοΫϚʔΫ͔ΒσʔλΛूΊ͖ͯͯɺֶशɻ ▸ text ➡ MeCab(ܗଶૉղੳ) ➡ fasttext ➡ จॻϕΫτϧʹ͢Δ
  52. ΍Γํ ࢈ۀٕज़૯߹ݚڀॴ͸ 2018೥5݄29೔ɺ౦ژେ ֶͱڞಉͰɺػցֶशͷֶ शॲཧʹඞཁͳԋࢉྔΛ࡟ ݮ͢Δܭࢉํࣜͱԋࢉճ࿏ ΛߟҊͨ͠ͱൃදͨ͠ɻ ࠓقͷΞχϝͷ ͋ͦͼ͋ͦ͹ͤͬͯ ໘ന͍Ͷɻ

    ػցֶशͷ͜ͱ͕ॻ͍ͯ͋Δϖʔδ͡Ό! ΢ΣϒϖʔδB ΢ΣϒϖʔδA ػցֶश͡Όͳ͍ϖʔδ͡Ό!
  53. Demo

  54. None
  55. None
  56. ͨͿΜ͏·͍ͬͨ͘!

  57. ᶄϒοΫϚʔΫ਺ਪఆ

  58. ϒοΫϚʔΫ਺ਪఆ?

  59. ϒοΫϚʔΫ਺ਪఆ? ▸ ࣗ෼ͷʮ͸ͯͳϒοΫϚʔΫʯ4ສҎ্ϒοΫϚʔΫ͍ͯ͠Δ ▸ ֤ϖʔδͷϒοΫϚʔΫ਺…ଞͷਓ΋߹ΘͤͯԿਓϒοΫϚʔΫ͔ͨ͠ ▸ ·ͨࣗ෼ͷϒοΫϚʔΫ͔ΒσʔλΛूΊֶͯश ▸ text ➡

    MeCab(ܗଶૉղੳ) ➡ fasttext ➡ จॻϕΫτϧʹ͢Δ ▸ ద౰ʹωοτϫʔΫΛֶͭͬͯ͘शͯ͠ΈΔ
  60. Demo

  61. ܇࿅σʔλଛࣦ⤵ ࢼݧσʔλଛࣦ⤴ ʮաֶश͠·͘ΔΈ͍ͨͰ͢ɻʯ

  62. (´ɾωɾ`) શવ͏·ֶ͘श͠ͳ͍ (աֶश͹͔Γ͢Δ)

  63. ϒοΫϚʔΫ਺ਪఆ͸จॻͰ͸ແཧͰ͸? ▸ ബʑؾ෇͍͍ͯͨ ▸ ͸ͯͳϒοΫϚʔΫͷ਺Λϖʔδͷ಺༰͚͔ͩΒਪଌ͢Δͷ͸ແཧͰ͸ɻ ▸ ༗໊ਓ͕֦ࢄͨ͠ͱ͔ɺଞͷӡͷཁૉ͕ڧ͍ ▸ Ͱ΋७ਮʹจষ͚͔ͩΒજࡏతͳʮϒοΫϚʔΫ͞ΕྗʯΈ͍ͨͳͷ͕Ͱ ͳ͍͔ͳ͋ͱ୶͍ظ଴͸͋ͬͨ

    ▸ ݚڀଓߦ͢Δ΂͖! ͸ͯϒΛՔ͍͗ͨ!
  64. ᶅGAN

  65. GAN?

  66. GAN? ▸ GAN = generative adversarial network (ఢରతੜ੒ωοτϫʔΫ) ▸ ੜ੒ऀ

    generator (G) v.s. ࣝผऀ discriminator (D) ͕ఢର͠ͳ͕Βֶशͯ͠ ͍͘ ▸ G ͱ D ͕ఢର͠ͳ͕Βֶश͍ͯ͘͠ ▸ Կ͔Λʮੜ੒͢Δʯ
  67. ը૾ੜ੒ͷ࡞ྫ

  68. ͜͏͍͏ͷ͕ ࡞Γ͍ͨ ▸ Ҵ৔Ѫ߳ͪΌΜͱΘ͠ͷը૾Λ ֶशͯ͠ɺը૾Λੜ੒

  69. epoch 25 ▸ ΄΅ϊΠζ

  70. epoch 37 ▸ ΍΍إΒ͖͠΋ͷ͕ݟ͖͑ͯͨ

  71. epoch 149 ▸ ੜ੒Ͱ͖͖ͯͨ!

  72. epoch 192

  73. ൺֱ ਖ਼ ϑΣΠΫ

  74. ָ͍͠ ✌('ω'✌ )ࡾ✌('ω')✌ࡾ( ✌'ω')✌

  75. ·ͱΊ ▸ Pythonॳ৺ऀ͕PyTorchΛ࢖ͬͯػցֶशΛ΍ͬͯΈͨ ▸ ͔֬ʹॻ͖΍͍͢ؾ͕͢Δ ▸ ͲΜͲΜ࣮ݧͯ͠༡΂Δ ▸ ৽͍͠ઃܭ͕࣍ʑग़͍ͯΔͷͰ΍͍͖͍ͬͯͨ ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ