#
๐ Perceptron์ด๋?
- 1957๋ ํ๋ญํฌ ๋ก์ ๋ธ๋ผํธ๊ฐ ๊ณ ์ํ ์ต์ด์ ์ธ๊ณต์ ๊ฒฝ๋ง ๋ชจ๋ธ ์ค ํ๋.
- ์๋ฌผํ์ ๋ด๋ฐ์ ์ํ์ ์ผ๋ก ๋ชจ๋ธ๋งํ ์ธ๊ณต ๋ด๋ฐ์ผ๋ก, ์ฌ๋ฌ ์ ๋ ฅ ์ ํธ๋ฅผ ๋ฐ์ ๊ฐ๊ฐ์ ๊ฐ์ค์น๋ฅผ ๊ณฑํ ํ ์ด๋ฅผ ํฉ์ฐํ์ฌ, ํ์ฑํ ํจ์๋ฅผ ํตํด ๋จ์ผ ์ ํธ๋ฅผ ์ถ๋ ฅํ๋ค.
- ํผ์ ํธ๋ก ์ ์ถ๋ ฅ์ ์ ํธ ์ ๋ฌด (1 ๋๋ 0)๋ก ํํ๋๊ณ , ์ด์ง ๋ถ๋ฅ ๋ฌธ์ ํด๊ฒฐ์ ํจ๊ณผ์ ์ด๋ค.
๐ Perceptron AND_GATE ์ค์ต
๐ AND ๊ฒ์ดํธ ๋ชจ๋ธ ํ๋ จ ํ ๊ฒฐ๊ณผ ํ์ธ
import numpy as np
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, input_size, lr=0.1, epochs=10):
self.weights = np.zeros(input_size)
self.bias = 0
self.lr = lr
self.epochs = epochs
self.errors = []
def activation(self, x):
return np.where(x >= 0, 1, 0)
def predict(self, x):
linear_output = np.dot(x, self.weights) + self.bias
return self.activation(linear_output)
def train(self, X, y):
for epoch in range(self.epochs):
total_error = 0
for x1, target in zip(X, y):
prediction = self.predict(x1)
update = self.lr * (target - prediction)
self.weights += update * x1
self.bias += update
total_error += int(update != 0)
self.errors.append(total_error)
print(f"Epoch {epoch+1}/{self.epochs}, Errors: {total_error}")
# AND ๊ฒ์ดํธ ๋ฐ์ดํฐ
X_and = np.array([[0,0],[0,1],[1,0],[1,1]])
y_and = np.array([0,0,0,1])
# ํผ์
ํธ๋ก ๋ชจ๋ธ ํ๋ จ
ppn_and = Perceptron(input_size=2)
ppn_and.train(X_and, y_and)
# ์์ธก ๊ฒฐ๊ณผ ํ์ธ
print("\nAND Gate Test:")
for x in X_and:
print(f"Input: {x}, Predicted Output: {ppn_and.predict(x)}")
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ AND ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ
from matplotlib.colors import ListedColormap
def plot_decision_boundary(X, y, model):
cmap_light = ListedColormap(['#FFAAAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#0000FF'])
h = .02 # mesh grid ๊ฐ๊ฒฉ
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, Z, cmap=cmap_light)
# ์ค์ ๋ฐ์ดํฐ ํฌ์ธํธ ํ์
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
edgecolor='k', s=100, marker='o')
plt.xlabel('Input 1')
plt.ylabel('Input 2')
plt.title('Perceptron Decision Boundary')
plt.show()
# AND ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ
plot_decision_boundary(X_and, y_and, ppn_and)
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ ์ค๋ฅ ์๊ฐํ
plt.figure(figsize=(8, 5))
plt.plot(range(1, len(ppn_and.errors) + 1), ppn_and.errors, marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of Errors')
plt.title('Perceptron Learning Error Over Epochs (AND Gate)')
plt.grid(True)
plt.show()
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ Perceptron OR_GATE ์ค์ต
๐ OR ๊ฒ์ดํธ ๋ชจ๋ธ ํ๋ จ ํ ๊ฒฐ๊ณผ ํ์ธ
import numpy as np
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, input_size, lr=0.1, epochs=10):
self.weights = np.zeros(input_size)
self.bias = 0
self.lr = lr
self.epochs = epochs
self.errors = []
def activation(self, x):
return np.where(x >= 0, 1, 0)
def predict(self, x):
linear_output = np.dot(x, self.weights) + self.bias
return self.activation(linear_output)
def train(self, X, y):
for epoch in range(self.epochs):
total_error = 0
for x1, target in zip(X, y):
prediction = self.predict(x1)
update = self.lr * (target - prediction)
self.weights += update * x1
self.bias += update
total_error += int(update != 0)
self.errors.append(total_error)
print(f"Epoch {epoch+1}/{self.epochs}, Errors: {total_error}")
# OR ๊ฒ์ดํธ ๋ฐ์ดํฐ
X_or = np.array([[0,0],[0,1],[1,0],[1,1]])
y_or = np.array([0,1,1,1])
# ํผ์
ํธ๋ก ๋ชจ๋ธ ํ๋ จ
ppn_or = Perceptron(input_size=2)
ppn_or.train(X_or, y_or)
# ์์ธก ๊ฒฐ๊ณผ ํ์ธ
print("\nOR Gate Test:")
for x in X_or:
print(f"Input: {x}, Predicted Output: {ppn_or.predict(x)}")
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ OR ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ
from matplotlib.colors import ListedColormap
def plot_decision_boundary(X, y, model):
cmap_light = ListedColormap(['#FFAAAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#0000FF'])
h = .02 # mesh grid ๊ฐ๊ฒฉ
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, Z, cmap=cmap_light)
# ์ค์ ๋ฐ์ดํฐ ํฌ์ธํธ ํ์
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
edgecolor='k', s=100, marker='o')
plt.xlabel('Input 1')
plt.ylabel('Input 2')
plt.title('Perceptron Decision Boundary')
plt.show()
# OR ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ
plot_decision_boundary(X_or, y_or, ppn_or)
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ ์ค๋ฅ ์๊ฐํ
plt.figure(figsize=(8, 5))
plt.plot(range(1, len(ppn_or.errors) + 1), ppn_or.errors, marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of Errors')
plt.title('Perceptron Learning Error Over Epochs (OR Gate)')
plt.grid(True)
plt.show()
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ Perceptron NAND_GATE ์ค์ต
๐ NAND ๊ฒ์ดํธ ๋ชจ๋ธ ํ๋ จ ํ ๊ฒฐ๊ณผ ํ์ธ
import numpy as np
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, input_size, lr=0.1, epochs=10):
self.weights = np.zeros(input_size)
self.bias = 0
self.lr = lr
self.epochs = epochs
self.errors = []
def activation(self, x):
return np.where(x >= 0, 1, 0)
def predict(self, x):
linear_output = np.dot(x, self.weights) + self.bias
return self.activation(linear_output)
def train(self, X, y):
for epoch in range(self.epochs):
total_error = 0
for x1, target in zip(X, y):
prediction = self.predict(x1)
update = self.lr * (target - prediction)
self.weights += update * x1
self.bias += update
total_error += int(update != 0)
self.errors.append(total_error)
print(f"Epoch {epoch+1}/{self.epochs}, Errors: {total_error}")
# NAND ๊ฒ์ดํธ ๋ฐ์ดํฐ
X_nand = np.array([[0,0],[0,1],[1,0],[1,1]])
y_nand = np.array([1,1,1,0])
# ํผ์
ํธ๋ก ๋ชจ๋ธ ํ๋ จ
ppn_nand = Perceptron(input_size=2)
ppn_nand.train(X_nand, y_nand)
# ์์ธก ๊ฒฐ๊ณผ ํ์ธ
print("\nNAND Gate Test:")
for x in X_nand:
print(f"Input: {x}, Predicted Output: {ppn_nand.predict(x)}")
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ NAND ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ
from matplotlib.colors import ListedColormap
def plot_decision_boundary(X, y, model):
cmap_light = ListedColormap(['#FFAAAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#0000FF'])
h = .02 # mesh grid ๊ฐ๊ฒฉ
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, Z, cmap=cmap_light)
# ์ค์ ๋ฐ์ดํฐ ํฌ์ธํธ ํ์
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
edgecolor='k', s=100, marker='o')
plt.xlabel('Input 1')
plt.ylabel('Input 2')
plt.title('Perceptron Decision Boundary')
plt.show()
# NAND ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ
plot_decision_boundary(X_nand, y_nand, ppn_nand)
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ ์ค๋ฅ ์๊ฐํ
plt.figure(figsize=(8, 5))
plt.plot(range(1, len(ppn_or.errors) + 1), ppn_or.errors, marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of Errors')
plt.title('Perceptron Learning Error Over Epochs (OR Gate)')
plt.grid(True)
plt.show()
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ Perceptron XOR_GATE ์ค์ต
๐ XOR ๊ฒ์ดํธ ๋ชจ๋ธ ํ๋ จ ํ ๊ฒฐ๊ณผ ํ์ธ
import numpy as np
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, input_size, lr=0.1, epochs=10):
self.weights = np.zeros(input_size)
self.bias = 0
self.lr = lr
self.epochs = epochs
self.errors = []
def activation(self, x):
return np.where(x >= 0, 1, 0)
def predict(self, x):
linear_output = np.dot(x, self.weights) + self.bias
return self.activation(linear_output)
def train(self, X, y):
for epoch in range(self.epochs):
total_error = 0
for x1, target in zip(X, y):
prediction = self.predict(x1)
update = self.lr * (target - prediction)
self.weights += update * x1
self.bias += update
total_error += int(update != 0)
self.errors.append(total_error)
print(f"Epoch {epoch+1}/{self.epochs}, Errors: {total_error}")
# XOR ๊ฒ์ดํธ ๋ฐ์ดํฐ
X_xor = np.array([[0,0],[0,1],[1,0],[1,1]])
y_xor = np.array([0,1,1,0])
# ํผ์
ํธ๋ก ๋ชจ๋ธ ํ๋ จ
ppn_xor = Perceptron(input_size=2)
ppn_xor.train(X_xor, y_xor)
# ์์ธก ๊ฒฐ๊ณผ ํ์ธ
print("\nXOR Gate Test:")
for x in X_xor:
print(f"Input: {x}, Predicted Output: {ppn_xor.predict(x)}")
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ XOR ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ
from matplotlib.colors import ListedColormap
def plot_decision_boundary(X, y, model):
cmap_light = ListedColormap(['#FFAAAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#0000FF'])
h = .02 # mesh grid ๊ฐ๊ฒฉ
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, Z, cmap=cmap_light)
# ์ค์ ๋ฐ์ดํฐ ํฌ์ธํธ ํ์
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
edgecolor='k', s=100, marker='o')
plt.xlabel('Input 1')
plt.ylabel('Input 2')
plt.title('Perceptron Decision Boundary')
plt.show()
# XOR ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ
plot_decision_boundary(X_xor, y_xor, ppn_xor)
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ ์ค๋ฅ ์๊ฐํ
plt.figure(figsize=(8, 5))
plt.plot(range(1, len(ppn_or.errors) + 1), ppn_or.errors, marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of Errors')
plt.title('Perceptron Learning Error Over Epochs (OR Gate)')
plt.grid(True)
plt.show()
๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐จ ๋จ์ธต Perceptron์ ํ๊ณ์
- XOR GATE๋ ํผ์ ํธ๋ก ์ผ๋ก ํ์ต์ด ๋์ง ์๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์๋ค.
- ํผ์ ํธ๋ก ์ ์ง์ ํ๋๋ก โ์ โ๋ฅผ ๋๋ ์ ์์ด์ผ ํ์ต์ด ๋๋ค.
- ํ์ง๋ง XOR์ ์ง์ ํ๋๋ก๋ ์ ๋ โ์ โ๋ฅผ ๋๋ ์ ์๋ค.