【发布时间】: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