Skip to content

Commit 9896769

Browse files
committed
default to num_threads = 1
1 parent 4056d22 commit 9896769

File tree

1 file changed

+51
-47
lines changed

1 file changed

+51
-47
lines changed

slise/slise.py

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def regression(
5050
max_approx: float = 1.15,
5151
max_iterations: int = 300,
5252
debug: bool = False,
53-
num_threads: int = -1,
53+
num_threads: int = 1,
5454
) -> SliseRegression:
5555
"""Use SLISE for robust regression
5656
@@ -78,7 +78,7 @@ def regression(
7878
max_approx (float, optional): Approximation ratio when selecting the next beta. Defaults to 1.15.
7979
max_iterations (int, optional): Maximum number of OWL-QN iterations. Defaults to 300.
8080
debug (bool, optional): Print debug statements each graduated optimisation step. Defaults to False.
81-
num_threads (int, optional): The number of threads to use for the optimisation. Defaults to -1.
81+
num_threads (int, optional): The number of numba threads. Set to -1 to use numba defaults. Values >1 sometimes cause unexpectedly large overhead on some CPUs. Defaults to 1.
8282
8383
Returns:
8484
SliseRegression: Object containing the regression result.
@@ -117,7 +117,7 @@ def explain(
117117
max_approx: float = 1.15,
118118
max_iterations: int = 300,
119119
debug: bool = False,
120-
num_threads: int = -1,
120+
num_threads: int = 1,
121121
) -> SliseExplainer:
122122
"""Use SLISE for explaining outcomes from black box models.
123123
@@ -151,7 +151,7 @@ def explain(
151151
max_approx (float, optional): Approximation ratio when selecting the next beta. Defaults to 1.15.
152152
max_iterations (int, optional): Maximum number of OWL-QN iterations. Defaults to 300.
153153
debug (bool, optional): Print debug statements each graduated optimisation step. Defaults to False.
154-
num_threads (int, optional): The number of threads to use for the optimisation. Defaults to -1.
154+
num_threads (int, optional): The number of numba threads. Set to -1 to use numba defaults. Values >1 sometimes cause unexpectedly large overhead on some CPUs. Defaults to 1.
155155
156156
Returns:
157157
SliseExplainer: Object containing the explanation.
@@ -194,7 +194,7 @@ def __init__(
194194
max_approx: float = 1.15,
195195
max_iterations: int = 300,
196196
debug: bool = False,
197-
num_threads: int = -1,
197+
num_threads: int = 1,
198198
):
199199
"""Use SLISE for robust regression.
200200
@@ -218,7 +218,7 @@ def __init__(
218218
max_approx (float, optional): Approximation ratio when selecting the next beta. Defaults to 1.15.
219219
max_iterations (int, optional): Maximum number of OWL-QN iterations. Defaults to 300.
220220
debug (bool, optional): Print debug statements each graduated optimisation step. Defaults to False.
221-
num_threads (int, optional): The number of threads to use for the optimisation. Defaults to -1.
221+
num_threads (int, optional): The number of numba threads. Set to -1 to use numba defaults. Values >1 sometimes cause unexpectedly large overhead on some CPUs. Defaults to 1.
222222
"""
223223
assert epsilon > 0.0, "`epsilon` must be positive!"
224224
assert lambda1 >= 0.0, "`lambda1` must not be negative!"
@@ -293,26 +293,28 @@ def fit(
293293
X = add_intercept_column(X)
294294
# Initialisation
295295
threads = set_threads(self.num_threads)
296-
if init is None:
297-
alpha, beta = self.init_fn(X, Y, self.epsilon, self._weight)
298-
else:
299-
alpha, beta = initialise_fixed(init, X, Y, self.epsilon, self._weight)
300-
# Optimisation
301-
alpha = graduated_optimisation(
302-
alpha=alpha,
303-
X=X,
304-
Y=Y,
305-
epsilon=self.epsilon,
306-
beta=beta,
307-
lambda1=self.lambda1,
308-
lambda2=self.lambda2,
309-
weight=self._weight,
310-
beta_max=self.beta_max,
311-
max_approx=self.max_approx,
312-
max_iterations=self.max_iterations,
313-
debug=self.debug,
314-
)
315-
set_threads(threads)
296+
try:
297+
if init is None:
298+
alpha, beta = self.init_fn(X, Y, self.epsilon, self._weight)
299+
else:
300+
alpha, beta = initialise_fixed(init, X, Y, self.epsilon, self._weight)
301+
# Optimisation
302+
alpha = graduated_optimisation(
303+
alpha=alpha,
304+
X=X,
305+
Y=Y,
306+
epsilon=self.epsilon,
307+
beta=beta,
308+
lambda1=self.lambda1,
309+
lambda2=self.lambda2,
310+
weight=self._weight,
311+
beta_max=self.beta_max,
312+
max_approx=self.max_approx,
313+
max_iterations=self.max_iterations,
314+
debug=self.debug,
315+
)
316+
finally:
317+
set_threads(threads)
316318
self._alpha = alpha
317319
if self._normalise:
318320
alpha2 = self._scale.unscale_model(alpha)
@@ -575,7 +577,7 @@ def __init__(
575577
max_approx: float = 1.15,
576578
max_iterations: int = 300,
577579
debug: bool = False,
578-
num_threads: int = -1,
580+
num_threads: int = 1,
579581
):
580582
"""Use SLISE for explaining outcomes from black box models.
581583
@@ -606,7 +608,7 @@ def __init__(
606608
max_approx (float, optional): Approximation ratio when selecting the next beta. Defaults to 1.15.
607609
max_iterations (int, optional): Maximum number of OWL-QN iterations. Defaults to 300.
608610
debug (bool, optional): Print debug statements each graduated optimisation step. Defaults to False.
609-
num_threads (int, optional): The number of threads to use for the optimisation. Defaults to -1.
611+
num_threads (int, optional): The number of numba threads. Set to -1 to use numba defaults. Values >1 sometimes cause unexpectedly large overhead on some CPUs. Defaults to 1.
610612
"""
611613
assert epsilon > 0.0, "`epsilon` must be positive!"
612614
assert lambda1 >= 0.0, "`lambda1` must not be negative!"
@@ -705,25 +707,27 @@ def explain(
705707
X = self._X2 - x[None, :]
706708
Y = self._Y2 - y
707709
threads = set_threads(self.num_threads)
708-
if init is None:
709-
alpha, beta = self.init_fn(X, Y, self.epsilon, self._weight)
710-
else:
711-
alpha, beta = initialise_fixed(init, X, Y, self.epsilon, self._weight)
712-
alpha = graduated_optimisation(
713-
alpha=alpha,
714-
X=X,
715-
Y=Y,
716-
epsilon=self.epsilon,
717-
beta=beta,
718-
lambda1=self.lambda1,
719-
lambda2=self.lambda2,
720-
weight=self._weight,
721-
beta_max=self.beta_max,
722-
max_approx=self.max_approx,
723-
max_iterations=self.max_iterations,
724-
debug=self.debug,
725-
)
726-
set_threads(threads)
710+
try:
711+
if init is None:
712+
alpha, beta = self.init_fn(X, Y, self.epsilon, self._weight)
713+
else:
714+
alpha, beta = initialise_fixed(init, X, Y, self.epsilon, self._weight)
715+
alpha = graduated_optimisation(
716+
alpha=alpha,
717+
X=X,
718+
Y=Y,
719+
epsilon=self.epsilon,
720+
beta=beta,
721+
lambda1=self.lambda1,
722+
lambda2=self.lambda2,
723+
weight=self._weight,
724+
beta_max=self.beta_max,
725+
max_approx=self.max_approx,
726+
max_iterations=self.max_iterations,
727+
debug=self.debug,
728+
)
729+
finally:
730+
set_threads(threads)
727731
alpha = np.concatenate(
728732
(y - np.sum(alpha * x, dtype=x.dtype, keepdims=True), alpha)
729733
)

0 commit comments

Comments
 (0)