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.