File size: 5,855 Bytes
32333f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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.

'''