mirror of
https://github.com/vale981/ray
synced 2025-03-06 18:41:40 -05:00

Removes all ML related code from `ray.util` Removes: - `ray.util.xgboost` - `ray.util.lightgbm` - `ray.util.horovod` - `ray.util.ray_lightning` Moves `ray.util.ml_utils` to other locations Closes #23900 Signed-off-by: Amog Kamsetty <amogkamsetty@yahoo.com> Signed-off-by: Kai Fricke <kai@anyscale.com> Co-authored-by: Kai Fricke <kai@anyscale.com>
1384 lines
82 KiB
Text
1384 lines
82 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "edce67b9",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Tuning XGBoost parameters\n",
|
|
"\n",
|
|
"(tune-xgboost-ref)=\n",
|
|
"\n",
|
|
"XGBoost is currently one of the most popular machine learning algorithms. It performs\n",
|
|
"very well on a large selection of tasks, and was the key to success in many Kaggle\n",
|
|
"competitions.\n",
|
|
"\n",
|
|
"```{image} /images/xgboost_logo.png\n",
|
|
":align: center\n",
|
|
":alt: XGBoost\n",
|
|
":target: https://xgboost.readthedocs.io/en/latest/\n",
|
|
":width: 200px\n",
|
|
"```\n",
|
|
"\n",
|
|
"This tutorial will give you a quick introduction to XGBoost, show you how\n",
|
|
"to train an XGBoost model, and then guide you on how to optimize XGBoost\n",
|
|
"parameters using Tune to get the best performance. We tackle the following topics:\n",
|
|
"\n",
|
|
"```{contents}\n",
|
|
":depth: 2\n",
|
|
"```\n",
|
|
"\n",
|
|
":::{note}\n",
|
|
"To run this tutorial, you will need to install the following:\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"$ pip install xgboost\n",
|
|
"```\n",
|
|
":::\n",
|
|
"\n",
|
|
"## What is XGBoost\n",
|
|
"\n",
|
|
"XGBoost is an acronym for e**X**treme **G**radient **Boost**ing. Internally,\n",
|
|
"XGBoost uses [decision trees](https://en.wikipedia.org/wiki/Decision_tree). Instead\n",
|
|
"of training just one large decision tree, XGBoost and other related algorithms train\n",
|
|
"many small decision trees. The intuition behind this is that even though single\n",
|
|
"decision trees can be inaccurate and suffer from high variance,\n",
|
|
"combining the output of a large number of these weak learners can actually lead to\n",
|
|
"strong learner, resulting in better predictions and less variance.\n",
|
|
"\n",
|
|
":::{figure} /images/tune-xgboost-ensemble.svg\n",
|
|
":alt: Single vs. ensemble learning\n",
|
|
"\n",
|
|
"A single decision tree (left) might be able to get to an accuracy of 70%\n",
|
|
"for a binary classification task. By combining the output of several small\n",
|
|
"decision trees, an ensemble learner (right) might end up with a higher accuracy\n",
|
|
"of 90%.\n",
|
|
":::\n",
|
|
"\n",
|
|
"Boosting algorithms start with a single small decision tree and evaluate how well\n",
|
|
"it predicts the given examples. When building the next tree, those samples that have\n",
|
|
"been misclassified before have a higher chance of being used to generate the tree.\n",
|
|
"This is useful because it avoids overfitting to samples that can be easily classified\n",
|
|
"and instead tries to come up with models that are able to classify hard examples, too.\n",
|
|
"Please see [here for a more thorough introduction to bagging and boosting algorithms](https://towardsdatascience.com/ensemble-methods-bagging-boosting-and-stacking-c9214a10a205).\n",
|
|
"\n",
|
|
"There are many boosting algorithms. In their core, they are all very similar. XGBoost\n",
|
|
"uses second-level derivatives to find splits that maximize the *gain* (the inverse of\n",
|
|
"the *loss*) - hence the name. In practice, there really is no drawback in using\n",
|
|
"XGBoost over other boosting algorithms - in fact, it usually shows the best performance.\n",
|
|
"\n",
|
|
"## Training a simple XGBoost classifier\n",
|
|
"\n",
|
|
"Let's first see how a simple XGBoost classifier can be trained. We'll use the\n",
|
|
"`breast_cancer`-Dataset included in the `sklearn` dataset collection. This is\n",
|
|
"a binary classification dataset. Given 30 different input features, our task is to\n",
|
|
"learn to identify subjects with breast cancer and those without.\n",
|
|
"\n",
|
|
"Here is the full code to train a simple XGBoost model:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "77b3c71c",
|
|
"metadata": {
|
|
"pycharm": {
|
|
"name": "#%%\n"
|
|
},
|
|
"vscode": {
|
|
"languageId": "python"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Accuracy: 0.9650\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import sklearn.datasets\n",
|
|
"import sklearn.metrics\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"import xgboost as xgb\n",
|
|
"\n",
|
|
"\n",
|
|
"def train_breast_cancer(config):\n",
|
|
" # Load dataset\n",
|
|
" data, labels = sklearn.datasets.load_breast_cancer(return_X_y=True)\n",
|
|
" # Split into train and test set\n",
|
|
" train_x, test_x, train_y, test_y = train_test_split(\n",
|
|
" data, labels, test_size=0.25)\n",
|
|
" # Build input matrices for XGBoost\n",
|
|
" train_set = xgb.DMatrix(train_x, label=train_y)\n",
|
|
" test_set = xgb.DMatrix(test_x, label=test_y)\n",
|
|
" # Train the classifier\n",
|
|
" results = {}\n",
|
|
" bst = xgb.train(\n",
|
|
" config,\n",
|
|
" train_set,\n",
|
|
" evals=[(test_set, \"eval\")],\n",
|
|
" evals_result=results,\n",
|
|
" verbose_eval=False)\n",
|
|
" return results\n",
|
|
"\n",
|
|
"\n",
|
|
"if __name__ == \"__main__\":\n",
|
|
" results = train_breast_cancer({\n",
|
|
" \"objective\": \"binary:logistic\",\n",
|
|
" \"eval_metric\": [\"logloss\", \"error\"]\n",
|
|
" })\n",
|
|
" accuracy = 1. - results[\"eval\"][\"error\"][-1]\n",
|
|
" print(f\"Accuracy: {accuracy:.4f}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ec2a13f8",
|
|
"metadata": {
|
|
"pycharm": {
|
|
"name": "#%% md\n"
|
|
}
|
|
},
|
|
"source": [
|
|
"As you can see, the code is quite simple. First, the dataset is loaded and split\n",
|
|
"into a `test` and `train` set. The XGBoost model is trained with `xgb.train()`.\n",
|
|
"XGBoost automatically evaluates metrics we specified on the test set. In our case\n",
|
|
"it calculates the *logloss* and the prediction *error*, which is the percentage of\n",
|
|
"misclassified examples. To calculate the accuracy, we just have to subtract the error\n",
|
|
"from `1.0`. Even in this simple example, most runs result\n",
|
|
"in a good accuracy of over `0.90`.\n",
|
|
"\n",
|
|
"Maybe you have noticed the `config` parameter we pass to the XGBoost algorithm. This\n",
|
|
"is a {class}`dict` in which you can specify parameters for the XGBoost algorithm. In this\n",
|
|
"simple example, the only parameters we passed are the `objective` and `eval_metric` parameters.\n",
|
|
"The value `binary:logistic` tells XGBoost that we aim to train a logistic regression model for\n",
|
|
"a binary classification task. You can find an overview over all valid objectives\n",
|
|
"[here in the XGBoost documentation](https://xgboost.readthedocs.io/en/latest/parameter.html#learning-task-parameters).\n",
|
|
"\n",
|
|
"## XGBoost Hyperparameters\n",
|
|
"\n",
|
|
"Even with the default settings, XGBoost was able to get to a good accuracy on the\n",
|
|
"breast cancer dataset. However, as in many machine learning algorithms, there are\n",
|
|
"many knobs to tune which might lead to even better performance. Let's explore some of\n",
|
|
"them below.\n",
|
|
"\n",
|
|
"### Maximum tree depth\n",
|
|
"\n",
|
|
"Remember that XGBoost internally uses many decision tree models to come up with\n",
|
|
"predictions. When training a decision tree, we need to tell the algorithm how\n",
|
|
"large the tree may get. The parameter for this is called the tree *depth*.\n",
|
|
"\n",
|
|
":::{figure} /images/tune-xgboost-depth.svg\n",
|
|
":align: center\n",
|
|
":alt: Decision tree depth\n",
|
|
"\n",
|
|
"In this image, the left tree has a depth of 2, and the right tree a depth of 3.\n",
|
|
"Note that with each level, $2^{(d-1)}$ splits are added, where *d* is the depth\n",
|
|
"of the tree.\n",
|
|
":::\n",
|
|
"\n",
|
|
"Tree depth is a property that concerns the model complexity. If you only allow short\n",
|
|
"trees, the models are likely not very precise - they underfit the data. If you allow\n",
|
|
"very large trees, the single models are likely to overfit to the data. In practice,\n",
|
|
"a number between `2` and `6` is often a good starting point for this parameter.\n",
|
|
"\n",
|
|
"XGBoost's default value is `3`.\n",
|
|
"\n",
|
|
"### Minimum child weight\n",
|
|
"\n",
|
|
"When a decision tree creates new leaves, it splits up the remaining data at one node\n",
|
|
"into two groups. If there are only few samples in one of these groups, it often\n",
|
|
"doesn't make sense to split it further. One of the reasons for this is that the\n",
|
|
"model is harder to train when we have fewer samples.\n",
|
|
"\n",
|
|
":::{figure} /images/tune-xgboost-weight.svg\n",
|
|
":align: center\n",
|
|
":alt: Minimum child weight\n",
|
|
"\n",
|
|
"In this example, we start with 100 examples. At the first node, they are split\n",
|
|
"into 4 and 96 samples, respectively. In the next step, our model might find\n",
|
|
"that it doesn't make sense to split the 4 examples more. It thus only continues\n",
|
|
"to add leaves on the right side.\n",
|
|
":::\n",
|
|
"\n",
|
|
"The parameter used by the model to decide if it makes sense to split a node is called\n",
|
|
"the *minimum child weight*. In the case of linear regression, this is just the absolute\n",
|
|
"number of nodes requried in each child. In other objectives, this value is determined\n",
|
|
"using the weights of the examples, hence the name.\n",
|
|
"\n",
|
|
"The larger the value, the more constrained the trees are and the less deep they will be.\n",
|
|
"This parameter thus also affects the model complexity. Values can range between 0\n",
|
|
"and infinity and are dependent on the sample size. For our ca. 500 examples in the\n",
|
|
"breast cancer dataset, values between `0` and `10` should be sensible.\n",
|
|
"\n",
|
|
"XGBoost's default value is `1`.\n",
|
|
"\n",
|
|
"### Subsample size\n",
|
|
"\n",
|
|
"Each decision tree we add is trained on a subsample of the total training dataset.\n",
|
|
"The probabilities for the samples are weighted according to the XGBoost algorithm,\n",
|
|
"but we can decide on which fraction of the samples we want to train each decision\n",
|
|
"tree on.\n",
|
|
"\n",
|
|
"Setting this value to `0.7` would mean that we randomly sample `70%` of the\n",
|
|
"training dataset before each training iteration.\n",
|
|
"\n",
|
|
"XGBoost's default value is `1`.\n",
|
|
"\n",
|
|
"### Learning rate / Eta\n",
|
|
"\n",
|
|
"Remember that XGBoost sequentially trains many decision trees, and that later trees\n",
|
|
"are more likely trained on data that has been misclassified by prior trees. In effect\n",
|
|
"this means that earlier trees make decisions for easy samples (i.e. those samples that\n",
|
|
"can easily be classified) and later trees make decisions for harder samples. It is then\n",
|
|
"sensible to assume that the later trees are less accurate than earlier trees.\n",
|
|
"\n",
|
|
"To address this fact, XGBoost uses a parameter called *Eta*, which is sometimes called\n",
|
|
"the *learning rate*. Don't confuse this with learning rates from gradient descent!\n",
|
|
"The original [paper on stochastic gradient boosting](https://www.sciencedirect.com/science/article/abs/pii/S0167947301000652)\n",
|
|
"introduces this parameter like so:\n",
|
|
"\n",
|
|
"$$\n",
|
|
"F_m(x) = F_{m-1}(x) + \\eta \\cdot \\gamma_{lm} \\textbf{1}(x \\in R_{lm})\n",
|
|
"$$\n",
|
|
"\n",
|
|
"This is just a complicated way to say that when we train we new decision tree,\n",
|
|
"represented by $\\gamma_{lm} \\textbf{1}(x \\in R_{lm})$, we want to dampen\n",
|
|
"its effect on the previous prediction $F_{m-1}(x)$ with a factor\n",
|
|
"$\\eta$.\n",
|
|
"\n",
|
|
"Typical values for this parameter are between `0.01` and `` 0.3` ``.\n",
|
|
"\n",
|
|
"XGBoost's default value is `0.3`.\n",
|
|
"\n",
|
|
"### Number of boost rounds\n",
|
|
"\n",
|
|
"Lastly, we can decide on how many boosting rounds we perform, which means how\n",
|
|
"many decision trees we ultimately train. When we do heavy subsampling or use small\n",
|
|
"learning rate, it might make sense to increase the number of boosting rounds.\n",
|
|
"\n",
|
|
"XGBoost's default value is `10`.\n",
|
|
"\n",
|
|
"### Putting it together\n",
|
|
"\n",
|
|
"Let's see how this looks like in code! We just need to adjust our `config` dict:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "35073e88",
|
|
"metadata": {
|
|
"pycharm": {
|
|
"name": "#%%\n"
|
|
},
|
|
"vscode": {
|
|
"languageId": "python"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Accuracy: 0.9790\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"if __name__ == \"__main__\":\n",
|
|
" config = {\n",
|
|
" \"objective\": \"binary:logistic\",\n",
|
|
" \"eval_metric\": [\"logloss\", \"error\"],\n",
|
|
" \"max_depth\": 2,\n",
|
|
" \"min_child_weight\": 0,\n",
|
|
" \"subsample\": 0.8,\n",
|
|
" \"eta\": 0.2\n",
|
|
" }\n",
|
|
" results = train_breast_cancer(config)\n",
|
|
" accuracy = 1. - results[\"eval\"][\"error\"][-1]\n",
|
|
" print(f\"Accuracy: {accuracy:.4f}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "69cf0c13",
|
|
"metadata": {
|
|
"pycharm": {
|
|
"name": "#%% md\n"
|
|
}
|
|
},
|
|
"source": [
|
|
"The rest stays the same. Please note that we do not adjust the `num_boost_rounds` here.\n",
|
|
"The result should also show a high accuracy of over 90%.\n",
|
|
"\n",
|
|
"## Tuning the configuration parameters\n",
|
|
"\n",
|
|
"XGBoosts default parameters already lead to a good accuracy, and even our guesses in the\n",
|
|
"last section should result in accuracies well above 90%. However, our guesses were\n",
|
|
"just that: guesses. Often we do not know what combination of parameters would actually\n",
|
|
"lead to the best results on a machine learning task.\n",
|
|
"\n",
|
|
"Unfortunately, there are infinitely many combinations of hyperparameters we could try\n",
|
|
"out. Should we combine `max_depth=3` with `subsample=0.8` or with `subsample=0.9`?\n",
|
|
"What about the other parameters?\n",
|
|
"\n",
|
|
"This is where hyperparameter tuning comes into play. By using tuning libraries such as\n",
|
|
"Ray Tune we can try out combinations of hyperparameters. Using sophisticated search\n",
|
|
"strategies, these parameters can be selected so that they are likely to lead to good\n",
|
|
"results (avoiding an expensive *exhaustive search*). Also, trials that do not perform\n",
|
|
"well can be preemptively stopped to reduce waste of computing resources. Lastly, Ray Tune\n",
|
|
"also takes care of training these runs in parallel, greatly increasing search speed.\n",
|
|
"\n",
|
|
"Let's start with a basic example on how to use Tune for this. We just need to make\n",
|
|
"a few changes to our code-block:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "ff856a82",
|
|
"metadata": {
|
|
"pycharm": {
|
|
"name": "#%%\n"
|
|
},
|
|
"vscode": {
|
|
"languageId": "python"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2022-07-22 15:52:52,004\tINFO services.py:1483 -- View the Ray dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8268\u001b[39m\u001b[22m\n",
|
|
"2022-07-22 15:52:55,858\tWARNING function_trainable.py:619 -- Function checkpointing is disabled. This may result in unexpected behavior when using checkpointing features or certain schedulers. To enable, set the train function arguments to be `func(config, checkpoint_dir=None)`.\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"== Status ==<br>Current time: 2022-07-22 15:53:04 (running for 00:00:07.77)<br>Memory usage on this node: 10.5/16.0 GiB<br>Using FIFO scheduling algorithm.<br>Resources requested: 0/16 CPUs, 0/0 GPUs, 0.0/4.57 GiB heap, 0.0/2.0 GiB objects<br>Result logdir: /Users/kai/ray_results/train_breast_cancer_2022-07-22_15-52-48<br>Number of trials: 10/10 (10 TERMINATED)<br><table>\n",
|
|
"<thead>\n",
|
|
"<tr><th>Trial name </th><th>status </th><th>loc </th><th style=\"text-align: right;\"> eta</th><th style=\"text-align: right;\"> max_depth</th><th style=\"text-align: right;\"> min_child_weight</th><th style=\"text-align: right;\"> subsample</th><th style=\"text-align: right;\"> acc</th><th style=\"text-align: right;\"> iter</th><th style=\"text-align: right;\"> total time (s)</th></tr>\n",
|
|
"</thead>\n",
|
|
"<tbody>\n",
|
|
"<tr><td>train_breast_cancer_f8669_00000</td><td>TERMINATED</td><td>127.0.0.1:48852</td><td style=\"text-align: right;\">0.0069356 </td><td style=\"text-align: right;\"> 5</td><td style=\"text-align: right;\"> 3</td><td style=\"text-align: right;\"> 0.823504</td><td style=\"text-align: right;\">0.944056</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0316169</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_f8669_00001</td><td>TERMINATED</td><td>127.0.0.1:48857</td><td style=\"text-align: right;\">0.00145619 </td><td style=\"text-align: right;\"> 6</td><td style=\"text-align: right;\"> 3</td><td style=\"text-align: right;\"> 0.832947</td><td style=\"text-align: right;\">0.958042</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0328588</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_f8669_00002</td><td>TERMINATED</td><td>127.0.0.1:48858</td><td style=\"text-align: right;\">0.00108208 </td><td style=\"text-align: right;\"> 7</td><td style=\"text-align: right;\"> 3</td><td style=\"text-align: right;\"> 0.987319</td><td style=\"text-align: right;\">0.944056</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0319381</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_f8669_00003</td><td>TERMINATED</td><td>127.0.0.1:48859</td><td style=\"text-align: right;\">0.00530429 </td><td style=\"text-align: right;\"> 8</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.615691</td><td style=\"text-align: right;\">0.923077</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.028388 </td></tr>\n",
|
|
"<tr><td>train_breast_cancer_f8669_00004</td><td>TERMINATED</td><td>127.0.0.1:48860</td><td style=\"text-align: right;\">0.000721843</td><td style=\"text-align: right;\"> 8</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.650973</td><td style=\"text-align: right;\">0.958042</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0299618</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_f8669_00005</td><td>TERMINATED</td><td>127.0.0.1:48861</td><td style=\"text-align: right;\">0.0074509 </td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.738341</td><td style=\"text-align: right;\">0.874126</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0193682</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_f8669_00006</td><td>TERMINATED</td><td>127.0.0.1:48862</td><td style=\"text-align: right;\">0.0879882 </td><td style=\"text-align: right;\"> 8</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.671576</td><td style=\"text-align: right;\">0.944056</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0267372</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_f8669_00007</td><td>TERMINATED</td><td>127.0.0.1:48863</td><td style=\"text-align: right;\">0.0765404 </td><td style=\"text-align: right;\"> 7</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.708157</td><td style=\"text-align: right;\">0.965035</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0276129</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_f8669_00008</td><td>TERMINATED</td><td>127.0.0.1:48864</td><td style=\"text-align: right;\">0.000627649</td><td style=\"text-align: right;\"> 6</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.81121 </td><td style=\"text-align: right;\">0.951049</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0310998</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_f8669_00009</td><td>TERMINATED</td><td>127.0.0.1:48865</td><td style=\"text-align: right;\">0.000383711</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 3</td><td style=\"text-align: right;\"> 0.990579</td><td style=\"text-align: right;\">0.93007 </td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0274954</td></tr>\n",
|
|
"</tbody>\n",
|
|
"</table><br><br>"
|
|
],
|
|
"text/plain": [
|
|
"<IPython.core.display.HTML object>"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2022-07-22 15:52:57,385\tINFO plugin_schema_manager.py:52 -- Loading the default runtime env schemas: ['/Users/kai/coding/ray/python/ray/_private/runtime_env/../../runtime_env/schemas/working_dir_schema.json', '/Users/kai/coding/ray/python/ray/_private/runtime_env/../../runtime_env/schemas/pip_schema.json'].\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Result for train_breast_cancer_f8669_00000:\n",
|
|
" date: 2022-07-22_15-53-00\n",
|
|
" done: true\n",
|
|
" experiment_id: 07d10c5f31e74133b53272b7ccf9c528\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" mean_accuracy: 0.9440559440559441\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 48852\n",
|
|
" time_since_restore: 0.031616926193237305\n",
|
|
" time_this_iter_s: 0.031616926193237305\n",
|
|
" time_total_s: 0.031616926193237305\n",
|
|
" timestamp: 1658501580\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: f8669_00000\n",
|
|
" warmup_time: 0.0027849674224853516\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_f8669_00009:\n",
|
|
" date: 2022-07-22_15-53-04\n",
|
|
" done: true\n",
|
|
" experiment_id: bc0d5dd2d079432b859faac8a18928f0\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" mean_accuracy: 0.9300699300699301\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 48865\n",
|
|
" time_since_restore: 0.027495384216308594\n",
|
|
" time_this_iter_s: 0.027495384216308594\n",
|
|
" time_total_s: 0.027495384216308594\n",
|
|
" timestamp: 1658501584\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: f8669_00009\n",
|
|
" warmup_time: 0.005235910415649414\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_f8669_00001:\n",
|
|
" date: 2022-07-22_15-53-04\n",
|
|
" done: true\n",
|
|
" experiment_id: 4b10d350d4374a0d9e7d0c3b1d4e3203\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" mean_accuracy: 0.958041958041958\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 48857\n",
|
|
" time_since_restore: 0.032858848571777344\n",
|
|
" time_this_iter_s: 0.032858848571777344\n",
|
|
" time_total_s: 0.032858848571777344\n",
|
|
" timestamp: 1658501584\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: f8669_00001\n",
|
|
" warmup_time: 0.004731178283691406\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_f8669_00008:\n",
|
|
" date: 2022-07-22_15-53-04\n",
|
|
" done: true\n",
|
|
" experiment_id: 91c25cbbeb6f409d93e1d6537cb8e1ee\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" mean_accuracy: 0.951048951048951\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 48864\n",
|
|
" time_since_restore: 0.031099796295166016\n",
|
|
" time_this_iter_s: 0.031099796295166016\n",
|
|
" time_total_s: 0.031099796295166016\n",
|
|
" timestamp: 1658501584\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: f8669_00008\n",
|
|
" warmup_time: 0.003270864486694336\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_f8669_00005:\n",
|
|
" date: 2022-07-22_15-53-04\n",
|
|
" done: true\n",
|
|
" experiment_id: d225b0fb59e14da7adba952456ccf1d5\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" mean_accuracy: 0.8741258741258742\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 48861\n",
|
|
" time_since_restore: 0.01936817169189453\n",
|
|
" time_this_iter_s: 0.01936817169189453\n",
|
|
" time_total_s: 0.01936817169189453\n",
|
|
" timestamp: 1658501584\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: f8669_00005\n",
|
|
" warmup_time: 0.003901958465576172\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_f8669_00004:\n",
|
|
" date: 2022-07-22_15-53-04\n",
|
|
" done: true\n",
|
|
" experiment_id: 322484af6ea5422f8aaf8ff6a91af4f7\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" mean_accuracy: 0.958041958041958\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 48860\n",
|
|
" time_since_restore: 0.029961824417114258\n",
|
|
" time_this_iter_s: 0.029961824417114258\n",
|
|
" time_total_s: 0.029961824417114258\n",
|
|
" timestamp: 1658501584\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: f8669_00004\n",
|
|
" warmup_time: 0.003547191619873047\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_f8669_00002:\n",
|
|
" date: 2022-07-22_15-53-04\n",
|
|
" done: true\n",
|
|
" experiment_id: 3f588954160b42ce8ce200f68127ebcd\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" mean_accuracy: 0.9440559440559441\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 48858\n",
|
|
" time_since_restore: 0.03193807601928711\n",
|
|
" time_this_iter_s: 0.03193807601928711\n",
|
|
" time_total_s: 0.03193807601928711\n",
|
|
" timestamp: 1658501584\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: f8669_00002\n",
|
|
" warmup_time: 0.003523111343383789\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_f8669_00003:\n",
|
|
" date: 2022-07-22_15-53-04\n",
|
|
" done: true\n",
|
|
" experiment_id: a39ea777ce2d4ebca51b3d7a4179dae5\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" mean_accuracy: 0.9230769230769231\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 48859\n",
|
|
" time_since_restore: 0.028388023376464844\n",
|
|
" time_this_iter_s: 0.028388023376464844\n",
|
|
" time_total_s: 0.028388023376464844\n",
|
|
" timestamp: 1658501584\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: f8669_00003\n",
|
|
" warmup_time: 0.0035560131072998047\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_f8669_00006:\n",
|
|
" date: 2022-07-22_15-53-04\n",
|
|
" done: true\n",
|
|
" experiment_id: f97c6b9674854f8d89ec26ba58cc1618\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" mean_accuracy: 0.9440559440559441\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 48862\n",
|
|
" time_since_restore: 0.026737213134765625\n",
|
|
" time_this_iter_s: 0.026737213134765625\n",
|
|
" time_total_s: 0.026737213134765625\n",
|
|
" timestamp: 1658501584\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: f8669_00006\n",
|
|
" warmup_time: 0.003425121307373047\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_f8669_00007:\n",
|
|
" date: 2022-07-22_15-53-04\n",
|
|
" done: true\n",
|
|
" experiment_id: ff172037065a4d55998ed72f51bdc5df\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" mean_accuracy: 0.965034965034965\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 48863\n",
|
|
" time_since_restore: 0.027612924575805664\n",
|
|
" time_this_iter_s: 0.027612924575805664\n",
|
|
" time_total_s: 0.027612924575805664\n",
|
|
" timestamp: 1658501584\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: f8669_00007\n",
|
|
" warmup_time: 0.0031311511993408203\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2022-07-22 15:53:04,846\tINFO tune.py:738 -- Total run time: 8.99 seconds (7.74 seconds for the tuning loop).\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import sklearn.datasets\n",
|
|
"import sklearn.metrics\n",
|
|
"\n",
|
|
"from ray import air, tune\n",
|
|
"from ray.air import session\n",
|
|
"\n",
|
|
"\n",
|
|
"def train_breast_cancer(config):\n",
|
|
" # Load dataset\n",
|
|
" data, labels = sklearn.datasets.load_breast_cancer(return_X_y=True)\n",
|
|
" # Split into train and test set\n",
|
|
" train_x, test_x, train_y, test_y = train_test_split(\n",
|
|
" data, labels, test_size=0.25)\n",
|
|
" # Build input matrices for XGBoost\n",
|
|
" train_set = xgb.DMatrix(train_x, label=train_y)\n",
|
|
" test_set = xgb.DMatrix(test_x, label=test_y)\n",
|
|
" # Train the classifier\n",
|
|
" results = {}\n",
|
|
" xgb.train(\n",
|
|
" config,\n",
|
|
" train_set,\n",
|
|
" evals=[(test_set, \"eval\")],\n",
|
|
" evals_result=results,\n",
|
|
" verbose_eval=False)\n",
|
|
" # Return prediction accuracy\n",
|
|
" accuracy = 1. - results[\"eval\"][\"error\"][-1]\n",
|
|
" session.report({\"mean_accuracy\": accuracy, \"done\": True})\n",
|
|
"\n",
|
|
"\n",
|
|
"if __name__ == \"__main__\":\n",
|
|
" config = {\n",
|
|
" \"objective\": \"binary:logistic\",\n",
|
|
" \"eval_metric\": [\"logloss\", \"error\"],\n",
|
|
" \"max_depth\": tune.randint(1, 9),\n",
|
|
" \"min_child_weight\": tune.choice([1, 2, 3]),\n",
|
|
" \"subsample\": tune.uniform(0.5, 1.0),\n",
|
|
" \"eta\": tune.loguniform(1e-4, 1e-1)\n",
|
|
" }\n",
|
|
" tuner = tune.Tuner(\n",
|
|
" train_breast_cancer,\n",
|
|
" tune_config=tune.TuneConfig(\n",
|
|
" num_samples=10,\n",
|
|
" ),\n",
|
|
" param_space=config,\n",
|
|
" )\n",
|
|
" results = tuner.fit()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "4999e858",
|
|
"metadata": {
|
|
"pycharm": {
|
|
"name": "#%% md\n"
|
|
}
|
|
},
|
|
"source": [
|
|
"As you can see, the changes in the actual training function are minimal. Instead of\n",
|
|
"returning the accuracy value, we report it back to Tune using `session.report()`.\n",
|
|
"Our `config` dictionary only changed slightly. Instead of passing hard-coded\n",
|
|
"parameters, we tell Tune to choose values from a range of valid options. There are\n",
|
|
"a number of options we have here, all of which are explained in\n",
|
|
"{ref}`the Tune docs <tune-sample-docs>`.\n",
|
|
"\n",
|
|
"For a brief explanation, this is what they do:\n",
|
|
"\n",
|
|
"- `tune.randint(min, max)` chooses a random integer value between *min* and *max*.\n",
|
|
" Note that *max* is exclusive, so it will not be sampled.\n",
|
|
"- `tune.choice([a, b, c])` chooses one of the items of the list at random. Each item\n",
|
|
" has the same chance to be sampled.\n",
|
|
"- `tune.uniform(min, max)` samples a floating point number between *min* and *max*.\n",
|
|
" Note that *max* is exclusive here, too.\n",
|
|
"- `tune.loguniform(min, max, base=10)` samples a floating point number between *min* and *max*,\n",
|
|
" but applies a logarithmic transformation to these boundaries first. Thus, this makes\n",
|
|
" it easy to sample values from different orders of magnitude.\n",
|
|
"\n",
|
|
"The `num_samples=10` option we pass to the `TuneConfig()` means that we sample 10 different\n",
|
|
"hyperparameter configurations from this search space.\n",
|
|
"\n",
|
|
"The output of our training run coud look like this:\n",
|
|
"\n",
|
|
"```{code-block} bash\n",
|
|
":emphasize-lines: 14\n",
|
|
"\n",
|
|
" Number of trials: 10/10 (10 TERMINATED)\n",
|
|
" +---------------------------------+------------+-------+-------------+-------------+--------------------+-------------+----------+--------+------------------+\n",
|
|
" | Trial name | status | loc | eta | max_depth | min_child_weight | subsample | acc | iter | total time (s) |\n",
|
|
" |---------------------------------+------------+-------+-------------+-------------+--------------------+-------------+----------+--------+------------------|\n",
|
|
" | train_breast_cancer_b63aa_00000 | TERMINATED | | 0.000117625 | 2 | 2 | 0.616347 | 0.916084 | 1 | 0.0306492 |\n",
|
|
" | train_breast_cancer_b63aa_00001 | TERMINATED | | 0.0382954 | 8 | 2 | 0.581549 | 0.937063 | 1 | 0.0357082 |\n",
|
|
" | train_breast_cancer_b63aa_00002 | TERMINATED | | 0.000217926 | 1 | 3 | 0.528428 | 0.874126 | 1 | 0.0264609 |\n",
|
|
" | train_breast_cancer_b63aa_00003 | TERMINATED | | 0.000120929 | 8 | 1 | 0.634508 | 0.958042 | 1 | 0.036406 |\n",
|
|
" | train_breast_cancer_b63aa_00004 | TERMINATED | | 0.00839715 | 5 | 1 | 0.730624 | 0.958042 | 1 | 0.0389378 |\n",
|
|
" | train_breast_cancer_b63aa_00005 | TERMINATED | | 0.000732948 | 8 | 2 | 0.915863 | 0.958042 | 1 | 0.0382841 |\n",
|
|
" | train_breast_cancer_b63aa_00006 | TERMINATED | | 0.000856226 | 4 | 1 | 0.645209 | 0.916084 | 1 | 0.0357089 |\n",
|
|
" | train_breast_cancer_b63aa_00007 | TERMINATED | | 0.00769908 | 7 | 1 | 0.729443 | 0.909091 | 1 | 0.0390737 |\n",
|
|
" | train_breast_cancer_b63aa_00008 | TERMINATED | | 0.00186339 | 5 | 3 | 0.595744 | 0.944056 | 1 | 0.0343912 |\n",
|
|
" | train_breast_cancer_b63aa_00009 | TERMINATED | | 0.000950272 | 3 | 2 | 0.835504 | 0.965035 | 1 | 0.0348201 |\n",
|
|
" +---------------------------------+------------+-------+-------------+-------------+--------------------+-------------+----------+--------+------------------+\n",
|
|
"```\n",
|
|
"\n",
|
|
"The best configuration we found used `eta=0.000950272`, `max_depth=3`,\n",
|
|
"`min_child_weight=2`, `subsample=0.835504` and reached an accuracy of\n",
|
|
"`0.965035`.\n",
|
|
"\n",
|
|
"## Early stopping\n",
|
|
"\n",
|
|
"Currently, Tune samples 10 different hyperparameter configurations and trains a full\n",
|
|
"XGBoost on all of them. In our small example, training is very fast. However,\n",
|
|
"if training takes longer, a significant amount of computer resources is spent on trials\n",
|
|
"that will eventually show a bad performance, e.g. a low accuracy. It would be good\n",
|
|
"if we could identify these trials early and stop them, so we don't waste any resources.\n",
|
|
"\n",
|
|
"This is where Tune's *Schedulers* shine. A Tune `TrialScheduler` is responsible\n",
|
|
"for starting and stopping trials. Tune implements a number of different schedulers, each\n",
|
|
"described {ref}`in the Tune documentation <tune-schedulers>`.\n",
|
|
"For our example, we will use the `AsyncHyperBandScheduler` or `ASHAScheduler`.\n",
|
|
"\n",
|
|
"The basic idea of this scheduler: We sample a number of hyperparameter configurations.\n",
|
|
"Each of these configurations is trained for a specific number of iterations.\n",
|
|
"After these iterations, only the best performing hyperparameters are retained. These\n",
|
|
"are selected according to some loss metric, usually an evaluation loss. This cycle is\n",
|
|
"repeated until we end up with the best configuration.\n",
|
|
"\n",
|
|
"The `ASHAScheduler` needs to know three things:\n",
|
|
"\n",
|
|
"1. Which metric should be used to identify badly performing trials?\n",
|
|
"2. Should this metric be maximized or minimized?\n",
|
|
"3. How many iterations does each trial train for?\n",
|
|
"\n",
|
|
"There are more parameters, which are explained in the\n",
|
|
"{ref}`documentation <tune-scheduler-hyperband>`.\n",
|
|
"\n",
|
|
"Lastly, we have to report the loss metric to Tune. We do this with a `Callback` that\n",
|
|
"XGBoost accepts and calls after each evaluation round. Ray Tune comes\n",
|
|
"with {ref}`two XGBoost callbacks <tune-integration-xgboost>`\n",
|
|
"we can use for this. The `TuneReportCallback` just reports the evaluation\n",
|
|
"metrics back to Tune. The `TuneReportCheckpointCallback` also saves\n",
|
|
"checkpoints after each evaluation round. We will just use the latter in this\n",
|
|
"example so that we can retrieve the saved model later.\n",
|
|
"\n",
|
|
"These parameters from the `eval_metrics` configuration setting are then automatically\n",
|
|
"reported to Tune via the callback. Here, the raw error will be reported, not the accuracy.\n",
|
|
"To display the best reached accuracy, we will inverse it later.\n",
|
|
"\n",
|
|
"We will also load the best checkpointed model so that we can use it for predictions.\n",
|
|
"The best model is selected with respect to the `metric` and `mode` parameters we\n",
|
|
"pass to the `TunerConfig()`."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "d08b5b0a",
|
|
"metadata": {
|
|
"pycharm": {
|
|
"name": "#%%\n"
|
|
},
|
|
"vscode": {
|
|
"languageId": "python"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"== Status ==<br>Current time: 2022-07-22 16:56:01 (running for 00:00:10.38)<br>Memory usage on this node: 10.3/16.0 GiB<br>Using AsyncHyperBand: num_stopped=10\n",
|
|
"Bracket: Iter 8.000: -0.5107275277792991 | Iter 4.000: -0.5876629346317344 | Iter 2.000: -0.6544494184997531 | Iter 1.000: -0.6859214191253369<br>Resources requested: 0/16 CPUs, 0/0 GPUs, 0.0/4.57 GiB heap, 0.0/2.0 GiB objects<br>Current best trial: c28a3_00003 with eval-logloss=0.38665050018083796 and parameters={'objective': 'binary:logistic', 'eval_metric': ['logloss', 'error'], 'max_depth': 2, 'min_child_weight': 3, 'subsample': 0.782626252548841, 'eta': 0.06385952388342125}<br>Result logdir: /Users/kai/ray_results/train_breast_cancer_2022-07-22_16-55-50<br>Number of trials: 10/10 (10 TERMINATED)<br><table>\n",
|
|
"<thead>\n",
|
|
"<tr><th>Trial name </th><th>status </th><th>loc </th><th style=\"text-align: right;\"> eta</th><th style=\"text-align: right;\"> max_depth</th><th style=\"text-align: right;\"> min_child_weight</th><th style=\"text-align: right;\"> subsample</th><th style=\"text-align: right;\"> iter</th><th style=\"text-align: right;\"> total time (s)</th><th style=\"text-align: right;\"> eval-logloss</th><th style=\"text-align: right;\"> eval-error</th></tr>\n",
|
|
"</thead>\n",
|
|
"<tbody>\n",
|
|
"<tr><td>train_breast_cancer_c28a3_00000</td><td>TERMINATED</td><td>127.0.0.1:54416</td><td style=\"text-align: right;\">0.0186954 </td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.516916</td><td style=\"text-align: right;\"> 10</td><td style=\"text-align: right;\"> 0.22218 </td><td style=\"text-align: right;\"> 0.571496</td><td style=\"text-align: right;\"> 0.0629371</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_c28a3_00001</td><td>TERMINATED</td><td>127.0.0.1:54440</td><td style=\"text-align: right;\">0.0304404 </td><td style=\"text-align: right;\"> 8</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.745969</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.135674 </td><td style=\"text-align: right;\"> 0.650353</td><td style=\"text-align: right;\"> 0.0629371</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_c28a3_00002</td><td>TERMINATED</td><td>127.0.0.1:54441</td><td style=\"text-align: right;\">0.0217157 </td><td style=\"text-align: right;\"> 8</td><td style=\"text-align: right;\"> 3</td><td style=\"text-align: right;\"> 0.764138</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.173076 </td><td style=\"text-align: right;\"> 0.658545</td><td style=\"text-align: right;\"> 0.041958 </td></tr>\n",
|
|
"<tr><td>train_breast_cancer_c28a3_00003</td><td>TERMINATED</td><td>127.0.0.1:54442</td><td style=\"text-align: right;\">0.0638595 </td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 3</td><td style=\"text-align: right;\"> 0.782626</td><td style=\"text-align: right;\"> 10</td><td style=\"text-align: right;\"> 0.281865 </td><td style=\"text-align: right;\"> 0.386651</td><td style=\"text-align: right;\"> 0.041958 </td></tr>\n",
|
|
"<tr><td>train_breast_cancer_c28a3_00004</td><td>TERMINATED</td><td>127.0.0.1:54443</td><td style=\"text-align: right;\">0.00442794 </td><td style=\"text-align: right;\"> 7</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.792359</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0270212</td><td style=\"text-align: right;\"> 0.689577</td><td style=\"text-align: right;\"> 0.0699301</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_c28a3_00005</td><td>TERMINATED</td><td>127.0.0.1:54444</td><td style=\"text-align: right;\">0.00222624 </td><td style=\"text-align: right;\"> 3</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.536331</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0238512</td><td style=\"text-align: right;\"> 0.691446</td><td style=\"text-align: right;\"> 0.0839161</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_c28a3_00006</td><td>TERMINATED</td><td>127.0.0.1:54445</td><td style=\"text-align: right;\">0.000825129</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.82472 </td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.015312 </td><td style=\"text-align: right;\"> 0.692624</td><td style=\"text-align: right;\"> 0.118881 </td></tr>\n",
|
|
"<tr><td>train_breast_cancer_c28a3_00007</td><td>TERMINATED</td><td>127.0.0.1:54446</td><td style=\"text-align: right;\">0.000770826</td><td style=\"text-align: right;\"> 7</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.947268</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0175898</td><td style=\"text-align: right;\"> 0.692598</td><td style=\"text-align: right;\"> 0.132867 </td></tr>\n",
|
|
"<tr><td>train_breast_cancer_c28a3_00008</td><td>TERMINATED</td><td>127.0.0.1:54447</td><td style=\"text-align: right;\">0.000429759</td><td style=\"text-align: right;\"> 7</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.88524 </td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0193739</td><td style=\"text-align: right;\"> 0.692785</td><td style=\"text-align: right;\"> 0.0559441</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_c28a3_00009</td><td>TERMINATED</td><td>127.0.0.1:54448</td><td style=\"text-align: right;\">0.0149863 </td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.722738</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.0165932</td><td style=\"text-align: right;\"> 0.682266</td><td style=\"text-align: right;\"> 0.111888 </td></tr>\n",
|
|
"</tbody>\n",
|
|
"</table><br><br>"
|
|
],
|
|
"text/plain": [
|
|
"<IPython.core.display.HTML object>"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Result for train_breast_cancer_c28a3_00000:\n",
|
|
" date: 2022-07-22_16-55-55\n",
|
|
" done: false\n",
|
|
" eval-error: 0.08391608391608392\n",
|
|
" eval-logloss: 0.6790360066440556\n",
|
|
" experiment_id: 2a3189442db341519836a07fb2d65dd9\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54416\n",
|
|
" time_since_restore: 0.01624011993408203\n",
|
|
" time_this_iter_s: 0.01624011993408203\n",
|
|
" time_total_s: 0.01624011993408203\n",
|
|
" timestamp: 1658505355\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: c28a3_00000\n",
|
|
" warmup_time: 0.0035409927368164062\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00000:\n",
|
|
" date: 2022-07-22_16-55-56\n",
|
|
" done: true\n",
|
|
" eval-error: 0.06293706293706294\n",
|
|
" eval-logloss: 0.5714958122560194\n",
|
|
" experiment_id: 2a3189442db341519836a07fb2d65dd9\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 10\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54416\n",
|
|
" time_since_restore: 0.22218012809753418\n",
|
|
" time_this_iter_s: 0.007044076919555664\n",
|
|
" time_total_s: 0.22218012809753418\n",
|
|
" timestamp: 1658505356\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 10\n",
|
|
" trial_id: c28a3_00000\n",
|
|
" warmup_time: 0.0035409927368164062\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00003:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: false\n",
|
|
" eval-error: 0.08391608391608392\n",
|
|
" eval-logloss: 0.6472820101918041\n",
|
|
" experiment_id: 7ff6133237404b4ea4755b9f8cd114f2\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54442\n",
|
|
" time_since_restore: 0.023206233978271484\n",
|
|
" time_this_iter_s: 0.023206233978271484\n",
|
|
" time_total_s: 0.023206233978271484\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: c28a3_00003\n",
|
|
" warmup_time: 0.006722211837768555\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00005:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: true\n",
|
|
" eval-error: 0.08391608391608392\n",
|
|
" eval-logloss: 0.6914464114429234\n",
|
|
" experiment_id: 344762ab6d574b63a9374e19526d0510\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54444\n",
|
|
" time_since_restore: 0.02385115623474121\n",
|
|
" time_this_iter_s: 0.02385115623474121\n",
|
|
" time_total_s: 0.02385115623474121\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: c28a3_00005\n",
|
|
" warmup_time: 0.008936882019042969\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00009:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: true\n",
|
|
" eval-error: 0.11188811188811189\n",
|
|
" eval-logloss: 0.6822656309688008\n",
|
|
" experiment_id: 133901655fa64bf79f2dcc4e8e5e41b1\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54448\n",
|
|
" time_since_restore: 0.016593217849731445\n",
|
|
" time_this_iter_s: 0.016593217849731445\n",
|
|
" time_total_s: 0.016593217849731445\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: c28a3_00009\n",
|
|
" warmup_time: 0.004940032958984375\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00007:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: true\n",
|
|
" eval-error: 0.13286713286713286\n",
|
|
" eval-logloss: 0.6925980357023386\n",
|
|
" experiment_id: b4331027cbaf442ab905b2e51797dbbd\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54446\n",
|
|
" time_since_restore: 0.017589807510375977\n",
|
|
" time_this_iter_s: 0.017589807510375977\n",
|
|
" time_total_s: 0.017589807510375977\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: c28a3_00007\n",
|
|
" warmup_time: 0.003782033920288086\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00006:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: true\n",
|
|
" eval-error: 0.11888111888111888\n",
|
|
" eval-logloss: 0.6926244418104212\n",
|
|
" experiment_id: d3906de5943a4e05a4cc782382f67d24\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54445\n",
|
|
" time_since_restore: 0.015311956405639648\n",
|
|
" time_this_iter_s: 0.015311956405639648\n",
|
|
" time_total_s: 0.015311956405639648\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: c28a3_00006\n",
|
|
" warmup_time: 0.005506038665771484\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00002:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: false\n",
|
|
" eval-error: 0.04895104895104895\n",
|
|
" eval-logloss: 0.6752762102580571\n",
|
|
" experiment_id: a3645fc2d43145d88a1f5b7cc94df703\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54441\n",
|
|
" time_since_restore: 0.027367830276489258\n",
|
|
" time_this_iter_s: 0.027367830276489258\n",
|
|
" time_total_s: 0.027367830276489258\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: c28a3_00002\n",
|
|
" warmup_time: 0.0062830448150634766\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00001:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: false\n",
|
|
" eval-error: 0.07692307692307693\n",
|
|
" eval-logloss: 0.6698804135089154\n",
|
|
" experiment_id: 85766fe4d9fa482a91e396a8fd509a19\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54440\n",
|
|
" time_since_restore: 0.017169952392578125\n",
|
|
" time_this_iter_s: 0.017169952392578125\n",
|
|
" time_total_s: 0.017169952392578125\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: c28a3_00001\n",
|
|
" warmup_time: 0.006204843521118164\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00008:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: true\n",
|
|
" eval-error: 0.05594405594405594\n",
|
|
" eval-logloss: 0.692784742458717\n",
|
|
" experiment_id: 2c7d8bc38ad04536b1dec76819a2b3bf\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54447\n",
|
|
" time_since_restore: 0.01937389373779297\n",
|
|
" time_this_iter_s: 0.01937389373779297\n",
|
|
" time_total_s: 0.01937389373779297\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: c28a3_00008\n",
|
|
" warmup_time: 0.004342079162597656\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00001:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: true\n",
|
|
" eval-error: 0.06293706293706294\n",
|
|
" eval-logloss: 0.6503534216980834\n",
|
|
" experiment_id: 85766fe4d9fa482a91e396a8fd509a19\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 2\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54440\n",
|
|
" time_since_restore: 0.13567376136779785\n",
|
|
" time_this_iter_s: 0.11850380897521973\n",
|
|
" time_total_s: 0.13567376136779785\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 2\n",
|
|
" trial_id: c28a3_00001\n",
|
|
" warmup_time: 0.006204843521118164\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00004:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: true\n",
|
|
" eval-error: 0.06993006993006994\n",
|
|
" eval-logloss: 0.689577207281873\n",
|
|
" experiment_id: ef4fdc645c444112985b4957ab8a84e9\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 1\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54443\n",
|
|
" time_since_restore: 0.027021169662475586\n",
|
|
" time_this_iter_s: 0.027021169662475586\n",
|
|
" time_total_s: 0.027021169662475586\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 1\n",
|
|
" trial_id: c28a3_00004\n",
|
|
" warmup_time: 0.0063669681549072266\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00002:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: true\n",
|
|
" eval-error: 0.04195804195804196\n",
|
|
" eval-logloss: 0.658545415301423\n",
|
|
" experiment_id: a3645fc2d43145d88a1f5b7cc94df703\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 2\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54441\n",
|
|
" time_since_restore: 0.17307591438293457\n",
|
|
" time_this_iter_s: 0.1457080841064453\n",
|
|
" time_total_s: 0.17307591438293457\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 2\n",
|
|
" trial_id: c28a3_00002\n",
|
|
" warmup_time: 0.0062830448150634766\n",
|
|
" \n",
|
|
"Result for train_breast_cancer_c28a3_00003:\n",
|
|
" date: 2022-07-22_16-56-01\n",
|
|
" done: true\n",
|
|
" eval-error: 0.04195804195804196\n",
|
|
" eval-logloss: 0.38665050018083796\n",
|
|
" experiment_id: 7ff6133237404b4ea4755b9f8cd114f2\n",
|
|
" hostname: Kais-MacBook-Pro.local\n",
|
|
" iterations_since_restore: 10\n",
|
|
" node_ip: 127.0.0.1\n",
|
|
" pid: 54442\n",
|
|
" time_since_restore: 0.28186488151550293\n",
|
|
" time_this_iter_s: 0.03063178062438965\n",
|
|
" time_total_s: 0.28186488151550293\n",
|
|
" timestamp: 1658505361\n",
|
|
" timesteps_since_restore: 0\n",
|
|
" training_iteration: 10\n",
|
|
" trial_id: c28a3_00003\n",
|
|
" warmup_time: 0.006722211837768555\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2022-07-22 16:56:01,498\tINFO tune.py:738 -- Total run time: 10.53 seconds (10.37 seconds for the tuning loop).\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Best model parameters: {'objective': 'binary:logistic', 'eval_metric': ['logloss', 'error'], 'max_depth': 2, 'min_child_weight': 3, 'subsample': 0.782626252548841, 'eta': 0.06385952388342125}\n",
|
|
"Best model total accuracy: 0.9580\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import sklearn.datasets\n",
|
|
"import sklearn.metrics\n",
|
|
"import os\n",
|
|
"from ray.tune.schedulers import ASHAScheduler\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"import xgboost as xgb\n",
|
|
"\n",
|
|
"from ray import air, tune\n",
|
|
"from ray.air import session\n",
|
|
"from ray.tune.integration.xgboost import TuneReportCheckpointCallback\n",
|
|
"\n",
|
|
"\n",
|
|
"def train_breast_cancer(config: dict):\n",
|
|
" # This is a simple training function to be passed into Tune\n",
|
|
" # Load dataset\n",
|
|
" data, labels = sklearn.datasets.load_breast_cancer(return_X_y=True)\n",
|
|
" # Split into train and test set\n",
|
|
" train_x, test_x, train_y, test_y = train_test_split(data, labels, test_size=0.25)\n",
|
|
" # Build input matrices for XGBoost\n",
|
|
" train_set = xgb.DMatrix(train_x, label=train_y)\n",
|
|
" test_set = xgb.DMatrix(test_x, label=test_y)\n",
|
|
" # Train the classifier, using the Tune callback\n",
|
|
" xgb.train(\n",
|
|
" config,\n",
|
|
" train_set,\n",
|
|
" evals=[(test_set, \"eval\")],\n",
|
|
" verbose_eval=False,\n",
|
|
" callbacks=[TuneReportCheckpointCallback(filename=\"model.xgb\")],\n",
|
|
" )\n",
|
|
"\n",
|
|
"\n",
|
|
"def get_best_model_checkpoint(results):\n",
|
|
" best_bst = xgb.Booster()\n",
|
|
" best_result = results.get_best_result()\n",
|
|
" \n",
|
|
" with best_result.checkpoint.as_directory() as best_checkpoint_dir:\n",
|
|
" best_bst.load_model(os.path.join(best_checkpoint_dir, \"model.xgb\"))\n",
|
|
" accuracy = 1.0 - best_result.metrics[\"eval-error\"]\n",
|
|
" print(f\"Best model parameters: {best_result.config}\")\n",
|
|
" print(f\"Best model total accuracy: {accuracy:.4f}\")\n",
|
|
" return best_bst\n",
|
|
"\n",
|
|
"\n",
|
|
"def tune_xgboost():\n",
|
|
" search_space = {\n",
|
|
" # You can mix constants with search space objects.\n",
|
|
" \"objective\": \"binary:logistic\",\n",
|
|
" \"eval_metric\": [\"logloss\", \"error\"],\n",
|
|
" \"max_depth\": tune.randint(1, 9),\n",
|
|
" \"min_child_weight\": tune.choice([1, 2, 3]),\n",
|
|
" \"subsample\": tune.uniform(0.5, 1.0),\n",
|
|
" \"eta\": tune.loguniform(1e-4, 1e-1),\n",
|
|
" }\n",
|
|
" # This will enable aggressive early stopping of bad trials.\n",
|
|
" scheduler = ASHAScheduler(\n",
|
|
" max_t=10, grace_period=1, reduction_factor=2 # 10 training iterations\n",
|
|
" )\n",
|
|
" \n",
|
|
" tuner = tune.Tuner(\n",
|
|
" train_breast_cancer,\n",
|
|
" tune_config=tune.TuneConfig(\n",
|
|
" metric=\"eval-logloss\",\n",
|
|
" mode=\"min\",\n",
|
|
" scheduler=scheduler,\n",
|
|
" num_samples=10,\n",
|
|
" ),\n",
|
|
" param_space=search_space,\n",
|
|
" )\n",
|
|
" results = tuner.fit()\n",
|
|
"\n",
|
|
" return results\n",
|
|
"\n",
|
|
"\n",
|
|
"if __name__ == \"__main__\":\n",
|
|
" import argparse\n",
|
|
"\n",
|
|
" parser = argparse.ArgumentParser()\n",
|
|
" parser.add_argument(\n",
|
|
" \"--server-address\",\n",
|
|
" type=str,\n",
|
|
" default=None,\n",
|
|
" required=False,\n",
|
|
" help=\"The address of server to connect to if using Ray Client.\",\n",
|
|
" )\n",
|
|
" args, _ = parser.parse_known_args()\n",
|
|
"\n",
|
|
" if args.server_address:\n",
|
|
" import ray\n",
|
|
"\n",
|
|
" ray.init(f\"ray://{args.server_address}\")\n",
|
|
"\n",
|
|
" results = tune_xgboost()\n",
|
|
"\n",
|
|
" # Load the best model checkpoint.\n",
|
|
" best_bst = get_best_model_checkpoint(results)\n",
|
|
"\n",
|
|
" # You could now do further predictions with\n",
|
|
" # best_bst.predict(...)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "20732fe4",
|
|
"metadata": {
|
|
"pycharm": {
|
|
"name": "#%% md\n"
|
|
}
|
|
},
|
|
"source": [
|
|
"The output of our run could look like this:\n",
|
|
"\n",
|
|
"```{code-block} bash\n",
|
|
":emphasize-lines: 7\n",
|
|
"\n",
|
|
" Number of trials: 10/10 (10 TERMINATED)\n",
|
|
" +---------------------------------+------------+-------+-------------+-------------+--------------------+-------------+--------+------------------+----------------+--------------+\n",
|
|
" | Trial name | status | loc | eta | max_depth | min_child_weight | subsample | iter | total time (s) | eval-logloss | eval-error |\n",
|
|
" |---------------------------------+------------+-------+-------------+-------------+--------------------+-------------+--------+------------------+----------------+--------------|\n",
|
|
" | train_breast_cancer_ba275_00000 | TERMINATED | | 0.00205087 | 2 | 1 | 0.898391 | 10 | 0.380619 | 0.678039 | 0.090909 |\n",
|
|
" | train_breast_cancer_ba275_00001 | TERMINATED | | 0.000183834 | 4 | 3 | 0.924939 | 1 | 0.0228798 | 0.693009 | 0.111888 |\n",
|
|
" | train_breast_cancer_ba275_00002 | TERMINATED | | 0.0242721 | 7 | 2 | 0.501551 | 10 | 0.376154 | 0.54472 | 0.06993 |\n",
|
|
" | train_breast_cancer_ba275_00003 | TERMINATED | | 0.000449692 | 5 | 3 | 0.890212 | 1 | 0.0234981 | 0.692811 | 0.090909 |\n",
|
|
" | train_breast_cancer_ba275_00004 | TERMINATED | | 0.000376393 | 7 | 2 | 0.883609 | 1 | 0.0231569 | 0.692847 | 0.062937 |\n",
|
|
" | train_breast_cancer_ba275_00005 | TERMINATED | | 0.00231942 | 3 | 3 | 0.877464 | 2 | 0.104867 | 0.689541 | 0.083916 |\n",
|
|
" | train_breast_cancer_ba275_00006 | TERMINATED | | 0.000542326 | 1 | 2 | 0.578584 | 1 | 0.0213971 | 0.692765 | 0.083916 |\n",
|
|
" | train_breast_cancer_ba275_00007 | TERMINATED | | 0.0016801 | 1 | 2 | 0.975302 | 1 | 0.02226 | 0.691999 | 0.083916 |\n",
|
|
" | train_breast_cancer_ba275_00008 | TERMINATED | | 0.000595756 | 8 | 3 | 0.58429 | 1 | 0.0221152 | 0.692657 | 0.06993 |\n",
|
|
" | train_breast_cancer_ba275_00009 | TERMINATED | | 0.000357845 | 8 | 1 | 0.637776 | 1 | 0.022635 | 0.692859 | 0.090909 |\n",
|
|
" +---------------------------------+------------+-------+-------------+-------------+--------------------+-------------+--------+------------------+----------------+--------------+\n",
|
|
"\n",
|
|
"\n",
|
|
" Best model parameters: {'objective': 'binary:logistic', 'eval_metric': ['logloss', 'error'], 'max_depth': 7, 'min_child_weight': 2, 'subsample': 0.5015513240240503, 'eta': 0.024272050872920895}\n",
|
|
" Best model total accuracy: 0.9301\n",
|
|
"```\n",
|
|
"\n",
|
|
"As you can see, most trials have been stopped only after a few iterations. Only the\n",
|
|
"two most promising trials were run for the full 10 iterations.\n",
|
|
"\n",
|
|
"You can also ensure that all available resources are being used as the scheduler\n",
|
|
"terminates trials, freeing them up. This can be done through the\n",
|
|
"`ResourceChangingScheduler`. An example of this can be found here:\n",
|
|
"{doc}`/tune/examples/includes/xgboost_dynamic_resources_example`.\n",
|
|
"\n",
|
|
"## Using fractional GPUs\n",
|
|
"\n",
|
|
"You can often accelerate your training by using GPUs in addition to CPUs. However,\n",
|
|
"you usually don't have as many GPUs as you have trials to run. For instance, if you\n",
|
|
"run 10 Tune trials in parallel, you usually don't have access to 10 separate GPUs.\n",
|
|
"\n",
|
|
"Tune supports *fractional GPUs*. This means that each task is assigned a fraction\n",
|
|
"of the GPU memory for training. For 10 tasks, this could look like this:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "7d1b20a3",
|
|
"metadata": {
|
|
"pycharm": {
|
|
"name": "#%%\n"
|
|
},
|
|
"vscode": {
|
|
"languageId": "python"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"== Status ==<br>Current time: 2022-07-22 16:17:16 (running for 00:00:15.23)<br>Memory usage on this node: 10.6/16.0 GiB<br>Using FIFO scheduling algorithm.<br>Resources requested: 0/16 CPUs, 0/0 GPUs, 0.0/4.57 GiB heap, 0.0/2.0 GiB objects<br>Result logdir: /Users/kai/ray_results/train_breast_cancer_2022-07-22_16-17-00<br>Number of trials: 10/10 (10 PENDING)<br><table>\n",
|
|
"<thead>\n",
|
|
"<tr><th>Trial name </th><th>status </th><th>loc </th><th style=\"text-align: right;\"> eta</th><th style=\"text-align: right;\"> max_depth</th><th style=\"text-align: right;\"> min_child_weight</th><th style=\"text-align: right;\"> subsample</th></tr>\n",
|
|
"</thead>\n",
|
|
"<tbody>\n",
|
|
"<tr><td>train_breast_cancer_55afc_00000</td><td>PENDING </td><td> </td><td style=\"text-align: right;\">0.0576229 </td><td style=\"text-align: right;\"> 6</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.877552</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_55afc_00001</td><td>PENDING </td><td> </td><td style=\"text-align: right;\">0.0489573 </td><td style=\"text-align: right;\"> 4</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.53162 </td></tr>\n",
|
|
"<tr><td>train_breast_cancer_55afc_00002</td><td>PENDING </td><td> </td><td style=\"text-align: right;\">0.000191779</td><td style=\"text-align: right;\"> 5</td><td style=\"text-align: right;\"> 3</td><td style=\"text-align: right;\"> 0.556715</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_55afc_00003</td><td>PENDING </td><td> </td><td style=\"text-align: right;\">0.000372967</td><td style=\"text-align: right;\"> 8</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.972162</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_55afc_00004</td><td>PENDING </td><td> </td><td style=\"text-align: right;\">0.00164281 </td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.941938</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_55afc_00005</td><td>PENDING </td><td> </td><td style=\"text-align: right;\">0.00632937 </td><td style=\"text-align: right;\"> 4</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.562461</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_55afc_00006</td><td>PENDING </td><td> </td><td style=\"text-align: right;\">0.00574961 </td><td style=\"text-align: right;\"> 8</td><td style=\"text-align: right;\"> 3</td><td style=\"text-align: right;\"> 0.70719 </td></tr>\n",
|
|
"<tr><td>train_breast_cancer_55afc_00007</td><td>PENDING </td><td> </td><td style=\"text-align: right;\">0.0321386 </td><td style=\"text-align: right;\"> 5</td><td style=\"text-align: right;\"> 2</td><td style=\"text-align: right;\"> 0.581837</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_55afc_00008</td><td>PENDING </td><td> </td><td style=\"text-align: right;\">0.0351682 </td><td style=\"text-align: right;\"> 4</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.554551</td></tr>\n",
|
|
"<tr><td>train_breast_cancer_55afc_00009</td><td>PENDING </td><td> </td><td style=\"text-align: right;\">0.000204664</td><td style=\"text-align: right;\"> 8</td><td style=\"text-align: right;\"> 1</td><td style=\"text-align: right;\"> 0.995994</td></tr>\n",
|
|
"</tbody>\n",
|
|
"</table><br><br>"
|
|
],
|
|
"text/plain": [
|
|
"<IPython.core.display.HTML object>"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\u001b[2m\u001b[1m\u001b[36m(scheduler +24m15s)\u001b[0m Tip: use `ray status` to view detailed cluster status. To disable these messages, set RAY_SCHEDULER_EVENTS=0.\n",
|
|
"\u001b[2m\u001b[1m\u001b[33m(scheduler +24m15s)\u001b[0m Error: No available node types can fulfill resource request {'CPU': 1.0, 'GPU': 0.1}. Add suitable node types to this cluster to resolve this issue.\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2022-07-22 16:17:19,124\tWARNING tune.py:666 -- Stop signal received (e.g. via SIGINT/Ctrl+C), ending Ray Tune run. This will try to checkpoint the experiment state one last time. Press CTRL+C (or send SIGINT/SIGKILL/SIGTERM) to skip. \n"
|
|
]
|
|
},
|
|
{
|
|
"ename": "KeyboardInterrupt",
|
|
"evalue": "",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
|
|
"\u001b[0;32m/var/folders/b2/0_91bd757rz02lrmr920v0gw0000gn/T/ipykernel_48774/3983013579.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0mparam_space\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mconfig\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m )\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtuner\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
|
"\u001b[0;32m~/coding/ray/python/ray/tune/tuner.py\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 190\u001b[0m \"\"\"\n\u001b[1;32m 191\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 192\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_is_ray_client\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 193\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 194\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_local_tuner\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m~/coding/ray/python/ray/tune/impl/tuner_internal.py\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 159\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mtrainable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 160\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 161\u001b[0;31m \u001b[0;32mdef\u001b[0m \u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mResultGrid\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 162\u001b[0m \u001b[0mtrainable\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_convert_trainable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_trainable\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_experiment_checkpoint_dir\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m~/coding/ray/python/ray/tune/impl/tuner_internal.py\u001b[0m in \u001b[0;36m_fit_internal\u001b[0;34m(self, trainable, param_space)\u001b[0m\n\u001b[1;32m 256\u001b[0m \u001b[0mscheduler\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_tune_config\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscheduler\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 257\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_run_config\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 258\u001b[0;31m \u001b[0mlog_to_file\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_run_config\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog_to_file\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 259\u001b[0m ),\n\u001b[1;32m 260\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_tuner_kwargs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m~/coding/ray/python/ray/tune/tune.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(run_or_experiment, name, metric, mode, stop, time_budget_s, config, resources_per_trial, num_samples, local_dir, search_alg, scheduler, keep_checkpoints_num, checkpoint_score_attr, checkpoint_freq, checkpoint_at_end, verbose, progress_reporter, log_to_file, trial_name_creator, trial_dirname_creator, sync_config, export_formats, max_failures, fail_fast, restore, server_port, resume, reuse_actors, trial_executor, raise_on_failed_trial, callbacks, max_concurrent_trials, _experiment_checkpoint_dir, _remote)\u001b[0m\n\u001b[1;32m 699\u001b[0m )\n\u001b[1;32m 700\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mrunner\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_finished\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mstate\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"signal\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 701\u001b[0;31m \u001b[0mrunner\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 702\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mhas_verbosity\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mVerbosity\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mV1_EXPERIMENT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0m_report_progress\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrunner\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprogress_reporter\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m~/coding/ray/python/ray/tune/execution/trial_runner.py\u001b[0m in \u001b[0;36mstep\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 811\u001b[0m \u001b[0mlogger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"Got new trial to run: {next_trial}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 812\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 813\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_wait_and_handle_event\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext_trial\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 814\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 815\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_stop_experiment_if_needed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m~/coding/ray/python/ray/tune/execution/trial_runner.py\u001b[0m in \u001b[0;36m_wait_and_handle_event\u001b[0;34m(self, next_trial)\u001b[0m\n\u001b[1;32m 755\u001b[0m \u001b[0;31m# Single wait of entire tune loop.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 756\u001b[0m event = self.trial_executor.get_next_executor_event(\n\u001b[0;32m--> 757\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_live_trials\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnext_trial\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 758\u001b[0m )\n\u001b[1;32m 759\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mevent\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtype\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0m_ExecutorEventType\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPG_READY\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m~/coding/ray/python/ray/tune/execution/ray_trial_executor.py\u001b[0m in \u001b[0;36mget_next_executor_event\u001b[0;34m(self, live_trials, next_trial_exists)\u001b[0m\n\u001b[1;32m 944\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 945\u001b[0m ready_futures, _ = ray.wait(\n\u001b[0;32m--> 946\u001b[0;31m \u001b[0mfutures_to_wait\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum_returns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_next_event_wait\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 947\u001b[0m )\n\u001b[1;32m 948\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m~/coding/ray/python/ray/_private/client_mode_hook.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 103\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__name__\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;34m\"init\"\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mis_client_mode_enabled_by_default\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 104\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__name__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 105\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 106\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32m~/coding/ray/python/ray/_private/worker.py\u001b[0m in \u001b[0;36mwait\u001b[0;34m(object_refs, num_returns, timeout, fetch_local)\u001b[0m\n\u001b[1;32m 2386\u001b[0m \u001b[0mtimeout_milliseconds\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2387\u001b[0m \u001b[0mworker\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcurrent_task_id\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2388\u001b[0;31m \u001b[0mfetch_local\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2389\u001b[0m )\n\u001b[1;32m 2390\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mready_ids\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mremaining_ids\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;32mpython/ray/_raylet.pyx\u001b[0m in \u001b[0;36mray._raylet.CoreWorker.wait\u001b[0;34m()\u001b[0m\n",
|
|
"\u001b[0;32mpython/ray/_raylet.pyx\u001b[0m in \u001b[0;36mray._raylet.check_status\u001b[0;34m()\u001b[0m\n",
|
|
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"config = {\n",
|
|
" \"objective\": \"binary:logistic\",\n",
|
|
" \"eval_metric\": [\"logloss\", \"error\"],\n",
|
|
" \"tree_method\": \"gpu_hist\",\n",
|
|
" \"max_depth\": tune.randint(1, 9),\n",
|
|
" \"min_child_weight\": tune.choice([1, 2, 3]),\n",
|
|
" \"subsample\": tune.uniform(0.5, 1.0),\n",
|
|
" \"eta\": tune.loguniform(1e-4, 1e-1)\n",
|
|
"}\n",
|
|
"\n",
|
|
"tuner = tune.Tuner(\n",
|
|
" tune.with_resources(\n",
|
|
" train_breast_cancer,\n",
|
|
" resources={\"cpu\": 1, \"gpu\": 0.1}\n",
|
|
" ),\n",
|
|
" tune_config=tune.TuneConfig(\n",
|
|
" num_samples=10,\n",
|
|
" ),\n",
|
|
" param_space=config,\n",
|
|
")\n",
|
|
"results = tuner.fit()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ee131861",
|
|
"metadata": {
|
|
"pycharm": {
|
|
"name": "#%% md\n"
|
|
}
|
|
},
|
|
"source": [
|
|
"Each task thus works with 10% of the available GPU memory. You also have to tell\n",
|
|
"XGBoost to use the `gpu_hist` tree method, so it knows it should use the GPU.\n",
|
|
"\n",
|
|
"## Conclusion\n",
|
|
"\n",
|
|
"You should now have a basic understanding on how to train XGBoost models and on how\n",
|
|
"to tune the hyperparameters to yield the best results. In our simple example,\n",
|
|
"Tuning the parameters didn't make a huge difference for the accuracy.\n",
|
|
"But in larger applications, intelligent hyperparameter tuning can make the\n",
|
|
"difference between a model that doesn't seem to learn at all, and a model\n",
|
|
"that outperforms all the other ones.\n",
|
|
"\n",
|
|
"## More XGBoost Examples\n",
|
|
"\n",
|
|
"- {doc}`/tune/examples/includes/xgboost_dynamic_resources_example`:\n",
|
|
" Trains a basic XGBoost model with Tune with the class-based API and a ResourceChangingScheduler, ensuring all resources are being used at all time.\n",
|
|
"\n",
|
|
"## Learn More\n",
|
|
"\n",
|
|
"- [XGBoost Hyperparameter Tuning - A Visual Guide](https://kevinvecmanis.io/machine%20learning/hyperparameter%20tuning/dataviz/python/2019/05/11/XGBoost-Tuning-Visual-Guide.html)\n",
|
|
"- [Notes on XGBoost Parameter Tuning](https://xgboost.readthedocs.io/en/latest/tutorials/param_tuning.html)\n",
|
|
"- [Doing XGBoost Hyperparameter Tuning the smart way](https://towardsdatascience.com/doing-xgboost-hyper-parameter-tuning-the-smart-way-part-1-of-2-f6d255a45dde)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.6"
|
|
},
|
|
"orphan": true
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|