Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(
np.zeros((16, 20), np.float32),
np.zeros((16, ), np.int32),
batch_size=1,
epochs=1,
callbacks=[KerasPruningCallback(trial, 'accuracy')],
verbose=0)
return 1.0
study = optuna.create_study(pruner=DeterministicPruner(True))
study.optimize(objective, n_trials=1)
assert study.trials[0].state == optuna.structs.TrialState.PRUNED
study = optuna.create_study(pruner=DeterministicPruner(False))
study.optimize(objective, n_trials=1)
assert study.trials[0].state == optuna.structs.TrialState.COMPLETE
assert study.trials[0].value == 1.0
def test_trials_dataframe_with_failure(storage_mode, cache_mode):
# type: (str, bool) -> None
def f(trial):
# type: (optuna.trial.Trial) -> float
x = trial.suggest_int('x', 1, 1)
y = trial.suggest_categorical('y', (2.5, ))
trial.set_user_attr('train_loss', 3)
raise ValueError()
return x + y # 3.5
with StorageSupplier(storage_mode, cache_mode) as storage:
study = optuna.create_study(storage=storage)
study.optimize(f, n_trials=3, catch=(ValueError,))
df = study.trials_dataframe()
# Change index to access rows via trial number.
df.set_index('number', inplace=True, drop=False)
assert len(df) == 3
# TODO(Yanase): Remove number from system_attrs after adding TrialModel.number.
# non-nested: 5, params: 2, user_attrs: 1 system_attrs: 2
assert len(df.columns) == 10
for i in range(3):
assert df.number[i] == i
assert df.state[i] == optuna.structs.TrialState.FAIL
assert df.value[i] is None
assert isinstance(df.datetime_start[i], pd.Timestamp)
assert isinstance(df.datetime_complete[i], pd.Timestamp)
assert df.params_x[i] == 1
assert df.params_y[i] == 2.5
def test_optimize_without_gc(collect_mock):
# type: (Mock) -> None
study = optuna.create_study()
study.optimize(func, n_trials=10, gc_after_trial=False)
check_study(study)
assert collect_mock.call_count == 0
def test_skopt_kwargs():
# type: () -> None
sampler = optuna.integration.SkoptSampler(skopt_kwargs={'base_estimator': "GBRT"})
study = optuna.create_study(sampler=sampler)
with patch('skopt.Optimizer') as mock_object:
study.optimize(lambda t: t.suggest_int('x', -10, 10), n_trials=2)
dimensions = [space.Integer(-10, 10)]
assert mock_object.mock_calls[0] == call(dimensions, base_estimator="GBRT")
def test_update_cache_none_value():
# type: () -> None
storage = InMemoryStorage()
study = optuna.create_study(storage=storage)
study.optimize(lambda trial: -1 * trial.number, n_trials=1)
assert storage.best_trial_id == 0
# The objective value is None.
study.optimize(lambda trial: None, n_trials=1) # type: ignore
assert storage.best_trial_id == 0
def test_callbacks(n_jobs):
# type: (int) -> None
study = optuna.create_study()
def objective(trial):
# type: (optuna.trial.Trial) -> float
return trial.suggest_int('x', 1, 1)
# Empty callback list.
study.optimize(objective, callbacks=[], n_trials=10, n_jobs=n_jobs)
# A callback.
values = []
callbacks = [lambda study, trial: values.append(trial.value)]
study.optimize(objective, callbacks=callbacks, n_trials=10, n_jobs=n_jobs)
assert values == [1] * 10
# Two callbacks.
# The KerasPruningCallback checks for pruning condition every epoch.
model.fit(x_train,
y_train,
batch_size=BATCHSIZE,
callbacks=[KerasPruningCallback(trial, 'val_acc')],
epochs=EPOCHS,
validation_data=(x_test, y_test),
verbose=1)
# Evaluate the model accuracy on the test set.
score = model.evaluate(x_test, y_test, verbose=0)
return score[1]
if __name__ == '__main__':
study = optuna.create_study(direction='maximize', pruner=optuna.pruners.MedianPruner())
study.optimize(objective, n_trials=100)
pruned_trials = [t for t in study.trials if t.state == optuna.structs.TrialState.PRUNED]
complete_trials = [t for t in study.trials if t.state == optuna.structs.TrialState.COMPLETE]
print('Study statistics: ')
print(' Number of finished trials: ', len(study.trials))
print(' Number of pruned trials: ', len(pruned_trials))
print(' Number of complete trials: ', len(complete_trials))
print('Best trial:')
trial = study.best_trial
print(' Value: ', trial.value)
print(' Params: ')
for key, value in trial.params.items():
print(' {}: {}'.format(key, value))
param['bagging_temperature'] = trial.suggest_uniform('bagging_temperature', 0, 10)
elif param['bootstrap_type'] == 'Bernoulli':
param['subsample'] = trial.suggest_uniform('subsample', 0.1, 1)
gbm = cb.CatBoostClassifier(**param)
gbm.fit(train_x, train_y, eval_set=[(test_x, test_y)], verbose=0, early_stopping_rounds=100)
preds = gbm.predict(test_x)
pred_labels = np.rint(preds)
accuracy = accuracy_score(test_y, pred_labels)
return accuracy
if __name__ == '__main__':
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=100, timeout=600)
print('Number of finished trials: {}'.format(len(study.trials)))
print('Best trial:')
trial = study.best_trial
print(' Value: {}'.format(trial.value))
print(' Params: ')
for key, value in trial.params.items():
print(' {}: {}'.format(key, value))
optimizer_params={'rescale_grad': 1.0 / BATCHSIZE},
num_epoch=EPOCH)
# Compute the accuracy on the entire test set.
test = mx.io.NDArrayIter(
data=mnist['test_data'],
label=mnist['test_label'],
batch_size=BATCHSIZE)
accuracy = model.score(eval_data=test, eval_metric='acc')[0]
return accuracy[1]
if __name__ == '__main__':
import optuna
study = optuna.create_study(direction='maximize', pruner=optuna.pruners.MedianPruner())
study.optimize(objective, n_trials=100, timeout=600)
pruned_trials = [t for t in study.trials if t.state == optuna.structs.TrialState.PRUNED]
complete_trials = [t for t in study.trials if t.state == optuna.structs.TrialState.COMPLETE]
print('Study statistics: ')
print(' Number of finished trials: ', len(study.trials))
print(' Number of pruned trials: ', len(pruned_trials))
print(' Number of complete trials: ', len(complete_trials))
print('Best trial:')
trial = study.best_trial
print(' Value: ', trial.value)
print(' Params: ')
for key, value in trial.params.items():
def take_action(self, parsed_args):
# type: (Namespace) -> None
config = optuna.config.load_optuna_config(self.app_args.config)
storage_url = get_storage_url(self.app_args.storage, config)
storage = optuna.storages.RDBStorage(storage_url)
study_name = optuna.create_study(
storage,
study_name=parsed_args.study_name,
direction=parsed_args.direction,
load_if_exists=parsed_args.skip_if_exists).study_name
print(study_name)