This commit is contained in:
Morten Hjorth-Jensen
2021-10-07 09:02:53 +02:00
parent 0dab750863
commit c1b0b5fdc0
7 changed files with 18 additions and 18 deletions
+3 -3
View File
@@ -236,11 +236,11 @@ import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import SGDRegressor
n = 100
n = 1000
x = 2*np.random.rand(n,1)
y = 4+3*x+np.random.randn(n,1)
X = np.c_[np.ones((m,1)), x]
X = np.c_[np.ones((n,1)), x]
theta_linreg = np.linalg.inv(X.T @ X) @ (X.T @ y)
print("Own inversion")
print(theta_linreg)
@@ -281,7 +281,7 @@ for epoch in range(n_epochs):
random_index = np.random.randint(m)
xi = X[random_index:random_index+1]
yi = y[random_index:random_index+1]
gradients = (2.0/m) * xi.T @ ((xi @ theta)-yi)
gradients = 2.0* xi.T @ ((xi @ theta)-yi)
eta = learning_schedule(epoch*m+i)
theta = theta - eta*gradients
print("theta from own sdg")