Github user avulanov commented on a diff in the pull request:
https://github.com/apache/spark/pull/8648#discussion_r38969255
--- 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) =>
--- End diff --
Flatten might be expensive for array of large arrays, is not it?
---
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]