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
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
Python
Ruby
--Python vs Ruby "Deep Learning from scratch" Summary --Qiita http://qiita.com/niwasawa/items/b8191f13d6dafbc2fede
Recommended Posts