Skip to content

Commit

Permalink
[MINOR][ML] Increase Bounded MLOR (without regularization) test error…
Browse files Browse the repository at this point in the history
… tolerance

### What changes were proposed in this pull request?
Improve LogisticRegression test error tolerance

### Why are the changes needed?
When we switch BLAS version, some of the tests will fail due to too strict error tolerance in test.

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
N/A

Closes #30587 from WeichenXu123/fix_lor_test.

Authored-by: Weichen Xu <weichen.xu@databricks.com>
Signed-off-by: Weichen Xu <weichen.xu@databricks.com>
(cherry picked from commit f021f6d)
Signed-off-by: Weichen Xu <weichen.xu@databricks.com>
  • Loading branch information
WeichenXu123 committed Dec 9, 2020
1 parent 1093c0f commit b0a70ab
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1548,9 +1548,9 @@ class LogisticRegressionSuite extends MLTest with DefaultReadWriteTest {
val interceptsExpected1 = Vectors.dense(
1.0000152482448372, 3.591773288423673, 5.079685953744937)

checkCoefficientsEquivalent(model1.coefficientMatrix, coefficientsExpected1)
checkBoundedMLORCoefficientsEquivalent(model1.coefficientMatrix, coefficientsExpected1)
assert(model1.interceptVector ~== interceptsExpected1 relTol 0.01)
checkCoefficientsEquivalent(model2.coefficientMatrix, coefficientsExpected1)
checkBoundedMLORCoefficientsEquivalent(model2.coefficientMatrix, coefficientsExpected1)
assert(model2.interceptVector ~== interceptsExpected1 relTol 0.01)

// Bound constrained optimization with bound on both side.
Expand Down Expand Up @@ -1585,9 +1585,9 @@ class LogisticRegressionSuite extends MLTest with DefaultReadWriteTest {
isTransposed = true)
val interceptsExpected3 = Vectors.dense(1.0, 2.0, 2.0)

checkCoefficientsEquivalent(model3.coefficientMatrix, coefficientsExpected3)
checkBoundedMLORCoefficientsEquivalent(model3.coefficientMatrix, coefficientsExpected3)
assert(model3.interceptVector ~== interceptsExpected3 relTol 0.01)
checkCoefficientsEquivalent(model4.coefficientMatrix, coefficientsExpected3)
checkBoundedMLORCoefficientsEquivalent(model4.coefficientMatrix, coefficientsExpected3)
assert(model4.interceptVector ~== interceptsExpected3 relTol 0.01)

// Bound constrained optimization with infinite bound on both side.
Expand Down Expand Up @@ -1621,9 +1621,9 @@ class LogisticRegressionSuite extends MLTest with DefaultReadWriteTest {
val interceptsExpected5 = Vectors.dense(
-2.2231282183460723, 0.3669496747012527, 1.856178543644802)

checkCoefficientsEquivalent(model5.coefficientMatrix, coefficientsExpected5)
checkBoundedMLORCoefficientsEquivalent(model5.coefficientMatrix, coefficientsExpected5)
assert(model5.interceptVector ~== interceptsExpected5 relTol 0.01)
checkCoefficientsEquivalent(model6.coefficientMatrix, coefficientsExpected5)
checkBoundedMLORCoefficientsEquivalent(model6.coefficientMatrix, coefficientsExpected5)
assert(model6.interceptVector ~== interceptsExpected5 relTol 0.01)
}

Expand Down Expand Up @@ -1719,9 +1719,9 @@ class LogisticRegressionSuite extends MLTest with DefaultReadWriteTest {
1.7524631428961193, 1.2292565990448736, 1.3433784431904323, 1.5846063017678864),
isTransposed = true)

checkCoefficientsEquivalent(model1.coefficientMatrix, coefficientsExpected)
checkBoundedMLORCoefficientsEquivalent(model1.coefficientMatrix, coefficientsExpected)
assert(model1.interceptVector.toArray === Array.fill(3)(0.0))
checkCoefficientsEquivalent(model2.coefficientMatrix, coefficientsExpected)
checkBoundedMLORCoefficientsEquivalent(model2.coefficientMatrix, coefficientsExpected)
assert(model2.interceptVector.toArray === Array.fill(3)(0.0))
}

Expand Down Expand Up @@ -2953,16 +2953,17 @@ object LogisticRegressionSuite {
}

/**
* Note: This method is only used in Bounded MLOR (without regularization) test
* When no regularization is applied, the multinomial coefficients lack identifiability
* because we do not use a pivot class. We can add any constant value to the coefficients
* and get the same likelihood. If fitting under bound constrained optimization, we don't
* choose the mean centered coefficients like what we do for unbound problems, since they
* may out of the bounds. We use this function to check whether two coefficients are equivalent.
*/
def checkCoefficientsEquivalent(coefficients1: Matrix, coefficients2: Matrix): Unit = {
def checkBoundedMLORCoefficientsEquivalent(coefficients1: Matrix, coefficients2: Matrix): Unit = {
coefficients1.colIter.zip(coefficients2.colIter).foreach { case (col1: Vector, col2: Vector) =>
(col1.asBreeze - col2.asBreeze).toArray.toSeq.sliding(2).foreach {
case Seq(v1, v2) => assert(v1 ~= v2 absTol 1E-3)
case Seq(v1, v2) => assert(v1 ~= v2 absTol 1E-2)
}
}
}
Expand Down

0 comments on commit b0a70ab

Please sign in to comment.