Eric-Yyvuw commited on
Commit
e223b21
·
verified ·
1 Parent(s): 9a5fa74

Upload perceptron/perceptron.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. perceptron/perceptron.py +131 -0
perceptron/perceptron.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Perceptron Learning Algorithm — CS401
3
+ From scratch, no ML libraries.
4
+ Matches the lecture: linear neuron with step activation, weights update only on misclassification.
5
+ """
6
+
7
+ # ---- Training Data ----
8
+ # (Hours studied, Hours slept) → Passed (1) / Failed (0)
9
+ DATA = [
10
+ (1.0, 5.0, 0),
11
+ (1.5, 6.0, 0),
12
+ (2.0, 5.0, 0),
13
+ (2.5, 4.0, 0),
14
+ (3.0, 5.0, 0),
15
+ (4.0, 5.0, 1),
16
+ (5.0, 6.0, 1),
17
+ (6.0, 5.0, 1),
18
+ (6.5, 7.0, 1),
19
+ (7.0, 6.0, 1),
20
+ ]
21
+
22
+ n = len(DATA)
23
+ POINTS = [(d[0], d[1]) for d in DATA]
24
+ LABELS = [d[2] for d in DATA]
25
+
26
+ # Standardise features so the learning rate behaves consistently
27
+ means = [sum(p[i] for p in POINTS) / n for i in range(2)]
28
+ stds = [(sum((p[i] - means[i]) ** 2 for p in POINTS) / n) ** 0.5 for i in range(2)]
29
+ POINTS_S = [((p[0] - means[0]) / stds[0], (p[1] - means[1]) / stds[1]) for p in POINTS]
30
+
31
+
32
+ # ---- Neuron: step activation ----
33
+ def neuron(x, w, b):
34
+ """output = 1 if w.x + b >= 0 else 0"""
35
+ z = w[0] * x[0] + w[1] * x[1] + b
36
+ return 1 if z >= 0 else 0
37
+
38
+
39
+ # ---- Perceptron Learning Rule ----
40
+ def train_perceptron(lr=0.1, epochs=50):
41
+ w = [0.0, 0.0]
42
+ b = 0.0
43
+
44
+ for epoch in range(epochs):
45
+ errors = 0
46
+ for x, y in zip(POINTS_S, LABELS):
47
+ y_pred = neuron(x, w, b)
48
+ if y_pred != y: # update ONLY on mistake
49
+ errors += 1
50
+ w[0] += lr * (y - y_pred) * x[0]
51
+ w[1] += lr * (y - y_pred) * x[1]
52
+ b += lr * (y - y_pred)
53
+ if epoch % 10 == 0 or errors == 0:
54
+ print(f"Epoch {epoch:3d} | errors: {errors} | w: [{w[0]:+.4f}, {w[1]:+.4f}] | b: {b:+.4f}")
55
+ if errors == 0:
56
+ print(f"Converged at epoch {epoch} — zero training errors")
57
+ break
58
+
59
+ return w, b
60
+
61
+
62
+ # ---- Logic gates (lecture: "Neuron as Logic Gate") ----
63
+ def train_gate(name, inputs, targets, lr=1.0, epochs=20):
64
+ w = [0.0, 0.0]
65
+ b = 0.0
66
+ errors = epochs
67
+ for _ in range(epochs):
68
+ errors = 0
69
+ for x, y in zip(inputs, targets):
70
+ z = 1 if w[0] * x[0] + w[1] * x[1] + b >= 0 else 0
71
+ if z != y:
72
+ errors += 1
73
+ w[0] += lr * (y - z) * x[0]
74
+ w[1] += lr * (y - z) * x[1]
75
+ b += lr * (y - z)
76
+ if errors == 0:
77
+ break
78
+ hits = sum(
79
+ (1 if w[0] * x1 + w[1] * x2 + b >= 0 else 0) == y
80
+ for (x1, x2), y in zip(inputs, targets)
81
+ )
82
+ print(f"{name}: w = [{w[0]:+.1f}, {w[1]:+.1f}], b = {b:+.1f} → "
83
+ f"{hits}/{len(targets)} correct")
84
+ return errors
85
+
86
+
87
+ # ---- Main ----
88
+ if __name__ == "__main__":
89
+ print("=" * 60)
90
+ print("Perceptron Learning Algorithm")
91
+ print("=" * 60)
92
+ print(f"Training data: {n} examples")
93
+ print()
94
+
95
+ w, b = train_perceptron(lr=0.1, epochs=50)
96
+
97
+ print()
98
+ print("=" * 60)
99
+ print(f"Result: h(x) = step({w[0]:.4f}*x1 + {w[1]:.4f}*x2 + {b:.4f})")
100
+ print("=" * 60)
101
+
102
+ # Predictions
103
+ print()
104
+ print(f"{'Hours':>6} | {'Slept':>6} | {'Actual':>6} | {'z = w.x+b':>9} | {'Predicted':>9}")
105
+ print("-" * 50)
106
+ correct = 0
107
+ for xi, yi in zip(POINTS, LABELS):
108
+ xs = ((xi[0] - means[0]) / stds[0], (xi[1] - means[1]) / stds[1])
109
+ z = w[0] * xs[0] + w[1] * xs[1] + b
110
+ y_pred = neuron(xs, w, b)
111
+ correct += y_pred == yi
112
+ print(f"{xi[0]:>6} | {xi[1]:>6} | {yi:>6} | {z:>+9.3f} | {y_pred:>9}")
113
+
114
+ print()
115
+ print(f"Training accuracy: {correct}/{n} = {correct / n:.0%}")
116
+
117
+ # Predict new value
118
+ print()
119
+ new_point = (4.5, 6.0)
120
+ xs = ((new_point[0] - means[0]) / stds[0], (new_point[1] - means[1]) / stds[1])
121
+ print(f"New prediction: {new_point[0]} hours studied, {new_point[1]} hours slept "
122
+ f"→ {'PASS' if neuron(xs, w, b) else 'FAIL'}")
123
+
124
+ # Logic gates — can a single neuron learn them?
125
+ print()
126
+ print("Logic gates (single neuron):")
127
+ train_gate("AND", [(0, 0), (0, 1), (1, 0), (1, 1)], [0, 0, 0, 1])
128
+ train_gate("OR ", [(0, 0), (0, 1), (1, 0), (1, 1)], [0, 1, 1, 1])
129
+ xor_errors = train_gate("XOR", [(0, 0), (0, 1), (1, 0), (1, 1)], [0, 1, 1, 0])
130
+ if xor_errors:
131
+ print("XOR: NOT linearly separable — a single neuron can never learn it (→ need hidden layers)")