I am trying to create plot for Stochastic gradient descent . I am not getting how to write code at fourplaces marked in below code.
Consider ??(??1,??2)=8, ??1=2 and ??2=2 for plotting.
import numpy as np
import matplotlib.pyplot as plt
def generate_data(n=100, d=10, d1=2, d2=2):
X = np.random.rand(n, d)*2-1
theta = np.random.rand(d, 1)
noise = np.random.f(d1, d2, n).reshape((-1, 1)) # F-distribution
noise = np.log(noise) / 2 # Fisher's Z-distribution
return X, X @ theta + noise
def loss(X, y, theta):
## 111111111111111111
pass
def loss_grad_i(X, y, theta, i):
## 222222222222222222
pass
def sgd_plot(theta_init, X, y, n_steps=100):
for learning_rate in [7e-2, 1e-3, 1e-6]:
theta = theta_init.copy()
losses = []
for step_n in range(n_steps):
for i in range(X.shape[0]):
theta = 0 ## 33333333333333333
loss_i = 0 ## 44444444444444444
losses.append(loss_i)
plt.plot(losses)
plt.show()
def main(n=100, d=10, d1=2, d2=2):
X, y = generate_data(n, d, d1, d2)
theta_init = np.random.rand(d, 1)
sgd_plot(theta_init, X, y)
main()
question from:
https://stackoverflow.com/questions/65841353/how-to-code-stochastic-gradient-descent-from-scratch 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…