This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Neptune-Optuna.py
101 lines (65 loc) · 2.55 KB
/
Neptune-Optuna.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Neptune + Optuna
# Before you start
## Install dependencies
## Import libraries
import lightgbm as lgb
import optuna
from sklearn.datasets import load_breast_cancer
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
## Create a sample `objective` function for Optuna
def objective(trial):
data, target = load_breast_cancer(return_X_y=True)
train_x, test_x, train_y, test_y = train_test_split(data, target, test_size=0.25)
dtrain = lgb.Dataset(train_x, label=train_y)
param = {
'verbose': -1,
'objective': 'binary',
'metric': 'binary_logloss',
'num_leaves': trial.suggest_int('num_leaves', 2, 256),
'feature_fraction': trial.suggest_uniform('feature_fraction', 0.2, 1.0),
'bagging_fraction': trial.suggest_uniform('bagging_fraction', 0.2, 1.0),
'min_child_samples': trial.suggest_int('min_child_samples', 3, 100),
}
gbm = lgb.train(param, dtrain)
preds = gbm.predict(test_x)
accuracy = roc_auc_score(test_y, preds)
return accuracy
## Initialize Neptune
import neptune
neptune.init(api_token='ANONYMOUS', project_qualified_name='shared/optuna-integration')
# Quickstart
## Step 1: Create an Experiment
neptune.create_experiment('optuna-sweep')
## Step 2: Create the Neptune Callback
import neptunecontrib.monitoring.optuna as opt_utils
neptune_callback = opt_utils.NeptuneCallback()
## Step 3: Run Optuna with the Neptune Callback
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=100, callbacks=[neptune_callback])
## Step 4: Stop logging
neptune.stop()
# Advanced Options
## Log charts and study object during sweep
# Create experiment
neptune.create_experiment('optuna-sweep-advanced')
# Create callback to log advanced options during the sweep
neptune_callback = opt_utils.NeptuneCallback(log_study=True, log_charts=True)
# Run Optuna with Neptune Callback
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=20, callbacks=[neptune_callback])
# Stop logging
neptune.stop()
## Log charts and study object after the sweep
# Create experiment
neptune.create_experiment('optuna-sweep-advanced')
# Create Neptune callback
neptune_callback = opt_utils.NeptuneCallback()
# Run Optuna with Neptune Callback
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=100, callbacks=[neptune_callback])
# Log Optuna charts and study object after the sweep is complete
opt_utils.log_study_info(study)
# Stop logging
neptune.stop()
# Explore results in the Neptune UI