feat: xgboost notebook (#246)

* feat: friday update

* feat: friday update

* feat: hpt notebook

* feat: hpt notebook

* feat: sklearn

* feat: sklearn

* feat: xgb training

* feat: xgb training
This commit is contained in:
Andrew Ferlitsch
2022-01-26 12:09:07 -08:00
committed by GitHub
parent b4d02f486e
commit a3047fb1bb
2 changed files with 1066 additions and 4 deletions
@@ -87,8 +87,9 @@
"\n",
"The steps performed include:\n",
"\n",
"- Training using a single Python script.\n",
"- Training using a Python package."
"- Training using a Python package.\n",
"- Report accuracy when hyperparameter tuning.\n",
"- Create a `Vertex AI Model` resource."
]
},
{
@@ -519,7 +520,9 @@
"\n",
"1. Save the in-memory model to the local filesystem in pickle format (e.g., model.pkl).\n",
"2. Create a Cloud Storage storage client.\n",
"3. Upload the pickle file as a blob to the specified Cloud Storage location using the Cloud Storage storage client."
"3. Upload the pickle file as a blob to the specified Cloud Storage location using the Cloud Storage storage client.\n",
"\n",
"*Note*: You can do hyperparameter tuning with a XGBoost model."
]
},
{
@@ -569,7 +572,7 @@
"setup_cfg = \"[egg_info]\\n\\ntag_build =\\n\\ntag_date = 0\"\n",
"! echo \"$setup_cfg\" > custom/setup.cfg\n",
"\n",
"setup_py = \"import setuptools\\n\\nsetuptools.setup(\\n\\n install_requires=[\\n\\n 'wget',\\n\\n ],\\n\\n packages=setuptools.find_packages())\"\n",
"setup_py = \"import setuptools\\n\\nsetuptools.setup(\\n\\n install_requires=[\\n\\n 'wget',\\n\\n 'cloudml-hypertune',\\n\\n ],\\n\\n packages=setuptools.find_packages())\"\n",
"! echo \"$setup_py\" > custom/setup.py\n",
"\n",
"pkg_info = \"Metadata-Version: 1.0\\n\\nName: News Aggregation text classification\\n\\nVersion: 0.0.0\\n\\nSummary: Demostration training script\\n\\nHome-page: www.google.com\\n\\nAuthor: Google\\n\\nAuthor-email: aferlitsch@google.com\\n\\nLicense: Public\\n\\nDescription: Demo\\n\\nPlatform: Vertex\"\n",
@@ -600,6 +603,9 @@
" - Builds the corresponding model architecture.\n",
"- Training (`train_model()`):\n",
" - Trains the model\n",
"- Evaluation (`evaluate_model()`):\n",
" - Evaluates the model.\n",
" - If hyperparameter tuning, reports the metric for accuracy.\n",
"- Model artifact saving\n",
" - Saves the model artifacts and evaluation metrics where the Cloud Storage location specified by `model-dir`."
]
@@ -627,6 +633,7 @@
"from sklearn.model_selection import train_test_split\n",
"from sklearn.naive_bayes import MultinomialNB\n",
"from sklearn.pipeline import Pipeline\n",
"import hypertune\n",
"\n",
"parser = argparse.ArgumentParser()\n",
"parser.add_argument('--model-dir', dest='model_dir',\n",
@@ -682,6 +689,13 @@
") -> float:\n",
" score = model.score(X_test, y_test)\n",
" logging.info(f\"Evaluation completed with model score: {score}\")\n",
"\n",
" # report metric for hyperparameter tuning\n",
" hpt = hypertune.HyperTune()\n",
" hpt.report_hyperparameter_tuning_metric(\n",
" hyperparameter_metric_tag='accuracy',\n",
" metric_value=score\n",
" )\n",
" return score\n",
"\n",
"def export_model_to_gcs(fitted_pipeline: Pipeline, gcs_uri: str) -> str:\n",
File diff suppressed because it is too large Load Diff