9/17/25, 12:43 AM Perceptron
In [7]: import numpy as np
from [Link] import OneHotEncoder
In [8]: import pickle
class PerceptronBigData:
def __init__(self, learning_rate=0.01, n_iters=100, batch_size=512, multiclass=False):
[Link] = learning_rate
self.n_iters = n_iters
self.batch_size = batch_size
[Link] = multiclass
self.errors_per_epoch = []
def _one_hot(self, y):
encoder = OneHotEncoder(sparse_output=False)
return encoder.fit_transform([Link](-1, 1))
def fit(self, X, y, start_epoch=0):
n_samples, n_features = [Link]
X_bias = [Link]([[Link]((n_samples, 1)), X])
if not hasattr(self, "weights") or [Link] is None:
if [Link]:
y_encoded = self._one_hot(y)
n_classes = y_encoded.shape[1]
[Link] = [Link]((n_classes, n_features + 1))
else:
y_encoded = y
[Link] = [Link](n_features + 1)
else:
y_encoded = self._one_hot(y) if [Link] else y
for epoch in range(start_epoch, self.n_iters):
indices = [Link](n_samples)
X_shuff, y_shuff = X_bias[indices], y_encoded[indices]
total_error = 0
for i in range(0, n_samples, self.batch_size):
X_batch = X_shuff[i:i+self.batch_size]
y_batch = y_shuff[i:i+self.batch_size]
[Link] 1/6
9/17/25, 12:43 AM Perceptron
if [Link]:
logits = X_batch @ [Link].T
preds = [Link](logits, axis=1)
y_true = [Link](y_batch, axis=1)
total_error += [Link](y_true != preds)
for j in range(len(y_batch)):
if preds[j] != y_true[j]:
[Link][y_true[j]] += [Link] * X_batch[j]
[Link][preds[j]] -= [Link] * X_batch[j]
else:
linear_output = X_batch @ [Link]
y_pred = [Link](linear_output >= 0, 1, 0)
errors = y_batch - y_pred
total_error += [Link](errors != 0)
[Link] += [Link] * (errors @ X_batch)
self.errors_per_epoch.append(total_error / (n_samples // self.batch_size))
# Tự động lưu checkpoint mỗi 10 epoch
if epoch % 10 == 0:
self.save_checkpoint("perceptron_checkpoint.pkl", epoch+1)
def predict(self, x):
x_bias = [Link](x, 0, 1.0)
if [Link]:
scores = [Link] @ x_bias
return [Link](scores)
else:
return 1 if [Link]([Link], x_bias) >= 0 else 0
def save_checkpoint(self, filename, next_epoch=0):
data = {
"weights": [Link],
"errors_per_epoch": self.errors_per_epoch,
"next_epoch": next_epoch
}
with open(filename, "wb") as f:
[Link](data, f)
print(f"Saved checkpoint at epoch {next_epoch}")
def load_checkpoint(self, filename):
[Link] 2/6
9/17/25, 12:43 AM Perceptron
with open(filename, "rb") as f:
data = [Link](f)
[Link] = data["weights"]
self.errors_per_epoch = data["errors_per_epoch"]
print(f"Loaded checkpoint, will continue from epoch {data['next_epoch']}")
return data["next_epoch"]
In [9]: from [Link] import make_classification
from sklearn.model_selection import train_test_split
# Sinh dữ liệu 3 lớp để thử
X, y = make_classification(n_samples=5000, n_features=2, n_classes=2,
n_informative=2, n_redundant=0, n_clusters_per_class=1,
random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Huấn luyện mô hình
model = PerceptronBigData(learning_rate=0.1, n_iters=50, batch_size=512, multiclass=True)
[Link](X_train, y_train)
# Đánh giá
preds = [Link]([[Link](x) for x in X_test])
acc = [Link](preds == y_test)
print("Accuracy:", acc)
Saved checkpoint at epoch 1
Saved checkpoint at epoch 11
Saved checkpoint at epoch 21
Saved checkpoint at epoch 31
Saved checkpoint at epoch 41
Accuracy: 0.881
In [10]: import [Link] as plt
[Link](figsize=(6,4))
[Link](model.errors_per_epoch, marker='o')
[Link]("Learning Curve - Error per Epoch")
[Link]("Epoch")
[Link]("Mean Error")
[Link](True)
[Link]()
[Link] 3/6
9/17/25, 12:43 AM Perceptron
In [11]: # Tạo lưới điểm để vẽ ranh giới
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 = [Link]([Link](x_min, x_max, 300),
[Link](y_min, y_max, 300))
Z = [Link]([[Link]([x, y]) for x, y in zip([Link](), [Link]())])
Z = [Link]([Link])
[Link](figsize=(6,6))
[Link](xx, yy, Z, alpha=0.3)
[Link](X_test[:, 0], X_test[:, 1], c=y_test, s=20, edgecolors="k")
[Link]("Decision Boundary - Multiclass Perceptron")
[Link]()
[Link] 4/6
9/17/25, 12:43 AM Perceptron
In [12]: # Tạo mô hình mới
model = PerceptronBigData(learning_rate=0.1, n_iters=100, batch_size=512, multiclass=True)
# Nếu có checkpoint thì load, nếu không thì bắt đầu từ 0
try:
start = model.load_checkpoint("perceptron_checkpoint.pkl")
except FileNotFoundError:
start = 0
[Link] 5/6
9/17/25, 12:43 AM Perceptron
# Tiếp tục huấn luyện
[Link](X_train, y_train, start_epoch=start)
Loaded checkpoint, will continue from epoch 41
Saved checkpoint at epoch 51
Saved checkpoint at epoch 61
Saved checkpoint at epoch 71
Saved checkpoint at epoch 81
Saved checkpoint at epoch 91
[Link] 6/6