0

我在閱讀使用L2正則化的SGD的開源mllib代碼時遇到困難。在mllib中使用L2正則化的SGD

的代碼是

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) 

微風lib中有說明文檔,解釋的部分:* =運算符

/** 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正規化的情況是

L2 gradient

如何的代碼表示該梯度在此更新?有人可以幫忙嗎?

回答

0

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

enter image description here

重新安排方面給予

enter image description here

承認的最後一項是剛剛梯度

enter image description here

這等價於有代碼

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 

現在代碼和方程式在歸一化因子內匹配,我認爲這是在下面的行中處理的。