Skip to content

Instantly share code, notes, and snippets.

@KhyatiMahendru
Created June 3, 2019 17:25
Show Gist options
  • Save KhyatiMahendru/602d45758e2f9a48fc7fef5460ca67c1 to your computer and use it in GitHub Desktop.
Save KhyatiMahendru/602d45758e2f9a48fc7fef5460ca67c1 to your computer and use it in GitHub Desktop.
def update_weights_MAE(m, b, X, Y, learning_rate):
m_deriv = 0
b_deriv = 0
N = len(X)
for i in range(N):
# Calculate partial derivatives
# -x(y - (mx + b)) / |mx + b|
m_deriv += - X[i] * (Y[i] - (m*X[i] + b)) / abs(Y[i] - (m*X[i] + b))
# -(y - (mx + b)) / |mx + b|
b_deriv += -(Y[i] - (m*X[i] + b)) / abs(Y[i] - (m*X[i] + b))
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment