Skip to content

Commit 1aca7c5

Browse files
committed
nit
1 parent 0577bb2 commit 1aca7c5

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

mllib/src/main/scala/org/apache/spark/ml/optim/aggregator/HingeAggregator.scala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ private[ml] class BlockHingeAggregator(
132132

133133
@transient private lazy val linear = {
134134
val linear = if (fitIntercept) coefficientsArray.take(numFeatures) else coefficientsArray
135-
Vectors.dense(linear).toDense
135+
Vectors.dense(linear)
136136
}
137137

138138
/**
139-
* Add a new training instance block to this HingeAggregator, and update the loss and gradient
140-
* of the objective function.
139+
* Add a new training instance block to this BlockHingeAggregator, and update the loss and
140+
* gradient of the objective function.
141141
*
142142
* @param block The InstanceBlock to be added.
143-
* @return This HingeAggregator object.
143+
* @return This BlockHingeAggregator object.
144144
*/
145145
def add(block: InstanceBlock): this.type = {
146146
require(block.matrix.isTransposed)
@@ -163,7 +163,6 @@ private[ml] class BlockHingeAggregator(
163163
// in-place convert dotProducts to gradient scales
164164
// then, vec represents gradient scales
165165
var i = 0
166-
var interceptGradSum = 0.0
167166
while (i < size) {
168167
val weight = block.getWeight(i)
169168
if (weight > 0) {
@@ -172,12 +171,11 @@ private[ml] class BlockHingeAggregator(
172171
// Therefore the gradient is -(2y - 1)*x
173172
val label = block.getLabel(i)
174173
val labelScaled = label + label - 1.0
175-
val loss = (1.0 - labelScaled * vec.values(i)) * weight
174+
val loss = (1.0 - labelScaled * vec(i)) * weight
176175
if (loss > 0) {
177176
lossSum += loss
178177
val gradScale = -labelScaled * weight
179178
vec.values(i) = gradScale
180-
if (fitIntercept) interceptGradSum += gradScale
181179
} else { vec.values(i) = 0.0 }
182180
} else { vec.values(i) = 0.0 }
183181
i += 1
@@ -190,20 +188,20 @@ private[ml] class BlockHingeAggregator(
190188
case dm: DenseMatrix =>
191189
BLAS.nativeBLAS.dgemv("N", dm.numCols, dm.numRows, 1.0, dm.values, dm.numCols,
192190
vec.values, 1, 1.0, gradientSumArray, 1)
193-
if (fitIntercept) gradientSumArray(numFeatures) += interceptGradSum
194191

195192
case sm: SparseMatrix if fitIntercept =>
196193
val linearGradSumVec = Vectors.zeros(numFeatures).toDense
197194
BLAS.gemv(1.0, sm.transpose, vec, 0.0, linearGradSumVec)
198195
BLAS.getBLAS(numFeatures).daxpy(numFeatures, 1.0, linearGradSumVec.values, 1,
199196
gradientSumArray, 1)
200-
gradientSumArray(numFeatures) += interceptGradSum
201197

202198
case sm: SparseMatrix if !fitIntercept =>
203199
val gradSumVec = new DenseVector(gradientSumArray)
204200
BLAS.gemv(1.0, sm.transpose, vec, 1.0, gradSumVec)
205201
}
206202

203+
if (fitIntercept) gradientSumArray(numFeatures) += vec.values.sum
204+
207205
this
208206
}
209207
}

mllib/src/main/scala/org/apache/spark/ml/optim/aggregator/LogisticAggregator.scala

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ private[ml] class LogisticAggregator(
370370
* BlockLogisticAggregator computes the gradient and loss used in Logistic classification
371371
* for blocks in sparse or dense matrix in an online fashion.
372372
*
373-
* Two BlockLogisticAggregator can be merged together to have a summary of loss and gradient of
373+
* Two BlockLogisticAggregators can be merged together to have a summary of loss and gradient of
374374
* the corresponding joint dataset.
375375
*
376376
* NOTE: The feature values are expected to be standardized before computation.
@@ -413,8 +413,8 @@ private[ml] class BlockLogisticAggregator(
413413
}
414414

415415
@transient private lazy val binaryLinear = (multinomial, fitIntercept) match {
416-
case (false, true) => Vectors.dense(coefficientsArray.take(numFeatures)).toDense
417-
case (false, false) => Vectors.dense(coefficientsArray).toDense
416+
case (false, true) => Vectors.dense(coefficientsArray.take(numFeatures))
417+
case (false, false) => Vectors.dense(coefficientsArray)
418418
case _ => null
419419
}
420420

@@ -428,11 +428,11 @@ private[ml] class BlockLogisticAggregator(
428428
}
429429

430430
/**
431-
* Add a new training instance block to this LogisticAggregator, and update the loss and gradient
432-
* of the objective function.
431+
* Add a new training instance block to this BlockLogisticAggregator, and update the loss and
432+
* gradient of the objective function.
433433
*
434434
* @param block The instance block of data point to be added.
435-
* @return This LogisticAggregator object.
435+
* @return This BlockLogisticAggregator object.
436436
*/
437437
def add(block: InstanceBlock): this.type = {
438438
require(block.matrix.isTransposed)
@@ -467,7 +467,6 @@ private[ml] class BlockLogisticAggregator(
467467
// in-place convert margins to multiplier
468468
// then, vec represents multiplier
469469
var i = 0
470-
var interceptGradSum = 0.0
471470
while (i < size) {
472471
val weight = block.getWeight(i)
473472
if (weight > 0) {
@@ -482,7 +481,6 @@ private[ml] class BlockLogisticAggregator(
482481
}
483482
val multiplier = weight * (1.0 / (1.0 + math.exp(margin)) - label)
484483
vec.values(i) = multiplier
485-
if (fitIntercept) interceptGradSum += multiplier
486484
} else { vec.values(i) = 0.0 }
487485
i += 1
488486
}
@@ -494,19 +492,19 @@ private[ml] class BlockLogisticAggregator(
494492
case dm: DenseMatrix =>
495493
BLAS.nativeBLAS.dgemv("N", dm.numCols, dm.numRows, 1.0, dm.values, dm.numCols,
496494
vec.values, 1, 1.0, gradientSumArray, 1)
497-
if (fitIntercept) gradientSumArray(numFeatures) += interceptGradSum
498495

499496
case sm: SparseMatrix if fitIntercept =>
500497
val linearGradSumVec = Vectors.zeros(numFeatures).toDense
501498
BLAS.gemv(1.0, sm.transpose, vec, 0.0, linearGradSumVec)
502499
BLAS.getBLAS(numFeatures).daxpy(numFeatures, 1.0, linearGradSumVec.values, 1,
503500
gradientSumArray, 1)
504-
gradientSumArray(numFeatures) += interceptGradSum
505501

506502
case sm: SparseMatrix if !fitIntercept =>
507503
val gradSumVec = new DenseVector(gradientSumArray)
508504
BLAS.gemv(1.0, sm.transpose, vec, 1.0, gradSumVec)
509505
}
506+
507+
if (fitIntercept) gradientSumArray(numFeatures) += vec.values.sum
510508
}
511509

512510
/** Update gradient and loss using multinomial (softmax) loss function. */

0 commit comments

Comments
 (0)