1

I have this code for running AutoKeras for X seconds

def run_auto_keras(x_train, y_train, x_eval, y_eval):
    print('Starting AutoKeras')
    with timeout(TIME_IN_SEC):
        clf = ak.StructuredDataClassifier(
            max_trials=NUM_MODELS, directory=os.getcwd())
        clf.fit(x_train, y_train, epochs=NUM_EPOCHS)

        return clf.evaluate(x_eval, y_eval, batch_size=BATCH_SZ)

if not TRAIN_PATH.endswith('.csv'):
    raise Exception(f'{TRAIN_PATH} is not CSV')

train = pd.read_csv(TRAIN_PATH)

if TARGET_COL not in train.columns:
    raise Exception(f'{TARGET_COL} not in {TRAIN_PATH}')

mask = np.random.rand(len(train.index)) == SPLIT_P

y = train.pop(TARGET_COL)

x_train = train[mask]
y_train = y[mask]

x_eval = train[~mask]
y_eval = y.pop(TARGET_COL)[~mask]

x_train = x_train.to_numpy()
y_train = y_train.to_numpy()

x_eval = x_eval.to_numpy()
y_eval = y_eval.to_numpy()

try:
    eval_score = run_auto_keras(x_train, y_train, x_eval, y_eval)
    pd.DataFrame(data=[eval_score, NUM_MODELS, 'AutoKeras'], columns=[
                 'result', 'num_ensemble', 'ta2']).to_csv(LOSS_PATH, index=False)
except TimeoutError:
    pd.DataFrame(data=[0, NUM_MODELS, 'AutoKeras'], columns=[
                 'result', 'num_ensemble', 'ta2']).to_csv(LOSS_PATH, index=False)

I read in a blog that this method for running a process for X seconds is probably the best

@contextmanager
def timeout(time):
    signal.signal(signal.SIGALRM, raise_timeout)
    signal.alarm(time)

    try:
        yield
    except TimeoutError:
        pass
    finally:
        signal.signal(signal.SIGALRM, signal.SIG_IGN)

def raise_timeout(signum, frame):
    raise TimeoutError

Is this a proper way to run a function for X seconds? What I want is if it completes to return a result if not then just throw an error.

  • Check [Timeout on a function call](https://stackoverflow.com/questions/492519/timeout-on-a-function-call) – SpiderPig1297 Oct 03 '20 at 00:42
  • @SpiderPig1297 this seems the same as what I posted above no? Also I can't use a while loop in my code cause it instantiates an AutoML object that runs for a unknown amount of time and as you can see above that's called by .fit so if I have that in while it'll stop it on the next while iteration. –  Oct 03 '20 at 01:55
  • Well, the first answer is but the there is another answer suggesting to use `multiprocessing` library. – SpiderPig1297 Oct 03 '20 at 08:43

1 Answers1

-1

Try using:

import time

time.sleep(1)

this will pause for x seconds.

Henry Bass
  • 19
  • 6