Skip to content

Instantly share code, notes, and snippets.

@FisherKK
Last active August 13, 2018 11:15
Show Gist options
  • Select an option

  • Save FisherKK/77a2e7dc4fc68508206430c3492a40fb to your computer and use it in GitHub Desktop.

Select an option

Save FisherKK/77a2e7dc4fc68508206430c3492a40fb to your computer and use it in GitHub Desktop.
from copy import deepcopy
def train(X, y, model_parameters, step=0.1, iterations=100):
# Make prediction for every data sample
predictions = [predict(x, model_parameters) for x in X]
# Calculate initial cost for model - MSE
lowest_error = mse(predictions, y)
for i in range(iterations):
candidates, errors = list(), list()
# w increased, b increased
param_candidate = deepcopy(model_parameters)
param_candidate["b"] += step
param_candidate["w"] += step
candidate_pred = [predict(x, param_candidate) for x in X]
candidate_error = mse(candidate_pred, y)
candidates.append(param_candidate)
errors.append(candidate_error)
# w increased, b unchanged
(...)
# w increased, b decreased
(...)
# w unchanged, b increased
(...)
# w unchanged, b unchanged
(...)
# w unchanged, b decreased
(...)
# w decreased, b increased
(...)
# w decreased, b unchanged
(...)
# w decreased, b decreased
(...)
# Update with parameters for which loss is smallest
best_candidate = None
for candidate, candidate_error in zip(candidates, errors):
if candidate_error < lowest_error:
lowest_error = candidate_error
model_parameters["w"] = candidate["w"]
model_parameters["b"] = candidate["b"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment