π Perceptronλ?
νΌμ νΈλ‘ (Perceptron)μ μλ¬Όνμ λ΄λ°μ μνμ μΌλ‘ λͺ¨λΈλ§ν μΈκ³΅ λ΄λ° λͺ¨λΈλ‘, μ¬λ¬ μ λ ₯ μ νΈλ₯Ό λ°μ κ° μ λ ₯μ λν κ°μ€μΉ(Weight)λ₯Ό κ³±ν ν, μ΄λ€μ κ°μ€ν©(Weighted Sum)μ κ³μ°νκ³ , νμ±ν ν¨μ(Activation Function)λ₯Ό ν΅ν΄ μ΅μ’ μΆλ ₯μ κ²°μ νλ ꡬ쑰μ΄λ€.
π§ ꡬ쑰 (Perceptron Structure)
μ
λ ₯(x) β κ°μ€μΉ(w) β κ°μ€ν©(β) β νμ±ν ν¨μ(f) β μΆλ ₯(y)
- μ λ ₯ (Input): AND, OR λ± λ Όλ¦¬ μ°μ°μ μν μ λ ₯ μ νΈ.
- κ°μ€μΉ (Weight): μ λ ₯ μ νΈμ μ€μλλ₯Ό κ²°μ νλ©°, νμ΅μ ν΅ν΄ μ‘°μ λ¨.
- κ°μ€ν© (Weighted Sum): κ° μ λ ₯κ³Ό κ·Έμ λμνλ κ°μ€μΉμ κ³±μ λͺ¨λ λν κ°.
- νμ±ν ν¨μ (Activation Function): κ°μ€ν©μ΄ μκ³κ°μ λμΌλ©΄ 1, λμ§ λͺ»νλ©΄ 0μ μΆλ ₯νλ ν¨μ. λνμ μΌλ‘ λ¨μ κ³λ¨ ν¨μ μ¬μ©.
- μΆλ ₯ (Output): μ΅μ’ κ²°κ³Όκ° (λ³΄ν΅ 0 λλ 1μ μ΄μ§ μΆλ ₯).
π― μμ½
- νΌμ νΈλ‘ μ μ΄μ§ λΆλ₯ λ¬Έμ λ₯Ό ν΄κ²°ν μ μλ κ°μ₯ κΈ°λ³Έμ μΈ μ κ²½λ§ κ΅¬μ‘°μ΄λ€.
- νμ΅μ ν΅ν΄ μ λ ₯ μ νΈμ μ€μλλ₯Ό λνλ΄λ κ°μ€μΉκ° μ‘°μ λλ€.
- λ¨μΈ΅ νΌμ νΈλ‘ μ μ ν λΆλ¦¬ κ°λ₯ν λ¬Έμ λ§ ν΄κ²°ν μ μλ€.
π¨βπ» μ€μ΅
π‘ Code : AND & OR & NAND & XOR Gate Perceptron
# AND & OR & NAND & XOR Gate Perceptron
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 xi, target in zip(X, y):
prediction = self.predict(xi)
update = self.lr * (target - prediction)
self.weights += update * xi
self.bias += update
total_error += int(update != 0.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])
print(" AND Gate Training")
ppn_and = Perceptron(input_size=2)
ppn_and.train(X_and, y_and)
print("\n AND Gate Test:")
for x in X_and:
print(f"Input: {x}, Predicted Output: {ppn_and.predict(x)}")
# OR κ²μ΄νΈ λ°μ΄ν° λ° νμ΅
X_or = np.array([[0,0],[0,1],[1,0],[1,1]])
y_or = np.array([0,1,1,1])
print("\n OR Gate Training")
ppn_or = Perceptron(input_size=2)
ppn_or.train(X_or, y_or)
print("\n OR Gate Test:")
for x in X_or:
print(f"Input: {x}, Predicted Output: {ppn_or.predict(x)}")
# NAND κ²μ΄νΈ λ°μ΄ν° λ° νμ΅
X_nand = np.array([[0,0],[0,1],[1,0],[1,1]])
y_nand = np.array([1,1,1,0]) # ANDμ λ°λ
print("\n NAND Gate Training")
ppn_nand = Perceptron(input_size=2)
ppn_nand.train(X_nand, y_nand)
print("\n NAND Gate Test:")
for x in X_nand:
print(f"Input: {x}, Predicted Output: {ppn_nand.predict(x)}")
# XOR κ²μ΄νΈ λ°μ΄ν° λ° νμ΅
X_xor = np.array([[0,0],[0,1],[1,0],[1,1]])
y_xor = np.array([0,1,1,0]) # μ ν λΆλ¦¬ λΆκ°λ₯
print("\n XOR Gate Training")
ppn_xor = Perceptron(input_size=2)
ppn_xor.train(X_xor, y_xor)
print("\n XOR Gate Test:")
for x in X_xor:
print(f"Input: {x}, Predicted Output: {ppn_xor.predict(x)}")
β Result : AND & OR & NAND & XOR Gate Perceptron
AND Gate Training
Epoch 1/10, Errors: 1
Epoch 2/10, Errors: 3
Epoch 3/10, Errors: 3
Epoch 4/10, Errors: 2
Epoch 5/10, Errors: 1
Epoch 6/10, Errors: 0
Epoch 7/10, Errors: 0
Epoch 8/10, Errors: 0
Epoch 9/10, Errors: 0
Epoch 10/10, Errors: 0
AND Gate Test:
Input: [0 0], Predicted Output: 0
Input: [0 1], Predicted Output: 0
Input: [1 0], Predicted Output: 0
Input: [1 1], Predicted Output: 1
OR Gate Training
Epoch 1/10, Errors: 1
Epoch 2/10, Errors: 2
Epoch 3/10, Errors: 1
Epoch 4/10, Errors: 0
Epoch 5/10, Errors: 0
Epoch 6/10, Errors: 0
Epoch 7/10, Errors: 0
Epoch 8/10, Errors: 0
Epoch 9/10, Errors: 0
Epoch 10/10, Errors: 0
OR Gate Test:
Input: [0 0], Predicted Output: 0
Input: [0 1], Predicted Output: 1
Input: [1 0], Predicted Output: 1
Input: [1 1], Predicted Output: 1
NAND Gate Training
Epoch 1/10, Errors: 2
Epoch 2/10, Errors: 3
Epoch 3/10, Errors: 3
Epoch 4/10, Errors: 0
Epoch 5/10, Errors: 0
Epoch 6/10, Errors: 0
Epoch 7/10, Errors: 0
Epoch 8/10, Errors: 0
Epoch 9/10, Errors: 0
Epoch 10/10, Errors: 0
NAND Gate Test:
Input: [0 0], Predicted Output: 1
Input: [0 1], Predicted Output: 1
Input: [1 0], Predicted Output: 1
Input: [1 1], Predicted Output: 0
XOR Gate Training
Epoch 1/10, Errors: 2
Epoch 2/10, Errors: 3
Epoch 3/10, Errors: 4
Epoch 4/10, Errors: 4
Epoch 5/10, Errors: 4
Epoch 6/10, Errors: 4
Epoch 7/10, Errors: 4
Epoch 8/10, Errors: 4
Epoch 9/10, Errors: 4
Epoch 10/10, Errors: 4
XOR Gate Test:
Input: [0 0], Predicted Output: 1
Input: [0 1], Predicted Output: 1
Input: [1 0], Predicted Output: 0
Input: [1 1], Predicted Output: 0
π‘ Code : κ²½κ³ κ²°μ μκ°ν ν¨μ (AND, OR, NAND, XOR)
# κ²½κ³ κ²°μ μκ°ν ν¨μ (AND, OR, NAND, XOR)
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
import numpy as np
def plot_decision_boundary(X, y, model, title='Perceptron Decision Boundary'):
cmap_light = ListedColormap(['#FFDDDD', '#DDDDFF']) # λ°°κ²½ μμ
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=(6, 5))
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(title)
plt.grid(True)
plt.show()
# AND κ²μ΄νΈ κ²°μ κ²½κ³ μκ°ν
plot_decision_boundary(X_and, y_and, ppn_and, title='AND Gate Decision Boundary')
# OR κ²μ΄νΈ κ²°μ κ²½κ³ μκ°ν
plot_decision_boundary(X_or, y_or, ppn_or, title='OR Gate Decision Boundary')
# NAND κ²μ΄νΈ κ²°μ κ²½κ³ μκ°ν
plot_decision_boundary(X_nand, y_nand, ppn_nand, title='NAND Gate Decision Boundary')
# XOR κ²μ΄νΈ κ²°μ κ²½κ³ μκ°ν
plot_decision_boundary(X_xor, y_xor, ppn_xor, title='XOR Gate Decision Boundary')
β Result : κ²½κ³ κ²°μ μκ°ν ν¨μ (AND, OR, NAND, XOR)
π‘ Code : # μ€λ₯ μκ°ν (AND, OR, NAND, XOR)
# μ€λ₯ μκ°ν (AND, OR, NAND, XOR)
plt.figure(figsize=(8, 5))
plt.plot(range(1, len(ppn_and.errors) + 1), ppn_and.errors, marker='o', label='AND Gate')
plt.plot(range(1, len(ppn_or.errors) + 1), ppn_or.errors, marker='s', label='OR Gate')
plt.plot(range(1, len(ppn_nand.errors) + 1), ppn_nand.errors, marker='^', label='NAND Gate')
plt.plot(range(1, len(ppn_xor.errors) + 1), ppn_xor.errors, marker='x', label='XOR Gate')
plt.xlabel('Epochs')
plt.ylabel('Number of Errors')
plt.title('Perceptron Learning Error Over Epochs')
plt.legend()
plt.grid(True)
plt.show()
β Result : μ€λ₯ μκ°ν (AND, OR, NAND, XOR)
π¬ Comment
- νΌμ νΈλ‘ : μ λ ₯ 벑ν°μ κ°μ€μΉλ₯Ό κ³±ν ν©μ΄ κΈ°μ€(0)μ λλμ§ νλ¨νκ³ , νμ΅ κ³Όμ μμλ νλ¦° λ§νΌλ§ μ‘°μ νλ©° μ ν λΆλ¦¬λ₯Ό λ°°μ°λ ꡬ쑰
- XORμ μ ν λΆλ¦¬ λΆκ°λ₯ν λ¬Έμ μ΄κΈ° λλ¬Έμ
λ¨μΈ΅ νΌμ νΈλ‘ μΌλ‘λ ν΄κ²°ν μ μλ€. - μ΄λ₯Ό ν΄κ²°νλ €λ©΄ λ€μΈ΅ νΌμ νΈλ‘ (MLP)μ΄λ λΉμ ν λ³νμ΄ νμνλ€.
π‘ Code : MLPλ‘ XOR λ¬Έμ ν΄κ²°
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
class MultiLayerPerceptron:
def __init__(self, input_size=2, hidden_size=4, output_size=1, lr=0.5, epochs=1000):
self.W1 = np.random.uniform(-1, 1, (input_size, hidden_size))
self.b1 = np.zeros((1, hidden_size))
self.W2 = np.random.uniform(-1, 1, (hidden_size, output_size))
self.b2 = np.zeros((1, output_size))
self.lr = lr
self.epochs = epochs
self.losses = []
def sigmoid(self, x):
return 1 / (1 + np.exp(-np.clip(x, -250, 250)))
def sigmoid_derivative(self, x):
return x * (1 - x)
def forward(self, X):
self.z1 = np.dot(X, self.W1) + self.b1
self.a1 = self.sigmoid(self.z1)
self.z2 = np.dot(self.a1, self.W2) + self.b2
self.a2 = self.sigmoid(self.z2)
return self.a2
def backward(self, X, y, output):
m = X.shape[0]
dZ2 = output - y
dW2 = (1 / m) * np.dot(self.a1.T, dZ2)
db2 = (1 / m) * np.sum(dZ2, axis=0, keepdims=True)
dZ1 = np.dot(dZ2, self.W2.T) * self.sigmoid_derivative(self.a1)
dW1 = (1 / m) * np.dot(X.T, dZ1)
db1 = (1 / m) * np.sum(dZ1, axis=0, keepdims=True)
self.W2 -= self.lr * dW2
self.b2 -= self.lr * db2
self.W1 -= self.lr * dW1
self.b1 -= self.lr * db1
def train(self, X, y):
for epoch in range(self.epochs):
output = self.forward(X)
loss = np.mean((output - y) ** 2)
self.losses.append(loss)
self.backward(X, y, output)
#if epoch % 200 == 0:
# print(f"Epoch {epoch}/{self.epochs}, Loss: {loss:.6f}")
def predict(self, X):
output = self.forward(X)
return (output > 0.5).astype(int)
def predict_prob(self, X):
return self.forward(X).ravel() # κ²°μ κ²½κ³μ©
# === XOR λ°μ΄ν° ===
X_xor = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_xor = np.array([[0], [1], [1], [0]])
# === νμ΅ ===
print("\n=== XOR Gate Multi-Layer Perceptron Training ===")
mlp = MultiLayerPerceptron(input_size=2, hidden_size=2, lr=0.5, epochs=10000)
mlp.train(X_xor, y_xor)
# === μμΈ‘ κ²°κ³Ό μΆλ ₯ ===
print("\nXOR GATE Test (Multi-Layer Perceptron):")
xor_predictions = mlp.predict(X_xor)
for i, x in enumerate(X_xor):
predicted = xor_predictions[i][0]
actual = y_xor[i][0]
result = "β" if predicted == actual else "β"
print(f"Input: {x}, Predicted: {predicted}, Actual: {actual}, {result}")
# === κ²°μ κ²½κ³ μκ°ν ν¨μ ===
def plot_decision_boundary(X, y, model, title="Decision Boundary"):
cmap_light = ListedColormap(['#FFDDDD', '#DDDDFF'])
cmap_bold = ListedColormap(['#FF0000', '#0000FF'])
h = .01
x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
grid = np.c_[xx.ravel(), yy.ravel()]
Z = model.predict_prob(grid)
Z = Z.reshape(xx.shape)
plt.figure(figsize=(6, 5))
plt.contourf(xx, yy, Z > 0.5, cmap=cmap_light)
plt.scatter(X[:, 0], X[:, 1], c=y.ravel(), cmap=cmap_bold,
edgecolor='k', s=120)
plt.title(title)
plt.xlabel("Input 1")
plt.ylabel("Input 2")
plt.grid(True)
plt.show()
# === κ²°μ κ²½κ³ μκ°ν ===
plot_decision_boundary(X_xor, y_xor, mlp, title="XOR MLP Decision Boundary")
# === μμ€ κ³‘μ μκ°ν ===
plt.figure(figsize=(8, 5))
plt.plot(range(mlp.epochs), mlp.losses, color='purple')
plt.title("MLP Training Loss on XOR Problem")
plt.xlabel("Epochs")
plt.ylabel("Mean Squared Error")
plt.grid(True)
plt.show()
β Result : MLPλ‘ XOR λ¬Έμ ν΄κ²°
=== XOR Gate Multi-Layer Perceptron Training ===
XOR GATE Test (Multi-Layer Perceptron):
Input: [0 0], Predicted: 0, Actual: 0, β
Input: [0 1], Predicted: 1, Actual: 1, β
Input: [1 0], Predicted: 1, Actual: 1, β
Input: [1 1], Predicted: 0, Actual: 0, β