3 Star 16 Fork 13

编程语言算法集/Jupyter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gradient_descent.py 603 Bytes
一键复制 编辑 原始数据 按行查看 历史
chiggshiggs 提交于 2022-10-19 16:47 . Added Gradient Descent (#82)
import numpy as np
def gradient_descent(x,y):
m_curr = b_curr = 0
iterations = 10000
n = len(x)
learning_rate = 0.08
for i in range(iterations):
y_predicted = m_curr * x + b_curr
cost = (1/n) * sum([val**2 for val in (y-y_predicted)])
md = -(2/n)*sum(x*(y-y_predicted))
bd = -(2/n)*sum(y-y_predicted)
m_curr = m_curr - learning_rate * md
b_curr = b_curr - learning_rate * bd
print ("m {}, b {}, cost {} iteration {}".format(m_curr,b_curr,cost, i))
x = np.array([1,2,3,4,5])
y = np.array([5,7,9,11,13])
gradient_descent(x,y)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/TheAlgorithms/Jupyter.git
git@gitee.com:TheAlgorithms/Jupyter.git
TheAlgorithms
Jupyter
Jupyter
master

搜索帮助