spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mengxr <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-11262][ML] Unit test for gradient, loss...
Date Tue, 15 Mar 2016 17:44:28 GMT
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/9229#discussion_r56207925
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/ann/Layer.scala ---
    @@ -150,72 +166,32 @@ private[ann] object AffineLayerModel {
       /**
        * Creates a model of Affine layer
        * @param layer layer properties
    -   * @param weights vector with weights
    -   * @param position position of weights in the vector
    +   * @param weights vector for weights initialization
    +   * @param random random number generator
        * @return model of Affine layer
        */
    -  def apply(layer: AffineLayer, weights: Vector, position: Int): AffineLayerModel = {
    -    val (w, b) = unroll(weights, position, layer.numIn, layer.numOut)
    -    new AffineLayerModel(w, b)
    +  def apply(layer: AffineLayer, weights: BDV[Double], random: Random): AffineLayerModel
= {
    +    randomWeights(layer.numIn, layer.numOut, weights, random)
    +    new AffineLayerModel(weights, layer)
       }
     
       /**
    -   * Creates a model of Affine layer
    -   * @param layer layer properties
    -   * @param seed seed
    -   * @return model of Affine layer
    -   */
    -  def apply(layer: AffineLayer, seed: Long): AffineLayerModel = {
    -    val (w, b) = randomWeights(layer.numIn, layer.numOut, seed)
    -    new AffineLayerModel(w, b)
    -  }
    -
    -  /**
    -   * Unrolls the weights from the vector
    -   * @param weights vector with weights
    -   * @param position position of weights for this layer
    -   * @param numIn number of layer inputs
    -   * @param numOut number of layer outputs
    -   * @return matrix A and vector b
    -   */
    -  def unroll(
    -    weights: Vector,
    -    position: Int,
    -    numIn: Int,
    -    numOut: Int): (BDM[Double], BDV[Double]) = {
    -    val weightsCopy = weights.toArray
    -    // TODO: the array is not copied to BDMs, make sure this is OK!
    -    val a = new BDM[Double](numOut, numIn, weightsCopy, position)
    -    val b = new BDV[Double](weightsCopy, position + (numOut * numIn), 1, numOut)
    -    (a, b)
    -  }
    -
    -  /**
    -   * Roll the layer weights into a vector
    -   * @param a matrix A
    -   * @param b vector b
    -   * @return vector of weights
    -   */
    -  def roll(a: BDM[Double], b: BDV[Double]): Vector = {
    -    val result = new Array[Double](a.size + b.length)
    -    // TODO: make sure that we need to copy!
    -    System.arraycopy(a.toArray, 0, result, 0, a.size)
    -    System.arraycopy(b.toArray, 0, result, a.size, b.length)
    -    Vectors.dense(result)
    -  }
    -
    -  /**
    -   * Generate random weights for the layer
    +   * Initialize weights
        * @param numIn number of inputs
        * @param numOut number of outputs
    -   * @param seed seed
    -   * @return (matrix A, vector b)
    +   * @param weights vector for weights initialization
    +   * @param random random number generator
        */
    -  def randomWeights(numIn: Int, numOut: Int, seed: Long = 11L): (BDM[Double], BDV[Double])
= {
    -    val rand: XORShiftRandom = new XORShiftRandom(seed)
    -    val weights = BDM.fill[Double](numOut, numIn){ (rand.nextDouble * 4.8 - 2.4) / numIn
}
    -    val bias = BDV.fill[Double](numOut){ (rand.nextDouble * 4.8 - 2.4) / numIn }
    -    (weights, bias)
    +  def randomWeights(
    +    numIn: Int,
    --- End diff --
    
    4-space indentation


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message