Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/1361#discussion_r15723261
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/optimization/GradientDescent.scala
---
@@ -162,45 +162,55 @@ object GradientDescent extends Logging {
val numExamples = data.count()
val miniBatchSize = numExamples * miniBatchFraction
- // Initialize weights as a column vector
- var weights = Vectors.dense(initialWeights.toArray)
- val n = weights.size
-
- /**
- * For the first iteration, the regVal will be initialized as sum of
weight squares
- * if it's L2 updater; for L1 updater, the same logic is followed.
- */
- var regVal = updater.compute(
- weights, Vectors.dense(new Array[Double](weights.size)), 0, 1,
regParam)._2
-
- for (i <- 1 to numIterations) {
- val bcWeights = data.context.broadcast(weights)
- // Sample a subset (fraction miniBatchFraction) of the total data
- // compute and sum up the subgradients on this subset (this is one
map-reduce)
- val (gradientSum, lossSum) = data.sample(false, miniBatchFraction,
42 + i)
- .treeAggregate((BDV.zeros[Double](n), 0.0))(
- seqOp = (c, v) => (c, v) match { case ((grad, loss), (label,
features)) =>
- val l = gradient.compute(features, label, bcWeights.value,
Vectors.fromBreeze(grad))
- (grad, loss + l)
- },
- combOp = (c1, c2) => (c1, c2) match { case ((grad1, loss1),
(grad2, loss2)) =>
- (grad1 += grad2, loss1 + loss2)
- })
+ // if no data, return initial weights to avoid NaNs
+ if (numExamples == 0) {
+
+ logInfo("GradientDescent.runMiniBatchSGD returning initial weights,
no data found")
+ (initialWeights, stochasticLossHistory.toArray)
--- End diff --
it may be better to use `return (initialWeights,
stochasticLossHistory.toArray)` here to avoid having extra indentation for the
main block.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---