-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbase.py
More file actions
185 lines (150 loc) · 5.52 KB
/
Copy pathbase.py
File metadata and controls
185 lines (150 loc) · 5.52 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""Base types shared by the router and the three scorers."""
from __future__ import annotations
import math
from dataclasses import dataclass, field
from typing import Dict, List, Protocol
@dataclass(frozen=True)
class ClassifierResult:
"""Output of any classifier in this package.
``label`` is the predicted class id (a ``str`` for the router,
``"pass"`` / ``"fail"`` for the scorers). ``confidence`` is the
model's calibrated probability for that label, in ``[0, 1]``.
``features`` exposes the per-feature contribution so callers can
persist a per-decision explainability trace.
"""
label: str
confidence: float
features: Dict[str, float] = field(default_factory=dict)
class Classifier(Protocol):
"""Common surface across the router and the three scorers.
Implementations are deterministic — given the same fit input
they produce the same weights, and given the same predict
input they produce the same :class:`ClassifierResult`.
"""
def fit(self, training_rows: List[dict]) -> "Classifier":
...
def predict(self, features: Dict[str, float]) -> ClassifierResult:
...
# ---------------------------------------------------------------------------
# Pure-Python logistic regression primitives shared by the four classifiers
# ---------------------------------------------------------------------------
def _sigmoid(x: float) -> float:
"""Numerically stable logistic function."""
if x >= 0.0:
z = math.exp(-x)
return 1.0 / (1.0 + z)
z = math.exp(x)
return z / (1.0 + z)
def _softmax(logits: List[float]) -> List[float]:
"""Numerically stable softmax over ``logits``."""
if not logits:
return []
m = max(logits)
exps = [math.exp(v - m) for v in logits]
s = sum(exps)
if s == 0.0:
n = len(exps)
return [1.0 / n] * n
return [v / s for v in exps]
def _dot(weights: List[float], features: List[float]) -> float:
"""Inner product of two equal-length vectors."""
total = 0.0
for w, x in zip(weights, features):
total += w * x
return total
def fit_binary_logreg(
rows: List[dict],
feature_dim: int,
*,
epochs: int = 20,
learning_rate: float = 0.10,
weight_decay: float = 1e-4,
batch_size: int = 256,
seed: int = 42,
) -> List[float]:
"""Fit a binary logistic regression with closed-form gradient.
Each row in ``rows`` must carry:
- ``"features"``: list[float] of length ``feature_dim + 1`` (the
last entry is the bias term — caller appends 1.0).
- ``"label"``: ``0`` or ``1``.
Returns a weight vector of length ``feature_dim + 1``. Uses
standard mini-batch gradient descent with an L2 penalty; pure
Python, no numpy.
"""
import random as _random
rng = _random.Random(seed)
n = feature_dim + 1 # bias column appended by caller
weights = [0.0] * n
indices = list(range(len(rows)))
if not indices:
return weights
for _epoch in range(epochs):
rng.shuffle(indices)
for start in range(0, len(indices), batch_size):
batch = indices[start:start + batch_size]
grad = [0.0] * n
for i in batch:
row = rows[i]
x = row["features"]
y = float(row["label"])
z = _dot(weights, x)
p = _sigmoid(z)
err = p - y
for j in range(n):
grad[j] += err * x[j]
inv_b = 1.0 / max(len(batch), 1)
for j in range(n):
grad[j] = grad[j] * inv_b + weight_decay * weights[j]
weights[j] -= learning_rate * grad[j]
return weights
def fit_multinomial_logreg(
rows: List[dict],
feature_dim: int,
num_classes: int,
*,
epochs: int = 20,
learning_rate: float = 0.10,
weight_decay: float = 1e-4,
batch_size: int = 256,
seed: int = 42,
) -> List[List[float]]:
"""Fit a multinomial logistic regression (softmax classifier).
Each row in ``rows`` must carry:
- ``"features"``: list[float] of length ``feature_dim + 1``.
- ``"label"``: integer in ``[0, num_classes)``.
Returns a ``num_classes x (feature_dim + 1)`` weight matrix.
"""
import random as _random
rng = _random.Random(seed)
n = feature_dim + 1
weights: List[List[float]] = [[0.0] * n for _ in range(num_classes)]
indices = list(range(len(rows)))
if not indices:
return weights
for _epoch in range(epochs):
rng.shuffle(indices)
for start in range(0, len(indices), batch_size):
batch = indices[start:start + batch_size]
grads: List[List[float]] = [[0.0] * n for _ in range(num_classes)]
for i in batch:
row = rows[i]
x = row["features"]
y = int(row["label"])
logits = [_dot(weights[k], x) for k in range(num_classes)]
probs = _softmax(logits)
for k in range(num_classes):
err = probs[k] - (1.0 if k == y else 0.0)
for j in range(n):
grads[k][j] += err * x[j]
inv_b = 1.0 / max(len(batch), 1)
for k in range(num_classes):
for j in range(n):
grads[k][j] = grads[k][j] * inv_b + weight_decay * weights[k][j]
weights[k][j] -= learning_rate * grads[k][j]
return weights
__all__ = [
"Classifier",
"ClassifierResult",
"fit_binary_logreg",
"fit_multinomial_logreg",
]