From c3020f1da5739692ab941f4b94aafd526ad3cec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Tue, 25 May 2021 12:07:29 +1200 Subject: [PATCH 1/2] Support running on Kaggle --- 03_classification.ipynb | 15 +++++----- 13_loading_and_preprocessing_data.ipynb | 18 ++++++------ 14_deep_computer_vision_with_cnns.ipynb | 15 +++++----- ...essing_sequences_using_rnns_and_cnns.ipynb | 15 +++++----- 16_nlp_with_rnns_and_attention.ipynb | 21 +++++++------- 17_autoencoders_and_gans.ipynb | 15 +++++----- 18_reinforcement_learning.ipynb | 29 ++++++++----------- 19_training_and_deploying_at_scale.ipynb | 21 +++++++------- 8 files changed, 72 insertions(+), 77 deletions(-) diff --git a/03_classification.ipynb b/03_classification.ipynb index 486fd0c..7bbeef5 100644 --- a/03_classification.ipynb +++ b/03_classification.ipynb @@ -44,6 +44,10 @@ "import sys\n", "assert sys.version_info >= (3, 5)\n", "\n", + "# Is this notebook running on Colab or Kaggle?\n", + "IS_COLAB = \"google.colab\" in sys.modules\n", + "IS_KAGGLE = \"kaggle_secrets\" in sys.modules\n", + "\n", "# Scikit-Learn ≥0.20 is required\n", "import sklearn\n", "assert sklearn.__version__ >= \"0.20\"\n", @@ -2330,12 +2334,9 @@ "metadata": {}, "outputs": [], "source": [ - "# if running this notebook on Colab, we just pip install urlextract\n", - "try:\n", - " import google.colab\n", - " !pip install -q -U urlextract\n", - "except ImportError:\n", - " pass # not running on Colab" + "# if running this notebook on Colab or Kaggle, we just pip install urlextract\n", + "if IS_COLAB or IS_KAGGLE:\n", + " !pip install -q -U urlextract" ] }, { @@ -2601,7 +2602,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.9" + "version": "3.7.10" }, "nav_menu": {}, "toc": { diff --git a/13_loading_and_preprocessing_data.ipynb b/13_loading_and_preprocessing_data.ipynb index 0986941..ff2cfac 100644 --- a/13_loading_and_preprocessing_data.ipynb +++ b/13_loading_and_preprocessing_data.ipynb @@ -44,18 +44,18 @@ "import sys\n", "assert sys.version_info >= (3, 5)\n", "\n", + "# Is this notebook running on Colab or Kaggle?\n", + "IS_COLAB = \"google.colab\" in sys.modules\n", + "IS_KAGGLE = \"kaggle_secrets\" in sys.modules\n", + "\n", + "if IS_COLAB or IS_KAGGLE:\n", + " !pip install -q -U tfx==0.21.2\n", + " print(\"You can safely ignore the package incompatibility errors.\")\n", + "\n", "# Scikit-Learn ≥0.20 is required\n", "import sklearn\n", "assert sklearn.__version__ >= \"0.20\"\n", "\n", - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 2.x\n", - " !pip install -q -U tfx==0.21.2\n", - " print(\"You can safely ignore the package incompatibility errors.\")\n", - "except Exception:\n", - " pass\n", - "\n", "# TensorFlow ≥2.0 is required\n", "import tensorflow as tf\n", "from tensorflow import keras\n", @@ -2766,7 +2766,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.9" + "version": "3.7.10" }, "nav_menu": { "height": "264px", diff --git a/14_deep_computer_vision_with_cnns.ipynb b/14_deep_computer_vision_with_cnns.ipynb index c1b9e2f..2122d2b 100644 --- a/14_deep_computer_vision_with_cnns.ipynb +++ b/14_deep_computer_vision_with_cnns.ipynb @@ -49,17 +49,14 @@ "import sys\n", "assert sys.version_info >= (3, 5)\n", "\n", + "# Is this notebook running on Colab or Kaggle?\n", + "IS_COLAB = \"google.colab\" in sys.modules\n", + "IS_KAGGLE = \"kaggle_secrets\" in sys.modules\n", + "\n", "# Scikit-Learn ≥0.20 is required\n", "import sklearn\n", "assert sklearn.__version__ >= \"0.20\"\n", "\n", - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 2.x\n", - " IS_COLAB = True\n", - "except Exception:\n", - " IS_COLAB = False\n", - "\n", "# TensorFlow ≥2.0 is required\n", "import tensorflow as tf\n", "from tensorflow import keras\n", @@ -69,6 +66,8 @@ " print(\"No GPU was detected. CNNs can be very slow without a GPU.\")\n", " if IS_COLAB:\n", " print(\"Go to Runtime > Change runtime and select a GPU hardware accelerator.\")\n", + " if IS_KAGGLE:\n", + " print(\"Go to Settings > Accelerator and select GPU.\")\n", "\n", "# Common imports\n", "import numpy as np\n", @@ -1366,7 +1365,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.9" + "version": "3.7.10" }, "nav_menu": {}, "toc": { diff --git a/15_processing_sequences_using_rnns_and_cnns.ipynb b/15_processing_sequences_using_rnns_and_cnns.ipynb index b846b42..2cd2c80 100644 --- a/15_processing_sequences_using_rnns_and_cnns.ipynb +++ b/15_processing_sequences_using_rnns_and_cnns.ipynb @@ -49,17 +49,14 @@ "import sys\n", "assert sys.version_info >= (3, 5)\n", "\n", + "# Is this notebook running on Colab or Kaggle?\n", + "IS_COLAB = \"google.colab\" in sys.modules\n", + "IS_KAGGLE = \"kaggle_secrets\" in sys.modules\n", + "\n", "# Scikit-Learn ≥0.20 is required\n", "import sklearn\n", "assert sklearn.__version__ >= \"0.20\"\n", "\n", - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 2.x\n", - " IS_COLAB = True\n", - "except Exception:\n", - " IS_COLAB = False\n", - "\n", "# TensorFlow ≥2.0 is required\n", "import tensorflow as tf\n", "from tensorflow import keras\n", @@ -69,6 +66,8 @@ " print(\"No GPU was detected. LSTMs and CNNs can be very slow without a GPU.\")\n", " if IS_COLAB:\n", " print(\"Go to Runtime > Change runtime and select a GPU hardware accelerator.\")\n", + " if IS_KAGGLE:\n", + " print(\"Go to Settings > Accelerator and select GPU.\")\n", "\n", "# Common imports\n", "import numpy as np\n", @@ -2018,7 +2017,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.9" + "version": "3.7.10" }, "nav_menu": {}, "toc": { diff --git a/16_nlp_with_rnns_and_attention.ipynb b/16_nlp_with_rnns_and_attention.ipynb index ad5963a..0df9c80 100644 --- a/16_nlp_with_rnns_and_attention.ipynb +++ b/16_nlp_with_rnns_and_attention.ipynb @@ -49,19 +49,18 @@ "import sys\n", "assert sys.version_info >= (3, 5)\n", "\n", + "# Is this notebook running on Colab or Kaggle?\n", + "IS_COLAB = \"google.colab\" in sys.modules\n", + "IS_KAGGLE = \"kaggle_secrets\" in sys.modules\n", + "\n", + "if IS_COLAB:\n", + " !pip install -q -U tensorflow-addons\n", + " !pip install -q -U transformers\n", + "\n", "# Scikit-Learn ≥0.20 is required\n", "import sklearn\n", "assert sklearn.__version__ >= \"0.20\"\n", "\n", - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 2.x\n", - " !pip install -q -U tensorflow-addons\n", - " !pip install -q -U transformers\n", - " IS_COLAB = True\n", - "except Exception:\n", - " IS_COLAB = False\n", - "\n", "# TensorFlow ≥2.0 is required\n", "import tensorflow as tf\n", "from tensorflow import keras\n", @@ -71,6 +70,8 @@ " print(\"No GPU was detected. LSTMs and CNNs can be very slow without a GPU.\")\n", " if IS_COLAB:\n", " print(\"Go to Runtime > Change runtime and select a GPU hardware accelerator.\")\n", + " if IS_KAGGLE:\n", + " print(\"Go to Settings > Accelerator and select GPU.\")\n", "\n", "# Common imports\n", "import numpy as np\n", @@ -2748,7 +2749,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.9" + "version": "3.7.10" }, "nav_menu": {}, "toc": { diff --git a/17_autoencoders_and_gans.ipynb b/17_autoencoders_and_gans.ipynb index 40d6bbe..52fc1a5 100644 --- a/17_autoencoders_and_gans.ipynb +++ b/17_autoencoders_and_gans.ipynb @@ -49,17 +49,14 @@ "import sys\n", "assert sys.version_info >= (3, 5)\n", "\n", + "# Is this notebook running on Colab or Kaggle?\n", + "IS_COLAB = \"google.colab\" in sys.modules\n", + "IS_KAGGLE = \"kaggle_secrets\" in sys.modules\n", + "\n", "# Scikit-Learn ≥0.20 is required\n", "import sklearn\n", "assert sklearn.__version__ >= \"0.20\"\n", "\n", - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 2.x\n", - " IS_COLAB = True\n", - "except Exception:\n", - " IS_COLAB = False\n", - "\n", "# TensorFlow ≥2.0 is required\n", "import tensorflow as tf\n", "from tensorflow import keras\n", @@ -69,6 +66,8 @@ " print(\"No GPU was detected. LSTMs and CNNs can be very slow without a GPU.\")\n", " if IS_COLAB:\n", " print(\"Go to Runtime > Change runtime and select a GPU hardware accelerator.\")\n", + " if IS_KAGGLE:\n", + " print(\"Go to Settings > Accelerator and select GPU.\")\n", "\n", "# Common imports\n", "import numpy as np\n", @@ -1773,7 +1772,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.9" + "version": "3.7.10" }, "nav_menu": { "height": "381px", diff --git a/18_reinforcement_learning.ipynb b/18_reinforcement_learning.ipynb index 10e07b6..c75f352 100644 --- a/18_reinforcement_learning.ipynb +++ b/18_reinforcement_learning.ipynb @@ -29,13 +29,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Setup" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ + "# Setup\n", "First, let's import a few common modules, ensure MatplotLib plots figures inline and prepare a function to save the figures. We also check that Python 3.5 or later is installed (although Python 2.x may work, it is deprecated so we strongly recommend you use Python 3 instead), as well as Scikit-Learn ≥0.20 and TensorFlow ≥2.0." ] }, @@ -49,19 +43,18 @@ "import sys\n", "assert sys.version_info >= (3, 5)\n", "\n", + "# Is this notebook running on Colab or Kaggle?\n", + "IS_COLAB = \"google.colab\" in sys.modules\n", + "IS_KAGGLE = \"kaggle_secrets\" in sys.modules\n", + "\n", + "if IS_COLAB or IS_KAGGLE:\n", + " !apt update && apt install -y libpq-dev libsdl2-dev swig xorg-dev xvfb\n", + " !pip install -q -U tf-agents pyvirtualdisplay gym[atari,box2d]\n", + "\n", "# Scikit-Learn ≥0.20 is required\n", "import sklearn\n", "assert sklearn.__version__ >= \"0.20\"\n", "\n", - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 2.x\n", - " !apt update && apt install -y libpq-dev libsdl2-dev swig xorg-dev xvfb\n", - " !pip install -q -U tf-agents pyvirtualdisplay gym[atari,box2d]\n", - " IS_COLAB = True\n", - "except Exception:\n", - " IS_COLAB = False\n", - "\n", "# TensorFlow ≥2.0 is required\n", "import tensorflow as tf\n", "from tensorflow import keras\n", @@ -71,6 +64,8 @@ " print(\"No GPU was detected. CNNs can be very slow without a GPU.\")\n", " if IS_COLAB:\n", " print(\"Go to Runtime > Change runtime and select a GPU hardware accelerator.\")\n", + " if IS_KAGGLE:\n", + " print(\"Go to Settings > Accelerator and select GPU.\")\n", "\n", "# Common imports\n", "import numpy as np\n", @@ -3223,7 +3218,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.9" + "version": "3.7.10" } }, "nbformat": 4, diff --git a/19_training_and_deploying_at_scale.ipynb b/19_training_and_deploying_at_scale.ipynb index e5a67ae..b2cb440 100644 --- a/19_training_and_deploying_at_scale.ipynb +++ b/19_training_and_deploying_at_scale.ipynb @@ -43,20 +43,19 @@ "import sys\n", "assert sys.version_info >= (3, 5)\n", "\n", - "# Scikit-Learn ≥0.20 is required\n", - "import sklearn\n", - "assert sklearn.__version__ >= \"0.20\"\n", + "# Is this notebook running on Colab or Kaggle?\n", + "IS_COLAB = \"google.colab\" in sys.modules\n", + "IS_KAGGLE = \"kaggle_secrets\" in sys.modules\n", "\n", - "try:\n", - " # %tensorflow_version only exists in Colab.\n", - " %tensorflow_version 2.x\n", + "if IS_COLAB or IS_KAGGLE:\n", " !echo \"deb http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal\" > /etc/apt/sources.list.d/tensorflow-serving.list\n", " !curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | apt-key add -\n", " !apt update && apt-get install -y tensorflow-model-server\n", " !pip install -q -U tensorflow-serving-api\n", - " IS_COLAB = True\n", - "except Exception:\n", - " IS_COLAB = False\n", + "\n", + "# Scikit-Learn ≥0.20 is required\n", + "import sklearn\n", + "assert sklearn.__version__ >= \"0.20\"\n", "\n", "# TensorFlow ≥2.0 is required\n", "import tensorflow as tf\n", @@ -67,6 +66,8 @@ " print(\"No GPU was detected. CNNs can be very slow without a GPU.\")\n", " if IS_COLAB:\n", " print(\"Go to Runtime > Change runtime and select a GPU hardware accelerator.\")\n", + " if IS_KAGGLE:\n", + " print(\"Go to Settings > Accelerator and select GPU.\")\n", "\n", "# Common imports\n", "import numpy as np\n", @@ -1250,7 +1251,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.9" + "version": "3.7.10" } }, "nbformat": 4, From 0af1cbf349ca445767c04300e1dd2304089872d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Tue, 25 May 2021 15:14:37 +1200 Subject: [PATCH 2/2] Fix the links to hosting services, and add Kaggle --- README.md | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7288291..d9d3eb3 100644 --- a/README.md +++ b/README.md @@ -11,20 +11,17 @@ python. It contains the example code and solutions to the exercises in the secon ## Quick Start ### Want to play with these notebooks online without having to install anything? -Use any of the following services. +Use any of the following services (I recommended Colab or Kaggle, since they offer free GPUs and TPUs). -**WARNING**: Please be aware that these services provide temporary environments: anything you do will be deleted after a while, so make sure you download any data you care about. +**WARNING**: _Please be aware that these services provide temporary environments: anything you do will be deleted after a while, so make sure you download any data you care about._ -* **Recommended**: open this repository in [Colaboratory](https://colab.research.google.com/github/ageron/handson-ml2/blob/master/): - +* Open In Colab -* Or open it in [Binder](https://mybinder.org/v2/gh/ageron/handson-ml2/master): - +* Open in Kaggle - * _Note_: Most of the time, Binder starts up quickly and works great, but when handson-ml2 is updated, Binder creates a new environment from scratch, and this can take quite some time. +* Launch binder -* Or open it in [Deepnote](https://beta.deepnote.com/launch?template=data-science&url=https%3A//github.com/ageron/handson-ml2/blob/master/index.ipynb): - +* Launch in Deepnote ### Just want to quickly look at some notebooks, without executing any code?