批量梯度下降法python具体实现

时间:2021-10-27 10:24:12
【文件属性】:
文件名称:批量梯度下降法python具体实现
文件大小:1KB
文件格式:PY
更新时间:2021-10-27 10:24:12
批量梯度下降 批量梯度下降法python具体实现, np.random.seed(666) x = 2 * np.random.random(size = 100) y = x * 3.0 + 4.0 + np.random.normal(size = 100) X = x.reshape(-1, 1) #损失函数 def J(theta, X_b, y): try: return np.sum((y - X_b.dot(theta))**2)/len(X_b) except: return float('inf') #损失函数的梯度 def dJ(theta, X_b, y): res = np.empty(len(theta)) res[0] = np.sum(X_b.dot(theta) - y) for i in range(1, len(theta)): res[i] = (X_b.dot(theta) - y).dot(X_b[:,i]) return res *2 /len(X_b)

网友评论