pytorch面试题II:梯度更新的代码实现

本文最后更新于 2024年7月8日凌晨4点06分

pytorch面试题II: 梯度更新的代码实现

1. BGD

1
2
3
4
5
6
7
def batchGradientDescent(x, y, theta, alpha, m, maxIteration):
for i in range(maxIteration):
hypothesis = np.dot(x, theta)
loss = hypothesis - y
gradient = np.dot(x.transpose(), loss) / m
theta = theta - alpha * gradient # 对所有样本求和
return theta

2. SGD

1
2
3
4
5
6
7
8
9
10
11
12
13
def StochasticGradientDescent(x, y, theta, alpha, m, maxIteration):
data = []
for i in range(10):
data.append(i)
# 这里随便挑选一个进行更新点进行即可(不用想BGD一样全部考虑)

for i in range(maxIteration):
hypothesis = np.dot(x, theta)
loss = hypothesis - y # 这里还是有十个样本
index = random.sample(data, 1)[0] # 随机抽取一个样本,得到它的下标
gradient = loss[index] * x[index] # 只取一个点进行更新计算
theta = theta - alpha * gradient.T
return theta

3. MBGD

1

参考:

(梯度下降的三种形式BGD/SGD/MBGD)[https://www.cnblogs.com/zongfa/p/9293887.html]


pytorch面试题II:梯度更新的代码实现
https://kangkang37.github.io/2024/07/08/pytorch02_GD/
作者
kangkang
发布于
2024年7月8日
许可协议