ゼロから作る Deep Learningを Ruby で

ゼロから作る Deep Learningを Ruby で

ゼロから作る
Deep Learningを
Ruby で

53b6d9a544151a9d620b0003d5fd512f?s=128

niwasawa

May 12, 2017
Tweet

Transcript

  1. θϩ͔Β࡞Δ Deep LearningΛ Ruby Ͱ Naoki Iwasawa @niwasawa

  2. ݸਓతʹࠓ೥͸Ruby Λֶͼ͍ͨͱࢥͬ ͍ͯͨͷͰ͕͢

  3. ػցֶशք۾ ͸ Python ͹͔Γ

  4. θϩ͔Β࡞Δ Deep Learning ͷίʔυ΋ Python

  5. RubyͰॻ͖͍ͨ

  6. ͳͥRubyͰػցֶश ͕ྲྀߦΒͳ͍ͷ͔

  7. ϥΠϒϥϦͱ৘ใ͕ἧͬ ͯͳ͍͔Β (´ʀТʀʆ)

  8. θϩ͔Β࡞ΔDeep Learning Ͱ࢖͍ͬͯΔ ֎෦ϥΠϒϥϦ͸

  9. ਺஋ܭࢉͷ NumPy ͱ άϥϑඳըͷ Matplotlib ͚ͩ

  10. RubyͰ΋͍͚Δ ͷͰ͸

  11. ΍ͬͯΈͨ

  12. θϩ͔Β࡞Δ Deep Learning 1ষ

  13. sinؔ਺ͱcosؔ਺ ͷάϥϑΛඳ͘

  14. Python vs Ruby

  15. import numpy as np import matplotlib matplotlib.use("AGG") # ඳըϥΠϒϥϦʹAGG(Anti-Grain Geometry)Λ࢖͏

    import matplotlib.pyplot as plt # σʔλͷ࡞੒ x = np.arange(0, 6, 0.1) # 0͔Β6·Ͱ0.1ࠁΈͰੜ੒ y1 = np.sin(x) y2 = np.cos(x) # άϥϑͷඳը plt.figure(figsize=(4, 3), dpi=160) # ը૾αΠζ plt.plot(x, y1, label="sin") plt.plot(x, y2, linestyle = "--", label="cos") # ഁઢͰඳը plt.xlabel("x") # x࣠ͷϥϕϧ plt.ylabel("y") # y࣠ͷϥϕϧ plt.title("sin & cos") # λΠτϧ plt.legend() plt.savefig("python_graph.png")
  16. require 'numo/narray' require 'numo/gnuplot' # σʔλͷ࡞੒ x = Numo::DFloat.new(60).seq(0, 0.1)

    # 0͔Β6·Ͱ0.1ࠁΈͰੜ੒ y1 = Numo::DFloat::Math.sin(x) y2 = Numo::DFloat::Math.cos(x) # άϥϑͷඳը g = Numo::gnuplot do set term: {png: {size: [640, 480]}} # ը૾αΠζ set output: 'ruby_graph.png' set title: 'sin \& cos' # λΠτϧ set key: 'box left bottom' set offset: [0, 0, 0, 0] plot x, y1, {w: 'lines', lw: 3, title: 'sin'}, x, y2, {w: 'lines', lw: 3, title: 'cos'} end
  17. Python

  18. Ruby

  19. RubyͰ΋ΘΓͱ ͍͚ͦ͏

  20. Python Ͱ͸ ਺஋ܭࢉ NumPy άϥϑඳը Matplotlib

  21. Ruby Ͱ͸ ਺஋ܭࢉ Numo::NArray άϥϑඳը Numo::Gnuplot

  22. RubyͰػցֶशʹ࢖͑Δ ϥΠϒϥϦ͸σϑΝΫ τελϯμʔυ͕ͳ͍ ͠৘ใ͕গͳ͍

  23. ࠓճ͸৽͠໨ͷϥΠϒ ϥϦ Numo::NArray ͱ Numo::Gnuplot ͕ྑͦ͞͏ ͩͬͨͷͰ࢖ͬͯΈͨ

  24. Numo ެࣜαΠτʹ͸ ʮ!!! under development !!!ʯ ͱॻ͔Ε͍ͯΔ

  25. https://github.com/ruby-numo/numo/blob/master/README.md

  26. ։ൃதʁ ෆ҆ఆʁ (((((ʀƅЧƅ))))) ͜Θ͍

  27. ·͋ಈ͍͍ͯΔ͔ Β͍͍͔ͳɻɻɻ

  28. θϩ͔Β࡞Δ Deep Learning 2ষ

  29. ύʔηϓτϩϯ ʹΑΔ࿦ཧճ࿏

  30. Python vs Ruby

  31. import numpy as np def AND(x1, x2): x = np.array([x1,

    x2]) w = np.array([0.5, 0.5]) # ॏΈ b = -0.7 # όΠΞε tmp = np.sum(w*x) + b if tmp <= 0: return 0 else: return 1
  32. require 'numo/narray' def AND(x1, x2) x = Numo::DFloat[x1, x2] w

    = Numo::DFloat[0.5, 0.5] # ॏΈ b = -0.7 # όΠΞε tmp = (w*x).sum + b if tmp <= 0 0 else 1 end end
  33. ࣮ߦ݁Ռ $ python and_gate.py (0, 0) -> 0 (1, 0)

    -> 0 (0, 1) -> 0 (1, 1) -> 1 $ ruby and_gate.rb [0, 0] -> 0 [1, 0] -> 0 [0, 1] -> 0 [1, 1] -> 1
  34. RubyͰ΋େৎ෉

  35. ࠓ೥͸RubyͰػցֶ श͠·͠ΐ͏( ´∀ʆ)

  36. ࠓճͷιʔείʔυ͸ Qiita Ͱެ։தɻ Python vs Ruby ʰθϩ͔Β࡞ΔDeep Learningʱ 1ষ sinؔ਺ͱcosؔ਺

    ͷάϥϑ - Qiita http://qiita.com/niwasawa/items/6d9aba43f3cdba5ca725 Python vs Ruby ʰθϩ͔Β࡞ΔDeep Learningʱ 2ষ ύʔηϓτϩϯ ʹΑΔ࿦ཧճ࿏ - Qiita http://qiita.com/niwasawa/items/73f604f956d043671f39 Naoki Iwasawa @niwasawa