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

    https://github.com/apache/spark/pull/13796#discussion_r67803183
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/ml/classification/MultinomialLogisticRegressionSuite.scala
 ---
    @@ -0,0 +1,1001 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.ml.classification
    +
    +import scala.language.existentials
    +
    +import org.apache.spark.SparkFunSuite
    +import org.apache.spark.ml.attribute.NominalAttribute
    +import org.apache.spark.ml.classification.LogisticRegressionSuite._
    +import org.apache.spark.ml.feature.LabeledPoint
    +import org.apache.spark.ml.linalg._
    +import org.apache.spark.ml.param.ParamsSuite
    +import org.apache.spark.ml.util.{DefaultReadWriteTest, MLTestingUtils}
    +import org.apache.spark.ml.util.TestingUtils._
    +import org.apache.spark.mllib.util.MLlibTestSparkContext
    +import org.apache.spark.sql.{DataFrame, Dataset, Row}
    +
    +class MultinomialLogisticRegressionSuite
    +  extends SparkFunSuite with MLlibTestSparkContext with 
DefaultReadWriteTest {
    +
    +  @transient var dataset: Dataset[_] = _
    +  @transient var multinomialDataset: DataFrame = _
    +  private val eps: Double = 1e-5
    +
    +  override def beforeAll(): Unit = {
    +    super.beforeAll()
    +
    +    dataset = {
    +      val nPoints = 100
    +      val coefficients = Array(
    +        -0.57997, 0.912083, -0.371077,
    +        -0.16624, -0.84355, -0.048509)
    +
    +      val xMean = Array(5.843, 3.057)
    +      val xVariance = Array(0.6856, 0.1899)
    +
    +      val testData = generateMultinomialLogisticInput(
    +        coefficients, xMean, xVariance, addIntercept = true, nPoints, 42)
    +
    +      val df = spark.createDataFrame(sc.parallelize(testData, 4))
    +      df.cache()
    +      df
    +    }
    +
    +    multinomialDataset = {
    +      val nPoints = 10000
    +      val coefficients = Array(
    +        -0.57997, 0.912083, -0.371077, -0.819866, 2.688191,
    +        -0.16624, -0.84355, -0.048509, -0.301789, 4.170682)
    +
    +      val xMean = Array(5.843, 3.057, 3.758, 1.199)
    +      val xVariance = Array(0.6856, 0.1899, 3.116, 0.581)
    +
    +      val testData = generateMultinomialLogisticInput(
    +        coefficients, xMean, xVariance, addIntercept = true, nPoints, 42)
    +
    +      val df = spark.createDataFrame(sc.parallelize(testData, 4))
    +      df.cache()
    +      df
    +    }
    +  }
    +
    +  /**
    +   * Enable the ignored test to export the dataset into CSV format,
    +   * so we can validate the training accuracy compared with R's glmnet 
package.
    +   */
    +  ignore("export test data into CSV format") {
    +    multinomialDataset.rdd.map { case Row(label: Double, features: Vector) 
=>
    +      label + "," + features.toArray.mkString(",")
    +    
}.repartition(1).saveAsTextFile("target/tmp/LogisticRegressionSuite/multinomialDataset")
    +  }
    +
    +    test("params") {
    +      ParamsSuite.checkParams(new MultinomialLogisticRegression)
    +      val model = new MultinomialLogisticRegressionModel("mLogReg",
    +        Matrices.dense(2, 1, Array(0.0, 0.0)), Vectors.dense(0.0, 0.0), 2)
    +      ParamsSuite.checkParams(model)
    +    }
    +
    +    test("multinomial logistic regression: default params") {
    +      val mlr = new MultinomialLogisticRegression
    +      assert(mlr.getLabelCol === "label")
    +      assert(mlr.getFeaturesCol === "features")
    +      assert(mlr.getPredictionCol === "prediction")
    +      assert(mlr.getRawPredictionCol === "rawPrediction")
    +      assert(mlr.getProbabilityCol === "probability")
    +      assert(!mlr.isDefined(mlr.weightCol))
    +      assert(!mlr.isDefined(mlr.thresholds))
    +      assert(mlr.getFitIntercept)
    +      assert(mlr.getStandardization)
    +      val model = mlr.fit(dataset)
    +      model.transform(dataset)
    +        .select("label", "probability", "prediction", "rawPrediction")
    +        .collect()
    +      assert(model.getFeaturesCol === "features")
    +      assert(model.getPredictionCol === "prediction")
    +      assert(model.getRawPredictionCol === "rawPrediction")
    +      assert(model.getProbabilityCol === "probability")
    +      assert(model.intercepts !== Vectors.dense(0.0, 0.0))
    +      assert(model.hasParent)
    +    }
    +
    +  test("multinomial logistic regression with intercept without 
regularization") {
    +
    +    val trainer1 = (new 
MultinomialLogisticRegression).setFitIntercept(true)
    +      
.setElasticNetParam(0.0).setRegParam(0.0).setStandardization(true).setMaxIter(100)
    +    val trainer2 = (new 
MultinomialLogisticRegression).setFitIntercept(true)
    +      .setElasticNetParam(0.0).setRegParam(0.0).setStandardization(false)
    +
    +    val model1 = trainer1.fit(multinomialDataset)
    +    val model2 = trainer2.fit(multinomialDataset)
    +
    +    /*
    +       Using the following R code to load the data and train the model 
using glmnet package.
    +       > library("glmnet")
    +       > data <- read.csv("path", header=FALSE)
    +       > label = as.factor(data$V1)
    +       > features = as.matrix(data.frame(data$V2, data$V3, data$V4, 
data$V5))
    +       > coefficients = coef(glmnet(features, label, family="multinomial", 
alpha = 0, lambda = 0))
    +       > coefficients
    +        $`0`
    +        5 x 1 sparse Matrix of class "dgCMatrix"
    +                    s0
    +           -2.24493379
    +        V2  0.25096771
    +        V3 -0.03915938
    +        V4  0.14766639
    +        V5  0.36810817
    +        $`1`
    +        5 x 1 sparse Matrix of class "dgCMatrix"
    +                   s0
    +            0.3778931
    +        V2 -0.3327489
    +        V3  0.8893666
    +        V4 -0.2306948
    +        V5 -0.4442330
    +        $`2`
    +        5 x 1 sparse Matrix of class "dgCMatrix"
    +                    s0
    +            1.86704066
    +        V2  0.08178121
    +        V3 -0.85020722
    +        V4  0.08302840
    +        V5  0.07612480
    +     */
    +
    +    val coefficientsR = new DenseMatrix(3, 4, Array(
    +      0.2509677, -0.0391594, 0.1476664, 0.3681082,
    +      -0.3327489, 0.8893666, -0.2306948, -0.4442330,
    +      0.0817812, -0.8502072, 0.0830284, 0.0761248), isTransposed = true)
    +    val interceptsR = Vectors.dense(-2.2449338, 0.3778931, 1.8670407)
    +
    +    assert(model1.coefficients ~== coefficientsR relTol 0.05)
    +    assert(model1.intercepts ~== interceptsR relTol 0.05)
    +    assert(model2.coefficients ~== coefficientsR relTol 0.05)
    +    assert(model2.intercepts ~== interceptsR relTol 0.05)
    +  }
    +
    +  test("multinomial logistic regression without intercept without 
regularization") {
    +
    +    val trainer1 = (new 
MultinomialLogisticRegression).setFitIntercept(false)
    +      .setElasticNetParam(0.0).setRegParam(0.0).setStandardization(true)
    +    val trainer2 = (new 
MultinomialLogisticRegression).setFitIntercept(false)
    +      .setElasticNetParam(0.0).setRegParam(0.0).setStandardization(false)
    +
    +    val model1 = trainer1.fit(multinomialDataset)
    +    val model2 = trainer2.fit(multinomialDataset)
    +
    +    /*
    +       Using the following R code to load the data and train the model 
using glmnet package.
    +       library("glmnet")
    +       data <- read.csv("path", header=FALSE)
    +       label = as.factor(data$V1)
    +       features = as.matrix(data.frame(data$V2, data$V3, data$V4, data$V5))
    +       coefficients = coef(glmnet(features, label, family="multinomial", 
alpha = 0, lambda = 0,
    +        intercept=F))
    +       > coefficients
    +        $`0`
    +        5 x 1 sparse Matrix of class "dgCMatrix"
    +                    s0
    +            .
    +        V2  0.06992464
    +        V3 -0.36562784
    +        V4  0.12142680
    +        V5  0.32052211
    +        $`1`
    +        5 x 1 sparse Matrix of class "dgCMatrix"
    +                   s0
    +            .
    +        V2 -0.3036269
    +        V3  0.9449630
    +        V4 -0.2271038
    +        V5 -0.4364839
    +        $`2`
    +        5 x 1 sparse Matrix of class "dgCMatrix"
    +                   s0
    +            .
    +        V2  0.2337022
    +        V3 -0.5793351
    +        V4  0.1056770
    +        V5  0.1159618
    +     */
    +
    +    val coefficientsR = new DenseMatrix(3, 4, Array(
    +      0.0699246, -0.3656278, 0.1214268, 0.3205221,
    +      -0.3036269, 0.9449630, -0.2271038, -0.4364839,
    +      0.2337022, -0.5793351, 0.1056770, 0.1159618), isTransposed = true)
    +
    +    assert(model1.coefficients ~== coefficientsR relTol 0.05)
    +    assert(model2.coefficients ~== coefficientsR relTol 0.05)
    +    assert(model1.intercepts.toArray === Array.fill(3)(0.0))
    +    assert(model2.intercepts.toArray === Array.fill(3)(0.0))
    +  }
    +
    +  test("multinomial logistic regression with intercept with L1 
regularization") {
    +
    +    // use tighter constraints because OWL-QN solver takes longer to 
converge
    +    val trainer1 = (new 
MultinomialLogisticRegression).setFitIntercept(true)
    +      .setElasticNetParam(1.0).setRegParam(0.05).setStandardization(true)
    +      .setMaxIter(300).setTol(1e-10)
    --- End diff --
    
    I found that it requires many iterations for the OWLQN solver to converge 
closely with glmnet. This causes the test to take a significant amount of time. 


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

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

Reply via email to