-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timeout parameter to limit time (in seconds) of the search process (fmin) #533
Changes from 11 commits
03b21e1
f00c409
ec07715
65e9937
822e237
5be628a
daa7ba5
d46e39d
00a846f
c73e52c
ca15fd4
7b01eaf
1717f5f
d3e98f7
53ced30
497767d
3b47284
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,12 @@ | |
import os | ||
import sys | ||
import time | ||
from timeit import default_timer as timer | ||
from tqdm import tqdm | ||
|
||
import numpy as np | ||
|
||
from hyperopt.base import validate_timeout | ||
from . import pyll | ||
from .utils import coarse_utcnow | ||
from . import base | ||
|
@@ -117,6 +119,7 @@ def __init__( | |
max_queue_len=1, | ||
poll_interval_secs=1.0, | ||
max_evals=sys.maxsize, | ||
timeout=None, | ||
verbose=False, | ||
show_progressbar=True, | ||
): | ||
|
@@ -136,6 +139,8 @@ def __init__( | |
self.poll_interval_secs = poll_interval_secs | ||
self.max_queue_len = max_queue_len | ||
self.max_evals = max_evals | ||
self.timeout = timeout | ||
self.start_time = timer() | ||
self.rstate = rstate | ||
self.verbose = verbose | ||
|
||
|
@@ -234,7 +239,8 @@ def get_n_unfinished(): | |
) as progress_ctx: | ||
|
||
all_trials_complete = False | ||
while n_queued < N or (block_until_done and not all_trials_complete): | ||
while (n_queued < N or (block_until_done and not all_trials_complete)) and \ | ||
(self.timeout is None or (timer() - self.start_time) < self.timeout): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can, but version I used (a) subjectively more readable, (b) objectively faster There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Black was applied |
||
qlen = get_queue_len() | ||
while ( | ||
qlen < self.max_queue_len and n_queued < N and not self.is_cancelled | ||
|
@@ -322,6 +328,7 @@ def fmin( | |
space, | ||
algo, | ||
max_evals, | ||
timeout=None, | ||
trials=None, | ||
rstate=None, | ||
allow_trials_fmin=True, | ||
|
@@ -371,6 +378,10 @@ def fmin( | |
max_evals : int | ||
Allow up to this many function evaluations before returning. | ||
|
||
timeout : int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use style from other params :
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
Limits search time by parametrized number of seconds. | ||
None is default, it means there is no time constraint in the search process. | ||
|
||
trials : None or base.Trials (or subclass) | ||
Storage for completed, ongoing, and scheduled evaluation points. If | ||
None, then a temporary `base.Trials` instance will be created. If | ||
|
@@ -436,12 +447,15 @@ def fmin( | |
else: | ||
rstate = np.random.RandomState() | ||
|
||
validate_timeout(timeout) | ||
|
||
if allow_trials_fmin and hasattr(trials, "fmin"): | ||
return trials.fmin( | ||
fn, | ||
space, | ||
algo=algo, | ||
max_evals=max_evals, | ||
timeout=timeout, | ||
max_queue_len=max_queue_len, | ||
rstate=rstate, | ||
pass_expr_memo_ctrl=pass_expr_memo_ctrl, | ||
|
@@ -465,6 +479,7 @@ def fmin( | |
domain, | ||
trials, | ||
max_evals=max_evals, | ||
timeout=timeout, | ||
rstate=rstate, | ||
verbose=verbose, | ||
max_queue_len=max_queue_len, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
import unittest | ||
import numpy as np | ||
import nose.tools | ||
from timeit import default_timer as timer | ||
import time | ||
|
||
from hyperopt import ( | ||
fmin, | ||
|
@@ -235,3 +237,36 @@ def test_generate_trials_to_calculate(self): | |
) | ||
assert best["x"] == 0.0 | ||
assert best["y"] == 0.0 | ||
|
||
|
||
def test_timeout(): | ||
fn = lambda x: [time.sleep(1), x][1] | ||
space = hp.choice('x', range(20)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use black There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
start_time_1 = timer() | ||
fmin( | ||
fn=fn, | ||
space=space, | ||
max_evals=10, | ||
timeout=1, | ||
algo=rand.suggest, | ||
return_argmin=False, | ||
rstate=np.random.RandomState(0) | ||
) | ||
end_time_1 = timer() | ||
assert (end_time_1 - start_time_1) < 2 | ||
assert (end_time_1 - start_time_1) > 0.9 | ||
|
||
start_time_5 = timer() | ||
fmin( | ||
fn=fn, | ||
space=space, | ||
max_evals=10, | ||
timeout=5, | ||
algo=rand.suggest, | ||
return_argmin=False, | ||
rstate=np.random.RandomState(0) | ||
) | ||
end_time_5 = timer() | ||
assert (end_time_5 - start_time_5) < 6 | ||
assert (end_time_5 - start_time_5) > 4.9 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is redundant. If it's not a number, you don't need to check if it's a bool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, if the docstring says int, then I think better to check here is int, rather than any number
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not my code segment, I used check from spark implementation, but since I use the same parameter I just moved this check to separate method to base file. To not break something by accident I did not change the logic of this validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.