pip install wandb
Introduction
Experiment tracking is a vital step in a modern machine learning, as without it it would be difficult to compare results and choose best parameters for our models. Here, I will show the minimum to track experiments with the help of Weights&Biases(I will refer to it as WandB).
So what is WandB?
WandB is a machine learning platform not only for keeping track of your hyperparameters, system metrics, and predictions so you can compare models, but also it enables to create ML workflows, version dataset and models, optimize hyperparameters and monitor models in production. It has several competitors. Namely, mlflow is one of the most famous ones. However, personally, I found it easier to set up and run WandB than mlflow. Moreover, it has a free tier plan, which is more than enough for personal needs.
First of all, you need to register an account there, and after that go to the User Settings and under the API keys you will find your key to interact with the service.
Then, you need to install WandB itself locally with:
Now, launch your favourite IDE (I would suggest jupyter-lab/jupyter notebook for prototyping) and connect to your dashboard with the following commands:
import wandb
'YOUR API KEY HERE'] wandb.login[
There are 5 key commands in order to work with WandB:
1. wandb.login() - authorisation in the system
2. wandb.init() - new experiment initialization
3. wandb.log() - logging metrics
4. wandb.log_artifact() - logging artifacts
5. wandb.finish() - finishing experiment
We will look into all these commands with the help of XGBoost example.
First, we’ll import libraries.
from xgboost import XGBClassifier
from xgboost.callback import EarlyStopping
import wandb
from sklearn.model_selection import StratifiedKFold
wandb.login()
Then, we will write configuration for our model.
= dict(
Config = 5,
n_splits = 42,
random_seed #params for the model
= "binary:logistic",
objective = "hist",
tree_method =200,
n_estimators=20,
early_stopping
# regularization
=5,
max_depth=17,
max_delta_step=0.632831510106799,
colsample_bytree=0.6390056763292044,
colsample_bylevel=0.487396497096089,
eta= 1,
min_child_weight = 0.25490782392352085,
gamma = 59.960195187994934,
reg_lambda = 8.529168659942826,
reg_alpha =4.71
scale_factor )
Some training code with cross-validation.
= []
clfs = []
scores = []
scores_eval #X = X_train.drop(cols2drop, axis=1)
="Project Name",
wandb.init(project=Config,
config="xgboost",
group="train",
job_type= "Training with parameters suggested with optuna with additional feats")
name
= StratifiedKFold(n_splits=Config["n_splits"], shuffle=True, random_state=Config["random_seed"])
skf
for train_index, test_index in skf.split(X, y):
= X.iloc[train_index], X.iloc[test_index]
X_train, X_test = y.iloc[train_index], y.iloc[test_index]
y_train, y_test
= EarlyStopping(
es =Config["early_stopping"],
rounds=1e-3,
min_delta=True,
save_best=True,
maximize="validation_0",
data_name="auc",
metric_name
)
= XGBClassifier(tree_method=Config["tree_method"],
clf =Config["n_estimators"],
n_estimators=Config["max_depth"],
max_depth=Config["scale_factor"],
scale_pos_weight=Config["max_delta_step"],
max_delta_step=Config["colsample_bytree"],
colsample_bytree=Config["colsample_bylevel"],
colsample_bylevel=Config["eta"],
learning_rate= Config["min_child_weight"],
min_child_weight = Config["gamma"],
gamma = Config["reg_lambda"],
reg_lambda = Config["reg_alpha"],
reg_alpha =True,
enable_categorical=Config["objective"],
objective="auc",
eval_metric=Config["random_seed"],
random_seed=[es])
callbacks
clfs.append(clf)=[(X_test, y_test)], verbose=10)
clf.fit(X_train, y_train, eval_set= clf.predict(X_valid)
preds = roc_auc_score(y_valid, preds)
auc_valid "Valid AUC": auc_valid})
wandb.log({
scores_eval.append(auc_valid)print(f"Valid AUC {auc_valid}")
'Train AUC': np.mean([v for k, v in clf.evals_result()["validation_0"].items() if "auc" in k], dtype="float16")})
wandb.log({for k, v in clf.evals_result()["validation_0"].items() if "auc" in k], dtype="float16"))
scores.append(np.mean([v
= np.mean(scores, dtype="float16") - np.std(scores, dtype="float16")
mean_score
print("mean AUC score --------->", mean_score)
print(f"mean valid AUC score {np.mean(scores_eval, dtype='float16') - np.std(scores_eval, dtype='float16')}")
"Mean AUC": mean_score, "Mean AUC valid": np.mean(scores_eval)})
wandb.log({
"xgb_classificator.json")
clf.save_model(
= wandb.Artifact(name='best_XGBoost', type='model')
artifact 'xgb_classificator.json')
artifact.add_file(
wandb.log_artifact(artifact)
wandb.finish()
With the wandb.init() method we define our parameters for WandB. Namely,
project
parameter is for the project name
config
parameter is for the model config
group
is for your group of models
job_type
is whether your model is in training mode or inference
name
is for your experiment name
I suggest giving meaningful name for your experimentation name, because it would be easier to find the right one among other experiments.
With wandb.log()
you can track any metric. It should be in a python’s dictype
data type.
WandB artifacts is a way to save your input/output data and model.
- Create an empty artifact with
wandb.Artifact()
- Add your model file or other files with
wandb.add_file()
- Call
wandb.log_artifact()
to save your files.
Finally, finish logging with wandb.finish()
.
I hope, this small article help you to start using Weights and Biases. For more information about artifacts refer to this Colab notebook. And link for official documenation and examples.