-
-
Save anonymous/c4d83f4cc8c0fdfbdd33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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