【问题标题】:SGD with L2 regularization in mllib在 mllib 中使用 L2 正则化的 SGD
【发布时间】:2016-04-25 19:51:55
【问题描述】:

我在阅读具有 L2 正则化的 SGD 的开源 mllib 代码时遇到困难。

代码是

class SquaredL2Updater extends Updater {
override def compute(
  weightsOld: Vector,
  gradient: Vector,
  stepSize: Double,
  iter: Int,
  regParam: Double): (Vector, Double) = {
// add up both updates from the gradient of the loss (= step) as well as
// the gradient of the regularizer (= regParam * weightsOld)
// w' = w - thisIterStepSize * (gradient + regParam * w)
// w' = (1 - thisIterStepSize * regParam) * w - thisIterStepSize * gradient
val thisIterStepSize = stepSize / math.sqrt(iter)
val brzWeights: BV[Double] = weightsOld.toBreeze.toDenseVector
brzWeights :*= (1.0 - thisIterStepSize * regParam)
brzAxpy(-thisIterStepSize, gradient.toBreeze, brzWeights)
val norm = brzNorm(brzWeights, 2.0)

(Vectors.fromBreeze(brzWeights), 0.5 * regParam * norm * norm)
}

我遇到问题的部分是

brzWeights :*= (1.0 - thisIterStepSize * regParam)

微风库有解释 :*= 运算符的文档

/** Mutates this by element-wise multiplication of b into this. */
final def :*=[TT >: This, B](b: B)(implicit op: OpMulScalar.InPlaceImpl2[TT, B]): This = {
 op(repr, b)
 repr
}

它看起来只是一个向量乘以一个标量。

我在 L2 正则化的情况下找到的梯度公式是

在本次更新中,代码如何表示这种渐变?有人可以帮忙吗?

【问题讨论】:

    标签: apache-spark apache-spark-mllib gradient-descent scala-breeze


    【解决方案1】:

    好的,我想通了。更新方程是

    重新排列术语给出

    识别最后一项只是梯度

    这相当于代码有

    brzAxpy(-thisIterStepSize, gradient.toBreeze, brzWeights)
    

    打破它

    brzWeights = brzWeights + -thisIterStepSize * gradient.toBreeze
    

    在上一行中,brzWeights :*= (1.0 - thisIterStepSize * regParam)

    这意味着 brzWeights = brzWeights * (1.0 - thisIterStepSize * regParam)

    所以,终于

    brzWeights = brzWeights * (1.0 - thisIterStepSize * regParam) + (-thisIterStepSize) * gradient.toBreeze
    

    现在代码和方程在归一化因子内匹配,我相信这在下面的行中得到了处理。

    【讨论】:

      猜你喜欢
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 2016-10-05
      • 2012-03-11
      相关资源
      最近更新 更多