From 1f43973c59916085a9d7d02b79b18dabe6b81af3 Mon Sep 17 00:00:00 2001 From: Aurelien Geron Date: Wed, 6 Nov 2019 12:16:20 +0800 Subject: [PATCH] Make notebook 13 runnable in Colab without changes --- 13_loading_and_preprocessing_data.ipynb | 48 ++++++++++++++----------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/13_loading_and_preprocessing_data.ipynb b/13_loading_and_preprocessing_data.ipynb index 73b0838d2..3d8b497d9 100644 --- a/13_loading_and_preprocessing_data.ipynb +++ b/13_loading_and_preprocessing_data.ipynb @@ -9,6 +9,17 @@ "_This notebook contains all the sample code and solutions to the exercises in chapter 13._" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -20,7 +31,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "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-preview." + "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." ] }, { @@ -37,7 +48,15 @@ "import sklearn\n", "assert sklearn.__version__ >= \"0.20\"\n", "\n", - "# TensorFlow ≥2.0-preview is required\n", + "try:\n", + " # %tensorflow_version only exists in Colab.\n", + " %tensorflow_version 2.x\n", + " !pip install -q -U tfx==0.15.0rc0\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", "assert tf.__version__ >= \"2.0\"\n", @@ -2307,8 +2326,7 @@ "HOUSING_URL = DOWNLOAD_ROOT + \"datasets/housing/housing.tgz\"\n", "\n", "def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):\n", - " if not os.path.isdir(housing_path):\n", - " os.makedirs(housing_path)\n", + " os.makedirs(housing_path, exist_ok=True)\n", " tgz_path = os.path.join(housing_path, \"housing.tgz\")\n", " urllib.request.urlretrieve(housing_url, tgz_path)\n", " housing_tgz = tarfile.open(tgz_path)\n", @@ -2966,18 +2984,6 @@ "model.fit(mnist_train, steps_per_epoch=60000 // 32, epochs=5)" ] }, - { - "cell_type": "code", - "execution_count": 110, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " datasets = tfds.load(\"imagenet2012\", split=[\"train\", \"test\"])\n", - "except AssertionError as ex:\n", - " print(ex)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -2987,7 +2993,7 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": 110, "metadata": {}, "outputs": [ { @@ -3027,7 +3033,7 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 111, "metadata": {}, "outputs": [], "source": [ @@ -3037,7 +3043,7 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 112, "metadata": {}, "outputs": [ { @@ -3080,7 +3086,7 @@ " -4.97694984e-02, -1.07776590e-01]], dtype=float32)>" ] }, - "execution_count": 113, + "execution_count": 112, "metadata": {}, "output_type": "execute_result" } @@ -3113,7 +3119,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.3" }, "nav_menu": { "height": "264px",