Run Tutorial

We'll walk through an example of instrumenting and executing run code 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

  • Create your first Run and log your model metric and parameters to SigOpt

  • View your Run in the SigOpt web application

Before starting, make sure you have Python 3.6+ and pip installed on your environment.

$ python --version
$ pip --version

View this tutorial in a notebook

For notebook instructions and tutorials, check out our GitHub notebook tutorials repo or open the SigOpt Run 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.

# Install sigopt
$ pip install sigopt 
# Confirm that sigopt >= 8.0.0 is installed
$ sigopt version
# 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 
$ pip install xgboost scikit-learn

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 AI Experiments in the SigOpt app. To find your API token, go directly to the API Token page.

# Set sigopt basic configuration. You will be asked to fill in your API token, 
# and whether you want SigOpt to collect your model logs and track your model code 
$ sigopt config

Step 3 - Set Project

Runs are created within Projects. The Project allows you to sort and filter through your Runs and AI Experiments and view useful charts to gain insights into everything you've tried. In order to complete this step, first create a Project by following the Project Tutorial.

# Set the environment variable to the SigOpt project where your Run will be saved. 
$ export SIGOPT_PROJECT=my_first_project

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.

model.py
import sklearn.datasets
import sklearn.metrics
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
import sigopt

# Data preparation required to run and evaluate the sample model
X, y = sklearn.datasets.load_iris(return_X_y=True)
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.33)

# Track the name of the dataset used for your Run
sigopt.log_dataset("iris 2/3 training, full test")
# Set n_estimators as the hyperparameter to explore for your AI Experiment
sigopt.params.setdefault("n_estimators", 100)
# Track the name of the model used for your Run
sigopt.log_model("xgboost")

# Instantiate and train your sample model
model = XGBClassifier(
  n_estimators=sigopt.params.n_estimators,
  use_label_encoder=False,
  eval_metric="logloss",
)
model.fit(Xtrain, ytrain)
pred = model.predict(Xtest)

# Track the metric value and metric name for each Run
sigopt.log_metric("accuracy", sklearn.metrics.accuracy_score(pred, ytest))

Step 5 - Run the Code

Use the command sigopt run to track and execute your model. In our example, we have a python script called model.py.

$ sigopt run python model.py

Once you've run the code above, SigOpt will conveniently output a link to the Run page on our web application.

Step 6 - View Your Run

Click on the Run link to view your completed Run in our web application. Here's a view of a Run page:

Conclusion

In this tutorial, we've covered the recommended way to instrument your Run with SigOpt. After your model has been instrumented, it is easy to take advantage of SigOpt's optimization features. Optimization helps find the parameters for your model that give you the best metric (e.g. maximizing an accuracy metric).

Check out the tutorial, AI Experiment and Optimization Tutorial, to see how you can create an Experiment.

Last updated