(self, X, Y, lr=1e-5, n_iters=400)
| 24 | return 0.5 * self.w.dot(self.w) + self.C * np.maximum(0, 1 - margins).sum() |
| 25 | |
| 26 | def fit(self, X, Y, lr=1e-5, n_iters=400): |
| 27 | N, D = X.shape |
| 28 | self.N = N |
| 29 | self.w = np.random.randn(D) |
| 30 | self.b = 0 |
| 31 | |
| 32 | # gradient descent |
| 33 | losses = [] |
| 34 | for _ in range(n_iters): |
| 35 | margins = Y * self._decision_function(X) |
| 36 | loss = self._objective(margins) |
| 37 | losses.append(loss) |
| 38 | |
| 39 | idx = np.where(margins < 1)[0] |
| 40 | grad_w = self.w - self.C * Y[idx].dot(X[idx]) |
| 41 | self.w -= lr * grad_w |
| 42 | grad_b = -self.C * Y[idx].sum() |
| 43 | self.b -= lr * grad_b |
| 44 | |
| 45 | self.support_ = np.where((Y * self._decision_function(X)) <= 1)[0] |
| 46 | print("num SVs:", len(self.support_)) |
| 47 | |
| 48 | print("w:", self.w) |
| 49 | print("b:", self.b) |
| 50 | |
| 51 | # hist of margins |
| 52 | # m = Y * self._decision_function(X) |
| 53 | # plt.hist(m, bins=20) |
| 54 | # plt.show() |
| 55 | |
| 56 | plt.plot(losses) |
| 57 | plt.title("loss per iteration") |
| 58 | plt.show() |
| 59 | |
| 60 | def _decision_function(self, X): |
| 61 | return X.dot(self.w) + self.b |
no test coverage detected