Logic circuit by perceptron in Python and Ruby with reference to the code in Chapter 2 of the book "Deep Learning from scratch-The theory and implementation of deep learning learned with Python" Implement (AND gate, NAND gate, OR gate, XOR gate).
An external library is used in the calculation process. Use NumPy for Python and Numo :: NArray for Ruby.
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
Weights and biases are manually derived values.
Python
and_gate.py
import numpy as np
def AND(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5]) #weight
b = -0.7 #bias
tmp = np.sum(w*x) + b
if tmp <= 0:
return 0
else:
return 1
if __name__ == '__main__':
for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
y = AND(xs[0], xs[1])
print(str(xs) + " -> " + str(y))
Ruby
and_gate.rb
require 'numo/narray'
def AND(x1, x2)
x = Numo::DFloat[x1, x2]
w = Numo::DFloat[0.5, 0.5] #weight
b = -0.7 #bias
tmp = (w*x).sum + b
if tmp <= 0
0
else
1
end
end
if __FILE__ == $0
for xs in [[0, 0], [1, 0], [0, 1], [1, 1]]
y = AND(xs[0], xs[1])
puts "#{xs} -> #{y}"
end
end
Compare the execution results of Python and Ruby. If the inputs have the same value, it can be seen that the output also has the same value.
$ 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
NAND gate implementations differ from AND gates only in weight and bias.
Python
nand_gate.py
import numpy as np
def NAND(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
if __name__ == '__main__':
for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
y = NAND(xs[0], xs[1])
print(str(xs) + " -> " + str(y))
Ruby
nand_gate.rb
require 'numo/narray'
def NAND(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
if __FILE__ == $0
for xs in [[0, 0], [1, 0], [0, 1], [1, 1]]
y = NAND(xs[0], xs[1])
puts "#{xs} -> #{y}"
end
end
$ python nand_gate.py
(0, 0) -> 1
(1, 0) -> 1
(0, 1) -> 1
(1, 1) -> 0
$ ruby nand_gate.rb
[0, 0] -> 1
[1, 0] -> 1
[0, 1] -> 1
[1, 1] -> 0
The OR gate implementation also differs only in weight and bias values.
Python
or_gate.py
import numpy as np
def OR(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
b = -0.2
tmp = np.sum(w*x) + b
if tmp <= 0:
return 0
else:
return 1
if __name__ == '__main__':
for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
y = OR(xs[0], xs[1])
print(str(xs) + " -> " + str(y))
Ruby
or_gate.rb
require 'numo/narray'
def OR(x1, x2)
x = Numo::DFloat[x1, x2]
w = Numo::DFloat[0.5, 0.5]
b = -0.2
tmp = (w*x).sum + b
if tmp <= 0
0
else
1
end
end
if __FILE__ == $0
for xs in [[0, 0], [1, 0], [0, 1], [1, 1]]
y = OR(xs[0], xs[1])
puts "#{xs} -> #{y}"
end
end
$ python or_gate.py
(0, 0) -> 0
(1, 0) -> 1
(0, 1) -> 1
(1, 1) -> 1
$ ruby or_gate.rb
[0, 0] -> 0
[1, 0] -> 1
[0, 1] -> 1
[1, 1] -> 1
The XOR gate implementation uses a multi-layer perceptron with NAND, OR, and AND gates stacked.
Python
xor_gate.py
from and_gate import AND
from or_gate import OR
from nand_gate import NAND
def XOR(x1, x2):
s1 = NAND(x1, x2)
s2 = OR(x1, x2)
y = AND(s1, s2)
return y
if __name__ == '__main__':
for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
y = XOR(xs[0], xs[1])
print(str(xs) + " -> " + str(y))
Ruby
xor_gate.rb
require './and_gate'
require './or_gate'
require './nand_gate'
def XOR(x1, x2)
s1 = NAND(x1, x2)
s2 = OR(x1, x2)
y = AND(s1, s2)
return y
end
if __FILE__ == $0
for xs in [[0, 0], [1, 0], [0, 1], [1, 1]]
y = XOR(xs[0], xs[1])
puts "#{xs} -> #{y}"
end
end
$ python xor_gate.py
(0, 0) -> 0
(1, 0) -> 1
(0, 1) -> 1
(1, 1) -> 0
$ ruby xor_gate.rb
[0, 0] -> 0
[1, 0] -> 1
[0, 1] -> 1
[1, 1] -> 0
--Python vs Ruby "Deep Learning from scratch" Summary --Qiita http://qiita.com/niwasawa/items/b8191f13d6dafbc2fede
Recommended Posts