Make a logic circuit with a perceptron (multilayer perceptron)

This article is a learning memo of Deep Learning from scratch.

The point

--Perceptron is an algorithm that receives multiple signals as inputs and outputs one signal. When "bias" and "weight" are set as parameters and a certain input is given, a fixed value is output. "Bias" is a parameter that controls the ease of ignition. "Weight" is a parameter that controls the importance of each input. --The AND element, NAND element, and OR element of a logic circuit can be expressed by a (single layer) perceptron. --The XOR element can be created by combining an AND element, a NAND element, and an OR element. → What could not be expressed with a single layer can now be expressed with a multi-layer perceptron.

Expression for perceptron

\begin{eqnarray}
y=\left\{ \begin{array}{ll}
0 & (b + w_1 x_1 + w_2 x_2 \leqq 0) \\
1 & (b + w_1 x_1 + w_2 x_2 \gt 0) \\
\end{array} \right.
\end{eqnarray}

Implement logic circuits with perceptron

perceptron.py


import numpy as np

# x1,x2:Input w1,w2:Weight b:bias
def perceptron(x1,x2,w1,w2,b):
    x = np.array([x1, x2])
    w = np.array([w1, w2])
    tmp = np.sum(w*x) + b
    return 0 if tmp <= 0 else 1

def AND(x1,x2):
    return perceptron(x1,x2,0.5,0.5,-0.7)

def NAND(x1,x2):
    return perceptron(x1,x2,-0.5,-0.5,0.7)

def OR(x1,x2):
    return perceptron(x1,x2,0.5,0.5,0.0)

def XOR(x1,x2):
    #Multilayer perceptron
    s1 = NAND(x1,x2)
    s2 = OR(x1,x2)
    y = AND(s1,s2)
    return y

#Check the operation below
print("AND")
print(AND(0,0)) # 0
print(AND(0,1)) # 0
print(AND(1,0)) # 0
print(AND(1,1)) # 1

print("NAND")
print(NAND(0,0)) # 1
print(NAND(0,1)) # 1
print(NAND(1,0)) # 1
print(NAND(1,1)) # 0

print("OR")
print(OR(0,0)) # 0
print(OR(0,1)) # 1
print(OR(1,0)) # 1
print(OR(1,1)) # 1

print("XOR")
print(XOR(0,0)) # 0
print(XOR(0,1)) # 1
print(XOR(1,0)) # 1
print(XOR(1,1)) # 0

Recommended Posts

Make a logic circuit with a perceptron (multilayer perceptron)
Make a fortune with Python
Make a fire with kdeplot
Let's make a GUI with python.
Make a sound with Jupyter notebook
Multilayer Perceptron with Chainer: Function Fitting
Let's make a breakout with wxPython
[Chainer] Learning XOR with multi-layer perceptron
Make a recommender system with python
Make a filter with a django template
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Make a model iterator with PySide
Make a nice graph with plotly
Let's make a shiritori game with Python
Make a video player with PySimpleGUI + OpenCV
Make a rare gacha simulator with Flask
Make a Notebook Pipeline with Kedro + Papermill
Make a drawing quiz with kivy + PyTorch
Let's make a voice slowly with Python
Make a cascade classifier with google colaboratory
Let's make a simple language with PLY 1
Make a Yes No Popup with Kivy
Make a wash-drying timer with a Raspberry Pi
Make a GIF animation with folder monitoring
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
A memorandum to make WebDAV only with nginx
Investment quest: Make a system trade with pyhton (2)
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
Make a monitoring device with an infrared sensor
Make a simple pixel art generator with Flask
Investment quest: Make a system trade with pyhton (1)
How to make a dictionary with a hierarchical structure.
I want to make a game with Python
[Python] Make a simple maze game with Pyxel
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
[Chat De Tornado] Make a chat using WebSocket with Tornado
Make holiday data into a data frame with pandas
Make a LINE WORKS bot with Amazon Lex
MNIST (handwritten digit) image classification with multi-layer perceptron
(Memorandum) Make a 3D scatter plot with matplodlib
Make one repeating string with a Python regular expression.
Stock Price Forecast with TensorFlow (Multilayer Perceptron: MLP) ~ Stock Forecast Part 2 ~
Try to make a command standby tool with python
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
Make a simple Slackbot with interactive button in python
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
Make a breakpoint on the c layer with python
Make a function to describe Japanese fonts with OpenCV
Let's make dependency management with pip a little easier
Make a CSV formatting tool with Python Pandas PyInstaller
Let's make a Mac app with Tkinter and py2app
Let's make a spherical grid with Rhinoceros / Grasshopper / GHPython