Github user feynmanliang commented on a diff in the pull request:
https://github.com/apache/spark/pull/9229#discussion_r42795707
--- Diff: mllib/src/main/scala/org/apache/spark/ml/ann/Layer.scala ---
@@ -546,65 +446,83 @@ private[ml] object FeedForwardTopology {
* Model of Feed Forward Neural Network.
* Implements forward, gradient computation and can return weights in
vector format.
* @param layerModels models of layers
- * @param topology topology of the network
+ * @param layers topology of the network
*/
private[ml] class FeedForwardModel private(
val layerModels: Array[LayerModel],
- val topology: FeedForwardTopology) extends TopologyModel {
+ val layers: Array[Layer]) extends TopologyModel {
+
+ private var outputs: Array[BDM[Double]] = null
+ private var deltas: Array[BDM[Double]] = null
+
override def forward(data: BDM[Double]): Array[BDM[Double]] = {
- val outputs = new Array[BDM[Double]](layerModels.length)
- outputs(0) = layerModels(0).eval(data)
+ // Initialize output arrays for all layers. Special treatment for
InPlace
+ val currentBatchSize = data.cols
+ // TODO: allocate outputs as one big array and then create BDMs from it
+ if (outputs == null || outputs(0).cols != currentBatchSize) {
+ outputs = new Array[BDM[Double]](layerModels.length)
+ var inputSize = data.rows
+ for (i <- 0 until layerModels.length) {
+ layerModels(i) match {
+ case inPlace: InPlace => outputs(i) = outputs(i - 1)
+ case _ => {
+ val outputSize = layers(i).outputSize(inputSize)
+ outputs(i) = new BDM[Double](outputSize, currentBatchSize)
+ inputSize = outputSize
+ }
+ }
+ }
+ }
+ layerModels(0).eval(data, outputs(0))
for (i <- 1 until layerModels.length) {
- outputs(i) = layerModels(i).eval(outputs(i-1))
+ layerModels(i).eval(outputs(i - 1), outputs(i))
}
outputs
}
- override def computeGradient(
- data: BDM[Double],
- target: BDM[Double],
- cumGradient: Vector,
- realBatchSize: Int): Double = {
+ override def computeGradient(data: BDM[Double],
+ target: BDM[Double],
+ cumGradient: Vector,
+ realBatchSize: Int): Double = {
val outputs = forward(data)
- val deltas = new Array[BDM[Double]](layerModels.length)
+ val currentBatchSize = data.cols
+ // TODO: allocate deltas as one big array and then create BDMs from it
+ if (deltas == null || deltas(0).cols != currentBatchSize) {
+ deltas = new Array[BDM[Double]](layerModels.length)
+ var inputSize = data.rows
+ for (i <- 0 until layerModels.length - 1) {
+ val outputSize = layers(i).outputSize(inputSize)
+ deltas(i) = new BDM[Double](outputSize, currentBatchSize)
+ inputSize = outputSize
+ }
+ }
val L = layerModels.length - 1
- val (newE, newError) = layerModels.last match {
- case flm: FunctionalLayerModel => flm.error(outputs.last, target)
+ // TODO: explain why delta of top layer is null (because it might
contain loss+layer)
+ val loss = layerModels.last match {
+ case levelWithError: LossFunction =>
levelWithError.loss(outputs.last, target, deltas(L - 1))
case _ =>
- throw new UnsupportedOperationException("Non-functional layer not
supported at the top")
+ throw new UnsupportedOperationException("Top layer is required to
have objective.")
}
- 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))
+ layerModels(i + 1).prevDelta(deltas(i + 1), outputs(i + 1),
deltas(i))
--- End diff --
extra leading whitespace
---
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]