Deep-Learning-Models / Single_Layer_Perceptron.py
TinTinDo's picture
Upload 3 files
cf386c6 verified
import numpy as np
# --- 1. Dữ liệu AND gate ---
X = np.array([[0,0],
[0,1],
[1,0],
[1,1]])
y = np.array([0, 0, 0, 1]) # output AND
# --- 2. Hàm kích hoạt step function ---
def step(x):
return 1 if x > 0 else 0
# --- 3. Khởi tạo weights và bias ---
np.random.seed(42)
weights = np.random.randn(2)
bias = np.random.randn()
learning_rate = 0.1
epochs = 10
# --- 4. Huấn luyện Perceptron ---
for epoch in range(epochs):
print(f"Epoch {epoch+1}")
for xi, yi in zip(X, y):
z = np.dot(xi, weights) + bias
y_pred = step(z)
error = yi - y_pred
weights += learning_rate * error * xi
bias += learning_rate * error
print(f"Input: {xi}, Pred: {y_pred}, Error: {error}, Weights: {weights}, Bias: {bias}")
print("-"*50)
# --- 5. Test Perceptron ---
print("Testing trained perceptron (AND):")
for xi in X:
z = np.dot(xi, weights) + bias
y_pred = step(z)
print(f"Input: {xi}, Predicted: {y_pred}")
# ===============================================================
# ===============================================================
# THÊM CHO OR GATE
# ===============================================================
# ===============================================================
print("\n\n===== OR GATE =====")
# Dữ liệu OR gate
X_or = np.array([[0,0],
[0,1],
[1,0],
[1,1]])
y_or = np.array([0, 1, 1, 1]) # output OR
# Khởi tạo mới weights, bias
weights = np.random.randn(2)
bias = np.random.randn()
# Huấn luyện OR
for epoch in range(epochs):
for xi, yi in zip(X_or, y_or):
z = np.dot(xi, weights) + bias
y_pred = step(z)
error = yi - y_pred
weights += learning_rate * error * xi
bias += learning_rate * error
# Test OR
print("Testing trained perceptron (OR):")
for xi in X_or:
z = np.dot(xi, weights) + bias
y_pred = step(z)
print(f"Input: {xi}, Predicted: {y_pred}")
# ===============================================================
# ===============================================================
# THÊM CHO NOT GATE
# ===============================================================
# ===============================================================
print("\n\n===== NOT GATE =====")
# Dữ liệu NOT gate (1 input → output đảo)
X_not = np.array([[0],
[1]])
y_not = np.array([1, 0]) # NOT
# Khởi tạo weight, bias (1 chiều)
weight = np.random.randn(1)
bias = np.random.randn()
# Huấn luyện NOT
for epoch in range(epochs):
for xi, yi in zip(X_not, y_not):
z = np.dot(xi, weight) + bias
y_pred = step(z)
error = yi - y_pred
weight += learning_rate * error * xi
bias += learning_rate * error
# Test NOT
print("Testing trained perceptron (NOT):")
for xi in X_not:
z = np.dot(xi, weight) + bias
y_pred = step(z)
print(f"Input: {xi}, Predicted: {y_pred}")
'''
✅ Tóm tắt học thuật:
| Tiêu chí | Single Layer Perceptron (SLP) | Multi-Layer Perceptron (MLP) |
| ------------------------- | ------------------------------------- | ---------------------------------------------------------------------- |
| **Số lớp** | 1 lớp (input → output) | Nhiều lớp (input → hidden → output) |
| **Hàm học** | Tuyến tính | Phi tuyến tính (nhờ lớp ẩn và activation) |
| **Hàm kích hoạt** | Step function | Sigmoid, ReLU, Tanh hoặc các hàm phi tuyến khác |
| **Khả năng học XOR** | Không | Có |
| **Thuật toán huấn luyện** | Perceptron learning rule | Backpropagation + gradient descent |
| **Ứng dụng** | Phân loại tuyến tính cơ bản (AND, OR, Not) | Classification, regression, nhận dạng hình ảnh, NLP, dữ liệu phi tuyến |
| **Ưu điểm** | Đơn giản, dễ hiểu | Học được phi tuyến, khả năng biểu diễn cao |
| **Hạn chế** | Chỉ học tuyến tính | Dễ overfitting, cần tuning hyperparameters, tốn tài nguyên |
'''
'''
Epoch không phải là weight hay bias, nhưng về thuật toán học, nó được coi là hyperparameter.
Hyperparameter = tham số do con người đặt trước trước khi huấn luyện (khác với parameter là giá trị học được từ dữ liệu).
Các hyperparameters phổ biến:
Learning rate (𝜂)
Batch size
Số epoch
Số lớp ẩn, số neuron
Hàm kích hoạt
Như vậy: Epoch → hyperparameter, ảnh hưởng trực tiếp đến quá trình huấn luyện.
3. Epoch ảnh hưởng đến quá trình học như thế nào
Quá ít epoch → underfitting, model chưa học đủ.
Quá nhiều epoch → overfitting, model nhớ dữ liệu training quá mức, generalization kém.
Kết hợp với learning rate → số epoch quyết định model có hội tụ hay không.
4. Kết luận
Epoch là hyperparameter, nhưng nó không phải parameter học được từ dữ liệu.
Chọn epoch phù hợp = một phần quan trọng trong tuning hyperparameters để đạt hiệu quả tối ưu.
'''