Created
May 22, 2021 00:58
-
-
Save Rajan-sust/f3d206063f25263d183996cdccfc009a to your computer and use it in GitHub Desktop.
Stochastic Gradient Descent
This file contains 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
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