全量梯度下降
import numpy as np
np.random.seed(1)
X = np.random.rand(100, 1)
y = 4 + 3*X + np.random.randn(100, 1)
X_b = np.c_[np.ones((100, 1)), X]
n_iterations = 10000
t0, t1 = 5, 500
def learning_rate_schedule(t):
return t0/(t+t1)
theta = np.random.randn(2, 1)
for i in range(n_iterations):
gradients = X_b.T.dot(X_b.dot(theta)-y)
learning_rate = learning_rate_schedule(i)
theta = theta - learning_rate * gradients
print(theta)
随机梯度下降
import numpy as np
X = 2*np.random.rand(100, 1)
y = 4 + 3*X + np.random.randn(100, 1)
X_b = np.c_[np.ones((100, 1)), X]
n_epochs = 10000
m = 100
t0, t1 = 5, 500
def learning_rate_schedule(t):
return t0/(t+t1)
theta = np.random.randn(2, 1)
for epoch in range(n_epochs):
arr = np.arange(len(X_b))
np.random.shuffle(arr)
X_b = X_b[arr]
y = y[arr]
for i in range(m):
xi = X_b[i:i+1]
yi = y[i:i+1]
gradients = xi.T.dot(xi.dot(theta)-yi)
learning_rate = learning_rate_schedule(epoch*m + i)
theta = theta - learning_rate * gradients
print(theta)
小批量梯度下降
import numpy as np
X = 2*np.random.rand(100, 1)
y = 4 + 3*X + np.random.randn(100, 1)
X_b = np.c_[np.ones((100, 1)), X]
t0, t1 = 5, 500
def learning_rate_schedule(t):
return t0/(t+t1)
n_epochs = 100000
m = 100
batch_size = 10
num_batches = int(m / batch_size)
theta = np.random.randn(2, 1)
for epoch in range(n_epochs):
arr = np.arange(len(X_b))
np.random.shuffle(arr)
X_b = X_b[arr]
y = y[arr]
for i in range(num_batches):
x_batch = X_b[i*batch_size: i*batch_size + batch_size]
y_batch = y[i*batch_size: i*batch_size + batch_size]
gradients = x_batch.T.dot(x_batch.dot(theta)-y_batch)
learning_rate = learning_rate_schedule(epoch * m + i)
theta = theta - learning_rate*gradients
print(theta)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
2023-04-05 Systemd入门(1)