python - 为什么我的线性回归梯度下降算法会发散
问题描述
即使我减小 alpha 的值甚至删除它,线性回归的梯度下降也会发散!
def gradientDescent(sqft_living, price, theta, alpha, num_iters):
m = price.size
# make a copy of theta, to avoid changing the original array, since numpy arrays
# are passed by reference to functions
theta = theta.copy()
J_history = [] # Use a python list to save cost in every iteration
theta_history = []
for z in range(num_iters):
term = 0
term0 = 0
for i in range(m):
h = np.dot(theta, sqft_living[i])
term += (h - price[i])
term0 += term * sqft_living[i][0]
temp0 = theta[0] - alpha * (1/m) * term0
term = 0
term1 = 0
for i in range(m):
h = np.dot(theta, sqft_living[i])
term += (h - price[i])
term1 += term * sqft_living[i][1]
temp1 = theta[1] - alpha * (1/m) * term1
theta[0] = temp0
theta[1] = temp1
cost = computeCost(sqft_living, price, theta)
print(cost)
J_history.append(cost)
theta_history.append(theta)
return theta, J_history
这是算法的使用
# initialize fitting parameters
theta = np.zeros(2)
# some gradient descent settings
iterations = 100
alpha = 0.00001
theta, J_history = gradientDescent(sqft_living, price, theta, alpha, iterations)
print(J_history)
这是输出的一部分:
成本 = 3.8144815615142405e+22
成本 = 8.337226954930875e+33
成本 = 1.8222489338606256e+45
成本 = 3.982848487760674e+56
成本 = 8.705222311669148e+67
成本 = 1.9026808508648173e+79
成本 = 4.158646718757122e+90
成本 = 9.089460549082665e+101
成本 = 1.9866629377457605e+113
成本 = 4.342204476162054e+124
成本 = 9.49065860875058e+135
成本 = 2.074351894810886e+147
成本 = 4.533864256311113e+158
解决方案
推荐阅读
- kotlin - 错误:线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“kotlin.Unit”
- javascript - 如何使用javascript显示列表中的平均值?
- javascript - tbody 元素的意外自动插入:表不是 row.parentElement
- javascript - 'this' 未定义 - ReactJS
- java - 调用 restTemplate.exchange() 时出现 IllegalStateException
- reactjs - 如何将 React + TypeScript 中的道具从父组件传递给子组件?
- android - 我可以在 MPAndroidChart 中设置单位吗?
- java - 为什么 Calendar.getTimeInMillis() 返回负值?
- java - 在 Spring JPA/Boot 中传播和持久化树中的更改
- node.js - 使用快速护照和sequelize的电子邮件验证