Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- Codetree
- 단어 변환
- XOR
- dfs
- 시뮬레이션
- 구현
- logic gate
- 골드3
- BOJ
- 5373
- 코딩테스트
- and
- 프로그래머스
- Perceptron
- python
- 큐빙
- 미친 아두이노
- Coding Test
- 자율주행자동차
- BFS
- 삼성
- nand
- 파이썬
- 8972
- NOR
- 백준
- Simulation
- or
- ML
- dl
Archives
- Today
- Total
thkyang324
2장 - 퍼셉트론 본문
퍼셉트론
사람의 뇌신경 세포의 동작을 모방하여 만든 수학적 모델
- 다수의 신호를 입력으로 받아 하나의 신호를 출력
- 노드, 가중치, 층과 같은 개념을 통해 딥러닝을 포함한 현대 신경망의 중요한 구성요소의 기본이 됨
입력층과 출력층이란 2개의 층으로 구성되는 단순한 구조로 이루어져 있음.
위 그래프를 수식으로 나타내면
$y = w1x1 + w2x2$
뉴런에서 보내온 신호의 총 합이 일정 수치 이상이면 1, 이하면 0을 출력한다고 하면
$y = \begin{cases}0 (w1x1 + w2x2 \leq \theta)\\1 (w1x1 + w2x2 > \theta)\end{cases}$
$\theta$를 좌변으로 넘겨 $-\theta$를 $b$로 치환하면
$y = \begin{cases}0 (w1x1 + w2x2 + b \leq 0)\\1 (w1x1 + w2x2 + b > 0)\end{cases}$
단순한 논리회로
and, or, nand, nor gate는 단층 퍼셉트론으로 구현이 가능
xor는 2층 퍼셉트론으로 구현이 가능
import numpy as np
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, w1, w2, theta):
self.w1 = w1
self.w2 = w2
self.theta = theta
def __call__(self, x1, x2):
output = int(self.w1*x1 + self.w2*x2 + self.theta > 0)
return output
def simulate(self, x_grid=(-1.5,1.5), y_grid=(-1.5,1.5), figsize=(2,2), n_samples=10):
x_space = np.linspace(*x_grid, n_samples)
y_space = np.linspace(*y_grid, n_samples)
fig = plt.figure(figsize=figsize)
for x in x_space:
for y in y_space:
color = "blue" if self(x,y) else "red"
plt.scatter(x,y,color=color, marker=".", alpha=.2)
for x, y in ((0,0), (0,1), (1,0), (1,1)):
output = self(x,y)
color = "blue" if output else "red"
plt.scatter(x,y,color=color, marker="o", alpha=1)
print(f"x1:{x}\tx2:{y}\t=>\toutput:{output}")
plt.axvline(0, color="black")
plt.axhline(0, color="black")
plt.show()
and_gate = Perceptron(w1=.5, w2=.5, theta=-.7)
nand_gate = Perceptron(w1=-.5, w2=-.5, theta=.7)
or_gate = Perceptron(w1=.5, w2=.5, theta=-0.2)
nor_gate = Perceptron(w1=-.5, w2=-.5, theta=0.2)
and_gate.simulate()
x1:0 x2:0 => output:0
x1:0 x2:1 => output:0
x1:1 x2:0 => output:0
x1:1 x2:1 => output:1
nand_gate.simulate()
x1:0 x2:0 => output:1
x1:0 x2:1 => output:1
x1:1 x2:0 => output:1
x1:1 x2:1 => output:0
or_gate.simulate()
x1:0 x2:0 => output:0
x1:0 x2:1 => output:1
x1:1 x2:0 => output:1
x1:1 x2:1 => output:1
nor_gate.simulate()
x1:0 x2:0 => output:1
x1:0 x2:1 => output:0
x1:1 x2:0 => output:0
x1:1 x2:1 => output:0
class XOR(Perceptron):
def __init__(self):
self.and_gate = Perceptron(w1=.5, w2=.5, theta=-.7)
self.nand_gate = Perceptron(w1=-.5, w2=-.5, theta=.7)
self.or_gate = Perceptron(w1=.5, w2=.5, theta=-0.2)
self.nor_gate = Perceptron(w1=-.5, w2=-.5, theta=0.2)
def __call__(self, x1, x2):
s1 = self.nand_gate(x1, x2)
s2 = self.or_gate(x1, x2)
output = self.and_gate(s1, s2)
return output
xor_gate = XOR()
xor_gate.simulate()
x1:0 x2:0 => output:0
x1:0 x2:1 => output:1
x1:1 x2:0 => output:1
x1:1 x2:1 => output:0
Summary
- 퍼셉트론은 입출력을 가진 알고리즘으로, 입력을 주면 정해진 규칙에 따른 값을 출력함.
- 가중치와 편향을 매개변수로 설정
- AND, OR, NAND, NOR은 단층 퍼셉트론으로 표현이 가능
- XOR은 2층 퍼셉트론으로 표현이 가능
- 단층은 직선형 영역만 표현이 가능하나, 다층은 비선형 영역 또한 표현 가능
- 다층 퍼셉트론으로 (이론상) 컴퓨터를 표현 가능
Comments