Make notebook 13 runnable in Colab without changes

main
Aurélien Geron 2019-11-06 12:16:20 +08:00
parent f810964f51
commit 56d8c08d3e
1 changed files with 26 additions and 20 deletions

View File

@ -9,6 +9,17 @@
"_This notebook contains all the sample code and solutions to the exercises in chapter 13._" "_This notebook contains all the sample code and solutions to the exercises in chapter 13._"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/ageron/handson-ml2/blob/master/13_loading_and_preprocessing_data.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
"</table>"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -20,7 +31,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "import sklearn\n",
"assert sklearn.__version__ >= \"0.20\"\n", "assert sklearn.__version__ >= \"0.20\"\n",
"\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", "import tensorflow as tf\n",
"from tensorflow import keras\n", "from tensorflow import keras\n",
"assert tf.__version__ >= \"2.0\"\n", "assert tf.__version__ >= \"2.0\"\n",
@ -1379,8 +1398,7 @@
"HOUSING_URL = DOWNLOAD_ROOT + \"datasets/housing/housing.tgz\"\n", "HOUSING_URL = DOWNLOAD_ROOT + \"datasets/housing/housing.tgz\"\n",
"\n", "\n",
"def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):\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, exist_ok=True)\n",
" os.makedirs(housing_path)\n",
" tgz_path = os.path.join(housing_path, \"housing.tgz\")\n", " tgz_path = os.path.join(housing_path, \"housing.tgz\")\n",
" urllib.request.urlretrieve(housing_url, tgz_path)\n", " urllib.request.urlretrieve(housing_url, tgz_path)\n",
" housing_tgz = tarfile.open(tgz_path)\n", " housing_tgz = tarfile.open(tgz_path)\n",
@ -1747,18 +1765,6 @@
"model.fit(mnist_train, steps_per_epoch=60000 // 32, epochs=5)" "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", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -1768,7 +1774,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 111, "execution_count": 110,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -1787,7 +1793,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 112, "execution_count": 111,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -1797,7 +1803,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 113, "execution_count": 112,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -1828,7 +1834,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.7.4" "version": "3.7.3"
}, },
"nav_menu": { "nav_menu": {
"height": "264px", "height": "264px",