Skip to content

Instantly share code, notes, and snippets.

@KhyatiMahendru
Created June 3, 2019 17:36
Show Gist options
  • Save KhyatiMahendru/78962ad9bd6d567cc5f97d97695ca3a7 to your computer and use it in GitHub Desktop.
Save KhyatiMahendru/78962ad9bd6d567cc5f97d97695ca3a7 to your computer and use it in GitHub Desktop.
def update_weights_Hinge(m1, m2, b, X1, X2, Y, learning_rate):
m1_deriv = 0
m2_deriv = 0
b_deriv = 0
N = len(X1)
for i in range(N):
# Calculate partial derivatives
if Y[i]*(m1*X1[i] + m2*X2[i] + b) <= 1:
m1_deriv += -X1[i] * Y[i]
m2_deriv += -X2[i] * Y[i]
b_deriv += -Y[i]
# else derivatives are zero
# We subtract because the derivatives point in direction of steepest ascent
m1 -= (m1_deriv / float(N)) * learning_rate
m2 -= (m2_deriv / float(N)) * learning_rate
b -= (b_deriv / float(N)) * learning_rate
return m1, m2, b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment