Github user brkyvz commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8648#discussion_r39191027
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/ann/Layer.scala ---
    @@ -573,47 +582,27 @@ private[ml] class FeedForwardModel private(
           case _ =>
             throw new UnsupportedOperationException("Non-functional layer not 
supported at the top")
         }
    +    // backward pass (back-propagate deltas given errors)
         deltas(L) = new BDM[Double](0, 0)
         deltas(L - 1) = newE
         for (i <- (L - 2) to (0, -1)) {
           deltas(i) = layerModels(i + 1).prevDelta(deltas(i + 1), outputs(i + 
1))
         }
    -    val grads = new Array[Array[Double]](layerModels.length)
    -    for (i <- 0 until layerModels.length) {
    -      val input = if (i==0) data else outputs(i - 1)
    -      grads(i) = layerModels(i).grad(deltas(i), input)
    +    // forward pass (forward-propagate gradients given inputs)
    +    val grads = layerModels.zipWithIndex.map { case (layer, i) =>
    +      val input = if (i == 0) data else outputs(i - 1)
    +      layer.grad(deltas(i), input)
         }
    -    // update cumGradient
    +    // update cumulative gradients
         val cumGradientArray = cumGradient.toArray
    -    var offset = 0
    -    // TODO: extract roll
    -    for (i <- 0 until grads.length) {
    -      val gradArray = grads(i)
    -      var k = 0
    -      while (k < gradArray.length) {
    -        cumGradientArray(offset + k) += gradArray(k)
    -        k += 1
    -      }
    -      offset += gradArray.length
    +    grads.flatten.zipWithIndex.foreach { case (newGrad, i) =>
    +      cumGradientArray(i) += newGrad
         }
         newError
       }
     
    -  // TODO: do we really need to copy the weights? they should be read-only
       override def weights(): Vector = {
    -    // TODO: extract roll
    -    var size = 0
    -    for (i <- 0 until layerModels.length) {
    -      size += layerModels(i).size
    -    }
    -    val array = new Array[Double](size)
    -    var offset = 0
    -    for (i <- 0 until layerModels.length) {
    -      val layerWeights = layerModels(i).weights().toArray
    -      System.arraycopy(layerWeights, 0, array, offset, layerWeights.length)
    -      offset += layerWeights.length
    -    }
    -    Vectors.dense(array)
    +    Vectors.dense(layerModels.flatMap(_.weights().toArray))
    --- End diff --
    
    I imagine System.arraycopy would be much faster and efficient here


---
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.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to