Python vs Ruby "Deep Learning from scratch" Chapter 3 Graph of step function, sigmoid function, ReLU function

Overview

Write a program to draw graphs of step functions, sigmoid functions, and ReLU functions in Python and Ruby, referring to the code in Chapter 3 of the book "Deep Learning from scratch-The theory and implementation of deep learning learned from Python".

An external library is used for calculation processing and drawing processing. Python uses NumPy and Matplotlib, and Ruby uses Numo :: NArray and Numo :: Gnuplot.

If you need to build an environment, see here. → Python vs Ruby "Deep Learning from scratch" Chapter 1 Graph of sin and cos functions http://qiita.com/niwasawa/items/6d9aba43f3cdba5ca725

Code to draw graphs for step functions, sigmoid functions, and ReLU functions

Python

import numpy as np
import matplotlib
matplotlib.use("AGG") #AGG in drawing library(Anti-Grain Geometry)use
import matplotlib.pyplot as plt

#Step function
def step(x):
  return np.array(x > 0, dtype=np.int)

#Sigmoid function
def sigmoid(x):
  return 1 / (1 + np.exp(-x))

# ReLU (Rectified Linear Unit)function
def relu(x):
  return np.maximum(0, x)

#Data creation
x = np.arange(-5.0, 5.0, 0.1)
y1 = step(x)
y2 = sigmoid(x)
y3 = relu(x)

#Drawing a graph
plt.figure(figsize=(3, 4), dpi=160) #Image size
plt.plot(x, y1, label="Step")
plt.plot(x, y2, label="Sigmoid")
plt.plot(x, y3, label="ReLU")
plt.title("Step, Sigmoid, ReLU")
plt.xlim(-5.5, 5.5) #x-axis range
plt.ylim(-0.2, 5.2) #y-axis range
plt.legend()
plt.savefig("python_graph.png ")

Ruby

require 'numo/narray'
require 'numo/gnuplot'

#Step function
def step(x)
  x > 0 # Numo::Returns Bit
end

#Sigmoid function
def sigmoid(x)
  1 / (1 + Numo::NMath.exp(-x)) # Numo::Returns DFloat
end

# ReLU (Rectified Linear Unit)function
def relu(x)
  y = Numo::DFloat[x] #copy
  y[y < 0] = 0 #Substitute 0 if the value is less than 0
  y
end

#Data creation
x = Numo::DFloat.new(100).seq(-5.0, 0.1)
y1 = step(x)
y2 = sigmoid(x)
y3 = relu(x)

#Drawing a graph
g = Numo::gnuplot do
  set term: {png: {size: [480, 640]}} #Image size
  set output: 'ruby_graph.png'
  set title: 'Step, Sigmoid, ReLU' #title
  set key: 'box left top'
  set xrange: -5.5...5.5 #x-axis range
  set yrange: -0.2...5.2 #y-axis range
  set offset: [0, 0, 0, 0]
  plot x, y1, {w: 'lines', lw: 3, title: 'Step'},
       x, y2, {w: 'lines', lw: 3, title: 'Sigmoid'},
       x, y3, {w: 'lines', lw: 3, title: 'ReLU'}
end

Output image

Python

python_graph.png

Ruby

ruby_graph.png

Reference material

--Python vs Ruby "Deep Learning from scratch" Summary --Qiita http://qiita.com/niwasawa/items/b8191f13d6dafbc2fede

Recommended Posts

Python vs Ruby "Deep Learning from scratch" Chapter 3 Graph of step function, sigmoid function, ReLU function
Python vs Ruby "Deep Learning from scratch" Chapter 4 Implementation of loss function
Python vs Ruby "Deep Learning from scratch" Chapter 1 Graph of sin and cos functions
Python vs Ruby "Deep Learning from scratch" Chapter 3 Implementation of 3-layer neural network
Python vs Ruby "Deep Learning from scratch" Summary
Python vs Ruby "Deep Learning from scratch" Chapter 2 Logic circuit by Perceptron
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
[Deep Learning from scratch] Initial value of neural network weight using sigmoid function
[Deep Learning from scratch] Initial value of neural network weight when using Relu function
Deep learning / Deep learning made from scratch Chapter 6 Memo
[Learning memo] Deep Learning made from scratch [Chapter 5]
[Learning memo] Deep Learning made from scratch [Chapter 6]
Deep learning / Deep learning made from scratch Chapter 7 Memo
Learning record of reading "Deep Learning from scratch"
[Learning memo] Deep Learning made from scratch [~ Chapter 4]
Chapter 1 Introduction to Python Cut out only the good points of deep learning made from scratch
[Deep Learning from scratch] I tried to implement sigmoid layer and Relu layer.
Deep Learning from scratch ① Chapter 6 "Techniques related to learning"
Deep learning / error back propagation of sigmoid function
Deep Learning from scratch Chapter 2 Perceptron (reading memo)
[Learning memo] Deep Learning from scratch ~ Implementation of Dropout ~
Application of Deep Learning 2 made from scratch Spam filter
Deep Learning from scratch 1-3 chapters
An amateur stumbled in Deep Learning from scratch Note: Chapter 1
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 5
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 2
An amateur stumbled in Deep Learning from scratch Note: Chapter 3
An amateur stumbled in Deep Learning from scratch Note: Chapter 7
An amateur stumbled in Deep Learning from scratch Note: Chapter 5
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 7
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 1
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 4
An amateur stumbled in Deep Learning from scratch Note: Chapter 4
Summary of activation functions (step, sigmoid, ReLU, softmax, identity function)
An amateur stumbled in Deep Learning from scratch Note: Chapter 2
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 6
Chapter 3 Neural Network Cut out only the good points of deep learning made from scratch
"Deep Learning from scratch" Self-study memo (Part 8) I drew the graph in Chapter 6 with matplotlib
Chapter 2 Implementation of Perceptron Cut out only the good points of deep learning made from scratch
Deep Learning / Deep Learning from Zero 2 Chapter 4 Memo
Deep Learning / Deep Learning from Zero Chapter 3 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 5 Memo
Deep learning from scratch (cost calculation)
Deep Learning / Deep Learning from Zero 2 Chapter 7 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 8 Memo
Write an impression of Deep Learning 3 framework edition made from scratch
Deep Learning / Deep Learning from Zero Chapter 5 Memo
Deep Learning / Deep Learning from Zero Chapter 4 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 3 Memo
Deep Learning memos made from scratch
"Deep Learning from scratch" Self-study memo (No. 10-2) Initial value of weight
Deep Learning / Deep Learning from Zero 2 Chapter 6 Memo
[Deep Learning from scratch] Layer implementation from softmax function to cross entropy error
Build a "Deep learning from scratch" learning environment on Cloud9 (jupyter miniconda python3)
Deep learning from scratch (forward propagation edition)
Learning notes from the beginning of Python 1
Deep learning / Deep learning from scratch 2-Try moving GRU
"Deep Learning from scratch" in Haskell (unfinished)
[Windows 10] "Deep Learning from scratch" environment construction
Learning notes from the beginning of Python 2
[Deep Learning from scratch] About hyperparameter optimization