【问题标题】:Python: How is the code for Stochastic Gradient Descent working?Python:随机梯度下降的代码是如何工作的?
【发布时间】:2020-08-02 08:33:04
【问题描述】:
n_epochs = 50
t0, t1 = 5, 50 # learning schedule hyperparameters
def learning_schedule(t):
return t0 / (t + t1)
theta = np.random.randn(2,1) # random initialization

for epoch in range(n_epochs):
   for i in range(m):
      random_index = np.random.randint(m)
      xi = X_b[random_index:random_index+1]
      yi = y[random_index:random_index+1]
      gradients = 2 * xi.T.dot(xi.dot(theta) - yi)
      eta = learning_schedule(epoch * m + i)
      theta = theta - eta * gradients

按照惯例,我们迭代 m 轮;每一轮称为一个纪元。 Batch Gradient Descent 代码在整个训练集上迭代了 1000 次,而这段代码只在训练集上迭代了 50 次,并达到了很好的解决方案

作者说代码只迭代了训练集 50 次。这怎么可能?

对于每个 epoch,1->50,i 从 1->100 开始,训练数据迭代 50*100=5000 次,这不是这样吗?

【问题讨论】:

    标签: python machine-learning gradient-descent


    【解决方案1】:

    它将迭代所有数据点 50 次 例如,如果数据点 =100 且 epoch = 50,则答案为 50*100=5000

    如果它的随机梯度下降,则总迭代次数为 5000

    如果它的简单梯度下降,那么总迭代次数是 50

    如果您想了解不同的梯度下降技术,请访问HERE

    【讨论】:

    • 但是按照这个逻辑,SGD 怎么比批量梯度下降快?由于 SGD 与 Batch 相比迭代 5000 次。还有为什么作者提到它只有50?
    • SGD 在时间复杂度方面并不比 Batch 梯度快。 SGD 的计算效率高于 Batch GD。但有时 SGD 会比 GD 提供更快的收敛结果。我认为作者的意思是收敛结果而不是时间复杂度
    猜你喜欢
    • 2019-04-15
    • 1970-01-01
    • 2016-06-13
    • 2016-09-25
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2014-07-30
    • 2018-12-10
    相关资源
    最近更新 更多