Experiment and Optimization Tutorial
We'll walk through an example of instrumenting a model in order to run a model parameter optimization with SigOpt. In this tutorial, you will learn how to:
  • Install the SigOpt Python client
  • Set your SigOpt API token
  • Set the project
  • Instrument your model
  • Configure your Experiment
  • Create your first Experiment and optimize your model metric with SigOpt
  • Visualize your Experiment results
Before starting, make sure you have Python 3.6+ and pip installed on your environment.
1
$ python --version
Copied!
1
$ pip --version
Copied!

View this tutorial in a notebook

For notebook instructions and tutorials, check out our GitHub notebook tutorials repo, open the SigOpt Experiment notebook tutorial in Google Colab.

Step 1 - Install SigOpt Python Client

Install the SigOpt Python package and the libraries required to run the model used for this tutorial.
1
# Install sigopt
2
$ pip install sigopt
3
# Confirm that sigopt >= 8.0.0 is installed
4
$ sigopt version
5
# Install XGBoost and scikit-learn. We have tested the sample model used in this tutorial with xgboost==1.5.2, and scikit-learn==1.0.2
6
$ pip install xgboost scikit-learn
Copied!

Step 2 - Set Your API Token

Once you've installed SigOpt, you need to get your API token in order to use the SigOpt API and later explore your Runs and Experiments in the SigOpt app. To find your API token, go directly to the API Token page.
If you do not have an account, sign up for a free account and get started with SigOpt today.
1
# Set sigopt basic configuration. You will be asked to fill in your API token,
2
# and whether you want SigOpt to collect your model logs and track your model code
3
$ sigopt config
Copied!

Step 3 - Set Project

Runs are created within projects. The project allows you to sort and filter through your Runs and Experiments and view useful charts to gain insights into everything you've tried.
1
# Set the environment variable to the SigOpt project where your Run will be saved.
2
$ export SIGOPT_PROJECT=my_first_project
Copied!

Step 4 - Instrument Your Model

The code below is a sample model instrumented with SigOpt where we highlight how to use SigOpt methods to log and track key model information.
Save the lines below in a script called model.py.
1
# model.py
2
import sklearn.datasets
3
import sklearn.metrics
4
from xgboost import XGBClassifier
5
import sigopt
6
7
# Data preparation required to run and evaluate the sample model
8
X, y = sklearn.datasets.load_iris(return_X_y=True)
9
Xtrain, ytrain = X[:100], y[:100]
10
11
# Track the name of the dataset used for your Run
12
sigopt.log_dataset('iris 2/3 training, full test')
13
# Set n_estimators as the hyperparameter to explore for your Experiment
14
sigopt.params.setdefault("n_estimators", 100)
15
# Track the name of the model used for your Run
16
sigopt.log_model('xgboost')
17
18
# Instantiate and train your sample model
19
model = XGBClassifier(
20
n_estimators=sigopt.params.n_estimators,
21
use_label_encoder=False,
22
eval_metric='logloss',
23
)
24
model.fit(Xtrain, ytrain)
25
pred = model.predict(X)
26
27
# Track the metric value and metric name for each Run
28
sigopt.log_metric("accuracy", sklearn.metrics.accuracy_score(pred, y))
Copied!

Step 5 - Define Your Experiment Configuration

Experiments are created in folders, your Experiment will automatically be created in the folder you set in Step 3. The experiment definition also includes a name, parameters (variables that SigOpt will suggest) and metrics. You can also set other options that you would like to run your Experiment with.
The names of the parameters are expected to match the names of the properties/attributes on sigopt.params. Similarly the metrics should match the names of the metrics passed to sigopt.log_metric calls. The budget defines how many Runs you will create for your experiment.
A SigOpt Experiment can be configured using a YAML configuration file. Save the lines below in a YAML file called experiment.yml.
1
# experiment.yml
2
name: XGBoost Optimization
3
metrics:
4
- name: accuracy
5
strategy: optimize
6
objective: maximize
7
parameters:
8
- name: n_estimators
9
bounds:
10
min: 10
11
max: 100
12
type: int
13
budget: 10
Copied!

Step 6 - Run the Code

Run the following command to start an experiment using the model from Step 4 and the experiment file from Step 5.
1
$ sigopt optimize -e experiment.yml python model.py
Copied!
SigOpt will conveniently output links to the Experiment and Runs pages on our web application.

Step 7 - Visualize Your Experiment Results

Open the Experiment link to view your Experiment in our web application. Here's a view of the Experiment page once the Experiment is completed.
From the Experiment page, open the History tab to see the list of Runs for your Experiment. Click on any individual run ID link to view any completed Run. Here's a view of a Run page:

Conclusion

In this tutorial, we covered the recommended way to instrument and optimize your model, and visualize your results with SigOpt. You learned that experiments are collections of runs that search through a defined parameter space to satisfy the experiment search criteria.
Check out our tutorial, Runs Tutorial, for a closer look at a single Run, and see how to track one-off runs without creating an experiment.