Github user freeman-lab commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1361#discussion_r15724418
  
    --- 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)
    +
    +    } else {
    +
    +      // Initialize weights as a column vector
    +      var weights = Vectors.dense(initialWeights.toArray)
    +      val n = weights.size
     
           /**
    -       * NOTE(Xinghao): lossSum is computed using the weights from the 
previous iteration
    -       * and regVal is the regularization value computed in the previous 
iteration as well.
    +       * 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.
            */
    -      stochasticLossHistory.append(lossSum / miniBatchSize + regVal)
    -      val update = updater.compute(
    -        weights, Vectors.fromBreeze(gradientSum / miniBatchSize), 
stepSize, i, regParam)
    -      weights = update._1
    -      regVal = update._2
    +      var regVal = updater.compute(
    +        weights, Vectors.dense(new Array[Double](weights.size)), 0, 1, 
regParam)._2
    +
    +      for (i <- 1 to numIterations) {
    +        // 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)
    +          .aggregate((BDV.zeros[Double](weights.size), 0.0))(
    --- End diff --
    
    Same for broadcasting, sorry, fixing...


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to