Skip to content

Instantly share code, notes, and snippets.

Created April 5, 2015 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/c4d83f4cc8c0fdfbdd33 to your computer and use it in GitHub Desktop.
Save anonymous/c4d83f4cc8c0fdfbdd33 to your computer and use it in GitHub Desktop.
def stochastic_linear_gradient_descent(X, y, theta, alpha, tolerance):
converged = False
prev_cost = 0
size = X.shape[0]
XT = X.transpose()
while not converged:
for i in range (size):
sample_x = X[i,:]
hypothesis = np.dot(sample_x, theta)
loss = hypothesis - y
curr_cost = compute_lin_cost(theta, X, y)
grad = (1.0/size) * np.dot(XT, loss)
theta -= alpha * grad
if (abs(curr_cost - prev_cost) < tolerance):
converged = True
break
else:
converged = False
prev_cost = curr_cost
print("cost " , curr_cost)
return theta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment