python 实施问题中的梯度下降

Gradient Descent in python implementation issue

嘿,我想了解这个线性假设算法。我不知道我的实现是否正确。我认为这是不正确的,但我不知道我错过了什么。

theta0 = 1
theta1 = 1
alpha = 0.01
for i in range(0,le*10): 
    for j in range(0,le):
        temp0 = theta0 - alpha * (theta1 * x[j] + theta0 - y[j])
        temp1 = theta1 - alpha * (theta1 * x[j] + theta0 - y[j]) * x[j]
        theta0 = temp0 
        theta1 = temp1

print ("Values of slope and y intercept derived using gradient descent ",theta1, theta0)

它给了我四级精度的正确答案。但是当我将它与网上的其他程序进行比较时,我感到很困惑。

提前致谢!

梯度下降算法的实现:

import numpy as np

cur_x = 1 # Initial value
gamma = 1e-2 # step size multiplier
precision = 1e-10
prev_step_size = cur_x

# test function
def foo_func(x):
    y = (np.sin(x) + x**2)**2
    return y

# Iteration loop until a certain error measure
# is smaller than a maximal error
while (prev_step_size > precision):
    prev_x = cur_x
    cur_x += -gamma * foo_func(prev_x)
    prev_step_size = abs(cur_x - prev_x)

print("The local minimum occurs at %f" % cur_x)