значения параметров тета продолжает увеличиваться при градиентном спуске - PullRequest
0 голосов
/ 04 марта 2019

ниже - код, который я использую для градиентного спуска, при инициализации x в качестве 1-й строки код работает нормально, и я получаю правильные тэты, но при использовании x, как определено во 2-й строке rest, весь код такой жетэты продолжают увеличиваться с каждой итерацией (в отрицательном или положительном направлении), я не могу понять, почему это происходит.

x = 2 * np.random.rand(100, 1) #first initialization
x = np.arange(100).reshape(100,1) #second initialization
#i am initializing one at a time in the main code here i wrote both just for the question
delta = np.random.uniform(-5,5,size=(100,)).reshape(100,1)
y = 2*x + 3 + delta
# y = 3.5*x + 6
eta = 0.01
iterations = 10
#in main code i am using iteraton=1000 and not printing in the below loop, but just as i was getting nan's when using second x, so i made it run for 10 iteration and printed out to check what is happening
x_c = np.c_[np.ones((100, 1)), x]
theta = np.random.randn(2,1)
print(theta)
for i in range(iterations):
    grad = 2/100 * x_c.T.dot(x_c.dot(theta) - y)
    theta = theta - eta * grad
    print(theta)
print(theta)
plt.scatter(x,y)
y=theta[1][0] + theta[0][0]*x
plt.plot(x, y)
plt.show()
...