Skip to content

Instantly share code, notes, and snippets.

@FisherKK
Last active March 15, 2020 12:48
Show Gist options
  • Save FisherKK/a8032a8d64effb3d676c053f5fbf96f7 to your computer and use it in GitHub Desktop.
Save FisherKK/a8032a8d64effb3d676c053f5fbf96f7 to your computer and use it in GitHub Desktop.
def train(X, y, model_parameters, learning_rate=0.0005, iterations=20000):
# Make prediction for every data sample
predictions = [predict(x, model_parameters) for x in X]
# Calculate initial cost for model - MSE
initial_error = mse(predictions, y)
print("Initial state:")
print(" - error: {}".format(initial_error))
print(" - parameters: {}".format(model_parameters))
for i in range(iterations):
# Sum up partial gradients for every data sample, for every parameter in model
accumulated_grad_w0 = 0
accumulated_grad_b = 0
for x, y_target in zip(X, y):
accumulated_grad_w0 += (predict(x, model_parameters) - y_target)*x[0]
accumulated_grad_b += (predict(x, model_parameters) - y_target)
# Calculate mean of gradient
w_grad = (1.0/len(X)) * accumulated_grad_w0
b_grad = (1.0/len(X)) * accumulated_grad_b
# Update parameters by small part of averaged gradient
model_parameters["w"][0] = model_parameters["w"][0] - learning_rate * w_grad
model_parameters["b"] = model_parameters["b"] - learning_rate * b_grad
if i % 4000 == 0:
print("\nIteration {}:".format(i))
print(" - error: {}".format(mse([predict(x, model_parameters) for x in X], y)))
print(" - parameters: {}".format(model_parameters))
print("\nFinal state:")
print(" - error: {}".format(mse([predict(x, model_parameters) for x in X], y)))
print(" - parameters: {}".format(model_parameters))
@arjangcore
Copy link

Hi
Is it possible to get the csv data file? thx

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