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

NxでMNISTの手書き数字画像分類を試す / Training MNIST Datasets with Nx

NxでMNISTの手書き数字画像分類を試す / Training MNIST Datasets with Nx

NervesJP #15 Nxを触ってみる回
https://nerves-jp.connpass.com/event/205125/

23f4d5d797a91b6d17d627b90b5a42d9?s=128

Kentaro Kuribayashi

February 25, 2021
Tweet

Transcript

  1. ܀ྛ݈ଠ࿠ʢ(.0ϖύϘגࣜձࣾɺ๺཮ઌ୺Պֶٕज़େֶӃେֶʣ /FSWFT+1/YΛ৮ͬͯΈΔճʢ೥݄೔ʣ NxͰMNISTͷखॻ͖਺ࣈը૾෼ྨΛࢼ͢

  2. ܀ྛ݈ଠ࿠BLB͋ΜͪΆ IUUQTLFOUBSPLVSJCBZBTIJDPN ɾ(.0ϖύϘגࣜձࣾऔక໾$50 ɾҰൠࣾஂ๏ਓ೔ຊ$50ڠձཧࣄ ๺཮ઌ୺Պֶٕज़େֶӃେֶʢ+"*45ʣത ࢜લظ՝ఔࡏֶதͷࣾձਓֶੜͰ΋͋ Δɻ *P5ؔ࿈ͷݚڀΛ४උ͍ͯ͠Δͱ͜Ζʢ ݄ʹ/FSWFT͕ग़ͯ͘Δݚڀใࠂ࿦จʹͭ ͍ͯൃද͠·͢ʣɻ

    ࣗݾ঺հ 2
  3. /Y /VNFSJDBM&MJYJS JTOPXQVCMJDMZBWBJMBCMF%BTICJU#MPH IUUQTEBTICJUDPCMPHOYOVNFSJDBMFMJYJSJTOPXQVCMJDMZBWBJMBCMF

  4. *OUSPEVDJOH/Y+PTÉ7BMJNc-BNCEB%BZT IUUQTZPVUVCFG1,.N+Q"(8D

  5. ಈըΛ؍ͯΔ͚ͩͰ͸Θ͔Βͳ͍ͷͰ ϥΠϒίʔσΟϯάΛࣸܦͰ࠶ݱͨ͠

  6. +OOOY+PTÉ`T/FVSBM/FUXPSLXJUI/Y IUUQTHJUIVCDPNLFOUBSPKOOOY

  7. ͜Μͳײ͡ͰKeras෩ʹࢼͤ·͢ ./*45σʔλͷಡΈࠐΈ [x_train, y_train, x_test, y_test] = Jnnnx.MNIST.Dataset.load_data() σʔλͷ੔ܗͱਖ਼نԽ x_train

    = x_train |> Nx.reshape({60000, 28*28}, names: [:batch, :input]) |> Nx.divide(255) x_test = x_test |> Nx.reshape({10000, 28*28}, names: [:batch, :input]) |> Nx.divide(255) POFIPUFODPEJOH y_train = y_train |> Jnnnx.Utils.to_categorical(10, names: [:batch, :output]) y_test = y_test |> Jnnnx.Utils.to_categorical(10, names: [:batch, :output]) τϨʔχϯάσʔλΛ༻ֶ͍ͯश params = Jnnnx.fit(x_train, y_train, epoch: 5, batch_size: 50, learning_rate: 0.01) ςετσʔλΛ༻͍ͯධՁ score = Jnnnx.evaluate(params, x_test, y_test) IO.puts("Accuracy: #{Nx.to_scalar(score)}")
  8. ૉͷ&MJYJS $16ʢ&9-"Λ༻͍ͳ͍ʣͰ࣮ߦͨ݁͠Ռ˞ ֶश݁ՌʢΤϙοΫ5ɺֶश཰0.01ɺֶशʹཁͨ࣌ؒ͠: ൒೔͙Β͍ʣ ˞&9-" $16΋ಈ͔ͯ͠Έ͕ͨɺܻ୆Ͱ଎͘ͳΔʢ࣍ϖʔδʣͱ͸͍͑ݩ͕஗ա͗ΔͷͰಉֶ͡शΛ΋͏Ұ౓΍Δ͜ͱ͸͠ͳ͔ͬͨɻ

  9. 4PGUNBYؔ਺ͷ࣮ߦํࣜ͝ͱͷϕϯνϚʔΫ݁Ռ IUUQTHJUIVCDPNFMJYJSOYOYUSFFNBJOOYOVNFSJDBMEFGJOJUJPOT

  10. ˔ +PTÉͷϥΠϒίʔσΟϯάಈըΛ؍ͳ͕Βࣸܦͨ͠ ˔ ػցֶशϥΠϒϥϦͷΑ͏ʹ࢖͑ΔΑ͏ʹ੔ཧͨ͠ ˓ ࣸܦͨ͠ίʔυΛϥΠϒϥϦͬΆ͍ϑΝΠϧߏ଄Ͱ഑ஔ ˓ ϋΠύʔύϥϝλΛؔ਺ͷҾ਺ͱͯ͠౉ͤΔΑ͏ʹͨ͠ ˔ ./*45ͷσʔληοτΛऔಘ͢ΔϞδϡʔϧΛ௥Ճͨ͠

    ˔ ֶशͨ͠ϞσϧΛɺςετσʔλʹΑͬͯධՁ͢Δؔ਺Λ௥Ճͨ͠ ˠಈըͰσϞͯͨ͠ίʔυͷݩʹͳ͍ͬͯΔ΋ͷͱࢥΘΕΔ΋ͷ͕ FYMBͷ΄͏ͷFYBNQMFTʹ͋ͬͨʂ˞ ΍ͬͨ͜ͱ ˞IUUQTHJUIVCDPNFMJYJSOYOYCMPCNBJOFYMBFYBNQMFTNOJTUFYT
  11. ˔ ݱঢ়Ͱ͸ϨΠϠʔͷߏ੒ɺ׆ੑԽؔ਺ɺଛࣦؔ਺౳ΛܾΊଧͪʹ͠ ͍ͯΔ͕ɺࣗ༝ʹ૊Έ߹ΘͤΒΕΔΑ͏ʹ͢Δ͜ͱ ˓ ͦͷ͋ͨΓ·Ͱ΍Δͱ΋͏গ͠ϥΠϒϥϦͬΆ͘ͳΔ ˓ ͍·͸୯ʹॲཧΛͦΕͬΆ͘ݟ͑ΔΑ͏ʹ·ͱΊ͚ͨͩ ˔ &9-"Λ࢖ͬͯ(16Ͱܭࢉ͢Δ͜ͱ˞ ·ͩ΍ͬͯͳ͍͜ͱ

    ˞+FUTPO/BOP(#Ͱࢼ͔͕ͨͬͨ͠ɺϕλϕλ৮ͬͨΓ͍͔ͨͤ͠ىಈ͠ͳ͘ͳͬ
  12. ͜Ε͔Βඞཁͳ΋ͷ͕੝Γͩ͘͞Μʂ IUUQTZPVUVCFG1,.N+Q"(8D U

  13. ˔ ݱঢ়͸ɺςϯιϧͷܭࢉ΍ࣗಈඍ෼౳ͷɺσΟʔϓϥʔχϯάΛ͢Δ ্ͰجຊͱͳΔϏϧσΟϯάϒϩοΫ͕Ͱ͖ͨͱ͜Ζ ˔ 5FOTPSGMPX,FSBTɺ1Z5PSDIɺTDJLJUMFBSOͷΑ͏ͳػցֶशϑϨʔ ϜϫʔΫ͕͋Δͱ͍͍ͳ͋ ˠͦΜͳؾ࣋ͪ΋͋ͬͯࠓճɺࡶʹࢼͯ͠ΈͨΓͨ͠ͷͰͨ͠ ˠʰθϩ͔Β࡞Δ%FFQ-FBSOJOH⁠ʕϑϨʔϜϫʔΫฤʱΛಡΈͳ ͕Βࢼ͠ʹ࡞Γ࢝ΊͯΈ͚ͨͲ͏·͍͜ͱઃܭͰ͖ͳ͍ͯͬͨ͘Μ͋ ͖ΒΊ·ͨ͠ʢؔ਺ܕݴޠ೴ʹͳΓ͖Εͯͳ͍ʜʜʣ

    ࠓޙ΁ͷظ଴