Skip to content

Instantly share code, notes, and snippets.

Created June 3, 2019 17:29
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save KhyatiMahendru/b28d98b54d369957d475e70f5ca58347 to your computer and use it in GitHub Desktop.
def update_weights_Huber(m, b, X, Y, delta, learning_rate):
m_deriv = 0
b_deriv = 0
N = len(X)
for i in range(N):
# derivative of quadratic for small values and of linear for large values
if abs(Y[i] - m*X[i] - b) <= delta:
m_deriv += -X[i] * (Y[i] - (m*X[i] + b))
b_deriv += - (Y[i] - (m*X[i] + b))
m_deriv += delta * X[i] * ((m*X[i] + b) - Y[i]) / abs((m*X[i] + b) - Y[i])
b_deriv += delta * ((m*X[i] + b) - Y[i]) / abs((m*X[i] + b) - Y[i])
# We subtract because the derivatives point in direction of steepest ascent
m -= (m_deriv / float(N)) * learning_rate
b -= (b_deriv / float(N)) * learning_rate
return m, b
Copy link

Jezahmoud commented Jun 10, 2020

Dear KhyatiMahendru,

I hope this message finds you well.

I am interested in using your Huber code.

However, I have no idea about python.

It would be appreciated if you could convert this python code to R?

Kind Regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment