Help us understand the problem. What is going on with this article?

パーセプトロンで論理回路を作る(多層パーセプトロン)

More than 1 year has passed since last update.

本記事は、ゼロから作るDeep Learningの学習メモです。  

要点

  • パーセプトロンとは、複数の信号を入力として受取り、一つの信号を出力するアルゴリズム。
    「バイアス」と「重み」をパラメータとして設定し、ある入力を与えると決まった値が出力される。
    「バイアス」は発火のしやすさを制御するパラメータ。
    「重み」は各入力の重要性を制御するパラメータ。
  • 論理回路のAND素子、NAND素子、OR素子について、(単層)パーセプトロンで表現することが出来る。
  • XOR素子はAND素子、NAND素子、OR素子を組み合わせて作成できる。
    →単層で表現できなかったものが、多層パーセプトロンで表現可能になる。

パーセプトロンを表す式

\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}

※x1,x2:入力 w1,w2:重み b: バイアス

パーセプトロンで論理回路を実装する

perceptron.py
import numpy as np

# x1,x2:入力 w1,w2:重み b:バイアス
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):
    # 多層パーセプトロン
    s1 = NAND(x1,x2)
    s2 = OR(x1,x2)
    y = AND(s1,s2)
    return y

# 以下動作確認
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
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away