Skip to content

Instantly share code, notes, and snippets.

@tarlanahad
Created March 21, 2020 17:43
Show Gist options
  • Save tarlanahad/11c36432d371e6ba16ad30c70c12aa3c to your computer and use it in GitHub Desktop.
Save tarlanahad/11c36432d371e6ba16ad30c70c12aa3c to your computer and use it in GitHub Desktop.
def get_cost_grads(self, X, w, y):
distances = self.distances(w)
# Get current cost
L = 1 / 2 * np.dot(w, w) - self.C * np.sum(distances)
dw = np.zeros(len(w))
for ind, d in enumerate(distances):
if d == 0: # if sample is not on the support vector
di = w # (alpha * y[ind] * X[ind]) = 0
else:
# (alpha * y[ind] * X[ind]) = y[ind] * X[ind]
di = w - (self.C * y[ind] * X[ind])
dw += di
return L, dw / len(X)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment