Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

python - How to code stochastic gradient descent from scratch?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...