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

    https://github.com/apache/spark/pull/9229#discussion_r42794044
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/ann/Layer.scala ---
    @@ -111,32 +126,26 @@ private[ann] class AffineLayer(val numIn: Int, val 
numOut: Int) extends Layer {
      * @param b bias (vector b)
      */
     private[ann] class AffineLayerModel private(w: BDM[Double], b: 
BDV[Double]) extends LayerModel {
    -  val size = w.size + b.length
    -  val gwb = new Array[Double](size)
    -  private lazy val gw: BDM[Double] = new BDM[Double](w.rows, w.cols, gwb)
    -  private lazy val gb: BDV[Double] = new BDV[Double](gwb, w.size)
    -  private var z: BDM[Double] = null
    -  private var d: BDM[Double] = null
    +
       private var ones: BDV[Double] = null
     
    -  override def eval(data: BDM[Double]): BDM[Double] = {
    -    if (z == null || z.cols != data.cols) z = new BDM[Double](w.rows, 
data.cols)
    -    z(::, *) := b
    -    BreezeUtil.dgemm(1.0, w, data, 1.0, z)
    -    z
    +  override def eval(data: BDM[Double], output: BDM[Double]): Unit = {
    +    output(::, *) := b
    +    BreezeUtil.dgemm(1.0, w, data, 1.0, output)
       }
     
    -  override def prevDelta(nextDelta: BDM[Double], input: BDM[Double]): 
BDM[Double] = {
    -    if (d == null || d.cols != nextDelta.cols) d = new BDM[Double](w.cols, 
nextDelta.cols)
    -    BreezeUtil.dgemm(1.0, w.t, nextDelta, 0.0, d)
    -    d
    +  override def prevDelta(nextDelta: BDM[Double], input: BDM[Double], 
delta: BDM[Double]): Unit = {
    +    BreezeUtil.dgemm(1.0, w.t, nextDelta, 0.0, delta)
       }
     
    -  override def grad(delta: BDM[Double], input: BDM[Double]): Array[Double] 
= {
    -    BreezeUtil.dgemm(1.0 / input.cols, delta, input.t, 0.0, gw)
    +  override def grad(delta: BDM[Double], input: BDM[Double], cumGrad: 
BDV[Double]): Unit = {
    +    // compute gradient of weights
    +    val cumGradientOfWeights = new BDM[Double](w.rows, w.cols, 
cumGrad.data, cumGrad.offset)
    +    BreezeUtil.dgemm(1.0 / input.cols, delta, input.t, 1.0, 
cumGradientOfWeights)
         if (ones == null || ones.length != delta.cols) ones = 
BDV.ones[Double](delta.cols)
    --- End diff --
    
    Since `w` does not resize if `delta` changes, I don't think we need to 
worry about resizing `ones` here and can instead just make L130 lazy init `ones`


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