Loading
Loading
An Artificial Neural Network (ANN) is inspired by the human brain โ but works completely differently. It's the foundation on which all modern AI is built.
Input Layer โ Hidden Layers โ Output Layer
Each neuron computes: output = activation(ฮฃ wแตขxแตข + b)
max(0, x) โ most common, fast, solves vanishing gradientDeep networks learn hierarchical representations: early layers detect edges and colors, middle layers detect shapes, deep layers recognize high-level concepts. Much more efficient than shallow networks with many neurons.
| Parameter | Description | Typical Value | |-----------|-------------|--------------| | Learning Rate | How fast to learn | 0.001 | | Batch Size | Samples per step | 32 or 64 | | Epochs | Full data passes | 10โ100 | | Dropout | Random deactivation | 0.1โ0.5 |
import numpy as np
print("=" * 55)
print("ุดุจูุฉ ุนุตุจูุฉ ู
ู ุงูุตูุฑ โ NumPy ููุท")
print("=" * 55)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ุฏูุงู ุงูุชูุดูุท
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def relu(x): return np.maximum(0, x)
def relu_grad(x): return (x > 0).astype(float)
def sigmoid(x): return 1 / (1 + np.exp(-np.clip(x, -500, 500)))
def sigmoid_grad(x):
s = sigmoid(x)
return s * (1 - s)
def softmax(x):
e = np.exp(x - x.max(axis=1, keepdims=True))
return e / e.sum(axis=1, keepdims=True)
print("\n1. ุฏูุงู ุงูุชูุดูุท:")
x_test = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
print(f" x : {x_test}")
print(f" ReLU(x) : {relu(x_test)}")
print(f" ฯ(x) : {sigmoid(x_test).round(3)}")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ุดุจูุฉ MLP ุจุณูุทุฉ โ ุทุจูุชุงู ู
ุฎููุชุงู
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class SimpleNN:
"""ุดุจูุฉ ุนุตุจูุฉ ุจุณูุทุฉ: Input โ Hidden โ Output"""
def __init__(self, input_size: int, hidden_size: int, output_size: int, lr: float = 0.01):
# ุชููุฆุฉ ุงูุฃูุฒุงู (Xavier Initialization)
scale1 = np.sqrt(2.0 / input_size)
scale2 = np.sqrt(2.0 / hidden_size)
self.W1 = np.random.randn(input_size, hidden_size) * scale1
self.b1 = np.zeros((1, hidden_size))
self.W2 = np.random.randn(hidden_size, output_size) * scale2
self.b2 = np.zeros((1, output_size))
self.lr = lr
def forward(self, X: np.ndarray) -> np.ndarray:
"""ุงูุชู
ุฑูุฑ ุงูุฃู
ุงู
ู"""
self.X = X
self.z1 = X @ self.W1 + self.b1
self.a1 = relu(self.z1) # Hidden layer: ReLU
self.z2 = self.a1 @ self.W2 + self.b2
self.a2 = sigmoid(self.z2) # Output: Sigmoid
return self.a2
def backward(self, y: np.ndarray) -> float:
"""Backpropagation"""
n = len(y)
loss = -np.mean(y * np.log(self.a2 + 1e-8) + (1 - y) * np.log(1 - self.a2 + 1e-8))
# ุชุฏุฑุฌุงุช ุทุจูุฉ Output
dz2 = (self.a2 - y) / n
dW2 = self.a1.T @ dz2
db2 = dz2.sum(axis=0, keepdims=True)
# ุชุฏุฑุฌุงุช ุทุจูุฉ Hidden
da1 = dz2 @ self.W2.T
dz1 = da1 * relu_grad(self.z1)
dW1 = self.X.T @ dz1
db1 = dz1.sum(axis=0, keepdims=True)
# ุชุญุฏูุซ ุงูุฃูุฒุงู (Gradient Descent)
self.W2 -= self.lr * dW2
self.b2 -= self.lr * db2
self.W1 -= self.lr * dW1
self.b1 -= self.lr * db1
return float(loss)
def predict(self, X: np.ndarray) -> np.ndarray:
return (self.forward(X) >= 0.5).astype(int)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ู
ุซุงู: XOR Problem
# (Linear models can't solve it, NNs can!)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print("\n2. XOR Problem โ ูู
ุงุฐุง ูุญุชุงุฌ Depthุ")
X_xor = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float)
y_xor = np.array([[0], [1], [1], [0]], dtype=float)
nn = SimpleNN(input_size=2, hidden_size=8, output_size=1, lr=0.1)
# ุชุฏุฑูุจ
print(" ุงูุชุฏุฑูุจ...")
for epoch in range(5000):
nn.forward(X_xor)
loss = nn.backward(y_xor)
if epoch % 1000 == 0:
preds = nn.predict(X_xor)
acc = (preds == y_xor).mean()
print(f" Epoch {epoch:4d}: Loss={loss:.4f}, Accuracy={acc:.0%}")
print("\n ุงููุชุงุฆุฌ ุงูููุงุฆูุฉ:")
print(" Input | ุงูุชูุจุค | ุงูุตุญูุญ")
for i, (x, y) in enumerate(zip(X_xor, y_xor)):
pred = nn.predict(x.reshape(1, -1))[0][0]
status = "โ
" if pred == int(y[0]) else "โ"
print(f" {x} | {pred} | {int(y[0])} {status}")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ุญุฌู
ุงููู
ูุฐุฌ ูุนุฏุฏ ุงูู
ุนุงู
ูุงุช
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print("\n3. ุญุณุงุจ ุนุฏุฏ ุงูู
ุนุงู
ูุงุช:")
configs = [
("ุตุบูุฑ", 2, 8, 1),
("ู
ุชูุณุท", 784, 128, 10),
("ูุจูุฑ", 784, 512, 10),
]
for name, inp, hid, out in configs:
params = (inp * hid + hid) + (hid * out + out)
print(f" {name:7s}: {params:>8,} ู
ุนุงู
ู")