Skip to content

Commit bb08d04

Browse files
committed
Fix: MLPRegressor tests (#1367)
* Fix: MLPRegressor tests * Fix: Ordering of statements in test * Fix: MLP n_calls
1 parent 6a6f6f1 commit bb08d04

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

test/test_pipeline/components/regression/test_base.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Type
1+
from typing import Type, Container
22

33
import unittest
44

@@ -41,31 +41,29 @@ def test_default_boston(self):
4141
Regressor=self.module
4242
)
4343

44+
score = sklearn.metrics.r2_score(y_true=targets, y_pred=predictions)
45+
46+
# Special treatment for Gaussian Process Regression
4447
if "default_boston_le_ge" in self.res:
45-
# Special treatment for Gaussian Process Regression
46-
self.assertLessEqual(
47-
sklearn.metrics.r2_score(y_true=targets, y_pred=predictions),
48-
self.res["default_boston_le_ge"][0]
49-
)
50-
self.assertGreaterEqual(
51-
sklearn.metrics.r2_score(y_true=targets, y_pred=predictions),
52-
self.res["default_boston_le_ge"][1]
53-
)
48+
upper, lower = self.res["default_boston_le_ge"]
49+
assert lower <= score <= upper
50+
5451
else:
55-
score = sklearn.metrics.r2_score(targets, predictions)
5652
fixture = self.res["default_boston"]
53+
places = self.res.get("default_boston_places", 7)
54+
5755
if score < -1e10:
58-
print(f"score = {score}, fixture = {fixture}")
5956
score = np.log(-score)
6057
fixture = np.log(-fixture)
61-
self.assertAlmostEqual(
62-
fixture,
63-
score,
64-
places=self.res.get("default_boston_places", 7),
65-
)
6658

67-
if self.res.get("boston_n_calls"):
68-
self.assertEqual(self.res["boston_n_calls"], n_calls)
59+
self.assertAlmostEqual(fixture, score, places)
60+
61+
if "boston_n_calls" in self.res:
62+
expected = self.res["boston_n_calls"]
63+
if isinstance(expected, Container):
64+
assert n_calls in expected
65+
else:
66+
assert n_calls == expected
6967

7068
def test_default_boston_iterative_fit(self):
7169

@@ -84,23 +82,28 @@ def test_default_boston_iterative_fit(self):
8482

8583
score = sklearn.metrics.r2_score(targets, predictions)
8684
fixture = self.res["default_boston_iterative"]
85+
places = self.res.get("default_boston_iterative_places", 7)
8786

8887
if score < -1e10:
8988
print(f"score = {score}, fixture = {fixture}")
9089
score = np.log(-score)
9190
fixture = np.log(-fixture)
9291

93-
self.assertAlmostEqual(
94-
fixture,
95-
score,
96-
places=self.res.get("default_boston_iterative_places", 7),
97-
)
92+
self.assertAlmostEqual(fixture, score, places)
9893

9994
if self.step_hyperparameter is not None:
100-
self.assertEqual(
101-
getattr(regressor.estimator, self.step_hyperparameter['name']),
102-
self.res.get("boston_iterative_n_iter", self.step_hyperparameter['value'])
103-
)
95+
param_name = self.step_hyperparameter['name']
96+
default = self.step_hyperparameter['value']
97+
98+
value = getattr(regressor.estimator, param_name)
99+
expected = self.res.get("boston_iterative_n_iter", default)
100+
101+
# To currently allow for MLPRegressor which is indeterministic,
102+
# we can have multiple values
103+
if isinstance(expected, Container):
104+
assert value in expected
105+
else:
106+
assert value == expected
104107

105108
def test_default_boston_iterative_sparse_fit(self):
106109

test/test_pipeline/components/regression/test_mlp.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, Dict
2+
13
import sklearn.neural_network
24

35
from autosklearn.pipeline.components.regression.mlp import MLPRegressor
@@ -22,21 +24,36 @@ class MLPComponentTest(BaseRegressionComponentTest):
2224
#
2325
# These seem to have consistent CPU's so I'm unsure what the underlying reason
2426
# for this to randomly fail only sometimes on Github runners
27+
#
28+
# Edit: If changing, please tracke what values were failing
29+
#
30+
# Seems there is a consistently different values for boston so:
31+
# * include two valuess for n_iter in 'boston_iterative_n_iter'
32+
# known-values = [236, 331]
33+
#
34+
# * decreased places from 6 -> 5 in 'default_boston_{sparse,_iterative_sparse}'
35+
# to check for for iterations and expanded the default places for checking
36+
# know-values = [-0.10972947168054104, -0.10973142976866268]
37+
#
38+
# * decreased places from 3 -> 1 in 'default_boston_places'
39+
# known-values = [0.29521793994422807, 0.2750079862455884]
40+
#
41+
# * Include two value for 'boston_n_calls'
42+
# known-values = [8, 9]
2543
__test__ = True
26-
2744
__test__ = True
2845

29-
res = dict()
46+
res: Dict[str, Any] = {}
3047
res["default_boston"] = 0.2750079862455884
31-
res["default_boston_places"] = 3
32-
res["boston_n_calls"] = 8
33-
res["boston_iterative_n_iter"] = 236
48+
res["default_boston_places"] = 1
49+
res["boston_n_calls"] = [8, 9]
50+
res["boston_iterative_n_iter"] = [236, 331]
3451
res["default_boston_iterative"] = res["default_boston"]
3552
res["default_boston_iterative_places"] = 1
3653
res["default_boston_sparse"] = -0.10972947168054104
37-
res["default_boston_sparse_places"] = 6
54+
res["default_boston_sparse_places"] = 5
3855
res["default_boston_iterative_sparse"] = res["default_boston_sparse"]
39-
res["default_boston_iterative_sparse_places"] = 6
56+
res["default_boston_iterative_sparse_places"] = res["default_boston_sparse_places"]
4057
res["default_diabetes"] = 0.35917389841850555
4158
res["diabetes_n_calls"] = 9
4259
res["diabetes_iterative_n_iter"] = 435

0 commit comments

Comments
 (0)