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

    https://github.com/apache/spark/pull/9229#discussion_r42795626
  
    --- 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],
    --- End diff --
    
    nit: Arguments should be 4-indented on new line


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