Skip to content

Instantly share code, notes, and snippets.

@KhyatiMahendru
Created June 3, 2019 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KhyatiMahendru/d5c0021c94c96c3003b68a6adda94705 to your computer and use it in GitHub Desktop.
Save KhyatiMahendru/d5c0021c94c96c3003b68a6adda94705 to your computer and use it in GitHub Desktop.
def update_weights_MSE(m, b, X, Y, learning_rate):
m_deriv = 0
b_deriv = 0
N = len(X)
for i in range(N):
# Calculate partial derivatives
# -2x(y - (mx + b))
m_deriv += -2*X[i] * (Y[i] - (m*X[i] + b))
# -2(y - (mx + b))
b_deriv += -2*(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