Skip to content

Instantly share code, notes, and snippets.

@Rajan-sust
Created May 22, 2021 00:58
Show Gist options
  • Save Rajan-sust/f3d206063f25263d183996cdccfc009a to your computer and use it in GitHub Desktop.
Save Rajan-sust/f3d206063f25263d183996cdccfc009a to your computer and use it in GitHub Desktop.
Stochastic Gradient Descent
from random import random, seed
from datetime import datetime
seed(datetime.utcnow().microsecond)
def main():
X = list(range(0, 25))
Y = [*map(lambda x: 2.0 * x + 3, X)]
weight = {
'w0' : random(),
'w1' : random(),
}
lr = 0.05
for epoch in range(1000):
for x,y in zip(X, Y):
y_prime = weight['w0'] + (x * weight['w1'])
weight['w0'] -= lr * 2.0 * (y_prime - y) / len(X)
weight['w1'] -= lr * 2.0 * (y_prime - y) * x / len(X)
print(weight)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment