-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from UrbanSystemsLab/htune
Support hyperparameter tuning.
- Loading branch information
Showing
5 changed files
with
181 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# AtmoML Hyperparameter Tuning" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import logging\n", | ||
"import keras_tuner\n", | ||
"import keras\n", | ||
"import time\n", | ||
"import pathlib\n", | ||
"\n", | ||
"from usl_models.atmo_ml.model import AtmoModel\n", | ||
"from usl_models.atmo_ml import dataset, visualizer, vars\n", | ||
"\n", | ||
"\n", | ||
"logging.getLogger().setLevel(logging.WARNING)\n", | ||
"keras.utils.set_random_seed(812)\n", | ||
"visualizer.init_plt()\n", | ||
"\n", | ||
"batch_size = 8\n", | ||
"filecache_dir = pathlib.Path(\"/home/shared/climateiq/filecache\")\n", | ||
"example_keys = [\n", | ||
" (\"NYC_Heat_Test/NYC_summer_2000_01p\", \"2000-05-25\"),\n", | ||
" (\"NYC_Heat_Test/NYC_summer_2000_01p\", \"2000-05-26\"),\n", | ||
" (\"NYC_Heat_Test/NYC_summer_2000_01p\", \"2000-05-27\"),\n", | ||
" (\"NYC_Heat_Test/NYC_summer_2000_01p\", \"2000-05-28\"),\n", | ||
" (\"PHX_Heat_Test/PHX_summer_2008_25p\", \"2008-05-25\"),\n", | ||
" (\"PHX_Heat_Test/PHX_summer_2008_25p\", \"2008-05-26\"),\n", | ||
" (\"PHX_Heat_Test/PHX_summer_2008_25p\", \"2008-05-27\"),\n", | ||
" (\"PHX_Heat_Test/PHX_summer_2008_25p\", \"2008-05-28\"),\n", | ||
"]\n", | ||
"timestamp = time.strftime(\"%Y%m%d-%H%M%S\")\n", | ||
"\n", | ||
"ds_config = dataset.Config(output_timesteps=2)\n", | ||
"train_ds = dataset.load_dataset_cached(\n", | ||
" filecache_dir,\n", | ||
" example_keys=example_keys,\n", | ||
" config=ds_config,\n", | ||
").batch(batch_size=batch_size)\n", | ||
"val_ds = dataset.load_dataset_cached(\n", | ||
" filecache_dir,\n", | ||
" example_keys=example_keys,\n", | ||
" config=ds_config,\n", | ||
" shuffle=False,\n", | ||
").batch(batch_size=batch_size)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"tuner = keras_tuner.BayesianOptimization(\n", | ||
" AtmoModel.get_hypermodel(\n", | ||
" input_cnn_kernel_size=[1, 2, 5],\n", | ||
" lstm_kernel_size=[5],\n", | ||
" ),\n", | ||
" objective=\"val_loss\",\n", | ||
" max_trials=10,\n", | ||
" project_name=f\"logs/htune_project_{timestamp}\",\n", | ||
")\n", | ||
"tuner.search_space_summary()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"log_dir = f\"logs/htune_{timestamp}\"\n", | ||
"print(log_dir)\n", | ||
"tb_callback = keras.callbacks.TensorBoard(log_dir=log_dir)\n", | ||
"tuner.search(train_ds, epochs=100, validation_data=val_ds, callbacks=[tb_callback])\n", | ||
"best_model, best_hp = tuner.get_best_models()[0], tuner.get_best_hyperparameters()[0]\n", | ||
"best_hp.values" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Train the best option further and save.\n", | ||
"model = AtmoModel(model=best_model)\n", | ||
"tb_callback = keras.callbacks.TensorBoard(log_dir=log_dir)\n", | ||
"model.fit(train_ds, val_ds, epochs=200, callbacks=[tb_callback], validation_freq=10)\n", | ||
"model.save_model(log_dir + \"/model\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 14, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Plot results\n", | ||
"model = AtmoModel.from_checkpoint(log_dir + \"/model\")\n", | ||
"input_batch, label_batch = next(iter(val_ds))\n", | ||
"pred_batch = model.call(input_batch)\n", | ||
"\n", | ||
"for fig in visualizer.plot_batch(\n", | ||
" input_batch=input_batch,\n", | ||
" label_batch=label_batch,\n", | ||
" pred_batch=pred_batch,\n", | ||
" st_var=vars.Spatiotemporal.RH,\n", | ||
" sto_var=vars.SpatiotemporalOutput.RH2,\n", | ||
" max_examples=None,\n", | ||
"):\n", | ||
" fig.show()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "base", | ||
"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.11.9" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters