diff --git a/14_deep_computer_vision_with_cnns.ipynb b/14_deep_computer_vision_with_cnns.ipynb index e1ef44d..873c66c 100644 --- a/14_deep_computer_vision_with_cnns.ipynb +++ b/14_deep_computer_vision_with_cnns.ipynb @@ -14,6 +14,17 @@ "_This notebook contains all the sample code in chapter 14._" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -25,7 +36,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." ] }, { @@ -42,11 +53,23 @@ "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", + " 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", "assert tf.__version__ >= \"2.0\"\n", "\n", + "if not tf.test.is_gpu_available():\n", + " 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", + "\n", "# Common imports\n", "import numpy as np\n", "import os\n", @@ -1412,7 +1435,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.7.3" }, "nav_menu": {}, "toc": { diff --git a/15_processing_sequences_using_rnns_and_cnns.ipynb b/15_processing_sequences_using_rnns_and_cnns.ipynb index 36b037b..f0f5f55 100644 --- a/15_processing_sequences_using_rnns_and_cnns.ipynb +++ b/15_processing_sequences_using_rnns_and_cnns.ipynb @@ -14,6 +14,17 @@ "_This notebook contains all the sample code in chapter 15._" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -25,7 +36,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." ] }, { @@ -42,11 +53,23 @@ "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", + " 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", "assert tf.__version__ >= \"2.0\"\n", "\n", + "if not tf.test.is_gpu_available():\n", + " 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", + "\n", "# Common imports\n", "import numpy as np\n", "import os\n", @@ -1116,8 +1139,6 @@ "metadata": {}, "outputs": [], "source": [ - "from tensorflow import keras\n", - "\n", "class GatedActivationUnit(keras.layers.Layer):\n", " def __init__(self, activation=\"tanh\", **kwargs):\n", " super().__init__(**kwargs)\n", @@ -1367,7 +1388,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.7.3" }, "nav_menu": {}, "toc": { diff --git a/16_nlp_with_rnns_and_attention.ipynb b/16_nlp_with_rnns_and_attention.ipynb index e12c6b6..24db9a7 100644 --- a/16_nlp_with_rnns_and_attention.ipynb +++ b/16_nlp_with_rnns_and_attention.ipynb @@ -14,6 +14,17 @@ "_This notebook contains all the sample code in chapter 16._" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -25,7 +36,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." ] }, { @@ -42,11 +53,24 @@ "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 tensorflow-addons\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", "assert tf.__version__ >= \"2.0\"\n", "\n", + "if not tf.test.is_gpu_available():\n", + " 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", + "\n", "# Common imports\n", "import numpy as np\n", "import os\n", @@ -1213,7 +1237,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.7.3" }, "nav_menu": {}, "toc": { diff --git a/17_autoencoders_and_gans.ipynb b/17_autoencoders_and_gans.ipynb index 16b2151..fa989a8 100644 --- a/17_autoencoders_and_gans.ipynb +++ b/17_autoencoders_and_gans.ipynb @@ -14,6 +14,17 @@ "_This notebook contains all the sample code in chapter 17._" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -25,7 +36,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." ] }, { @@ -42,11 +53,23 @@ "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", + " 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", "assert tf.__version__ >= \"2.0\"\n", "\n", + "if not tf.test.is_gpu_available():\n", + " 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", + "\n", "# Common imports\n", "import numpy as np\n", "import os\n", @@ -1598,7 +1621,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.7.3" }, "nav_menu": { "height": "381px", diff --git a/18_reinforcement_learning.ipynb b/18_reinforcement_learning.ipynb index fde8b2b..55458ae 100644 --- a/18_reinforcement_learning.ipynb +++ b/18_reinforcement_learning.ipynb @@ -14,6 +14,17 @@ "_This notebook contains all the sample code in chapter 18_." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -25,7 +36,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." ] }, { @@ -42,11 +53,25 @@ "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", + " !apt update && apt install -y libpq-dev libsdl2-dev swig xorg-dev xvfb\n", + " !pip install -q -U tf-agents-nightly pyvirtualdisplay gym[atari]\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", "assert tf.__version__ >= \"2.0\"\n", "\n", + "if not tf.test.is_gpu_available():\n", + " 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", + "\n", "# Common imports\n", "import numpy as np\n", "import os\n", @@ -2752,7 +2777,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/19_training_and_deploying_at_scale.ipynb b/19_training_and_deploying_at_scale.ipynb index 2e0e093..6c0a9a3 100644 --- a/19_training_and_deploying_at_scale.ipynb +++ b/19_training_and_deploying_at_scale.ipynb @@ -14,12 +14,23 @@ "_This notebook contains all the sample code in chapter 19._" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, { "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-preview.\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.\n" ] }, { @@ -36,11 +47,27 @@ "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", + " !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", + "# TensorFlow ≥2.0 is required\n", "import tensorflow as tf\n", "from tensorflow import keras\n", "assert tf.__version__ >= \"2.0\"\n", "\n", + "if not tf.test.is_gpu_available():\n", + " 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", + "\n", "# Common imports\n", "import numpy as np\n", "import os\n", @@ -292,11 +319,49 @@ "Once you are finished using it, press Ctrl-C to shut down the server." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Alternatively, if `tensorflow_model_server` is installed (e.g., if you are running this notebook in Colab), then the following 3 cells will start the server:" + ] + }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], + "source": [ + "os.environ[\"MODEL_DIR\"] = os.path.split(os.path.abspath(model_path))[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "%%bash --bg\n", + "nohup tensorflow_model_server \\\n", + " --rest_api_port=8501 \\\n", + " --model_name=my_mnist_model \\\n", + " --model_base_path=\"${MODEL_DIR}\" >server.log 2>&1" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "!tail server.log" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], "source": [ "import json\n", "\n", @@ -308,7 +373,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ @@ -324,7 +389,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -338,7 +403,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -347,7 +412,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ @@ -364,7 +429,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ @@ -379,7 +444,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ @@ -393,7 +458,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -409,7 +474,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 28, "metadata": { "scrolled": true }, @@ -418,7 +483,7 @@ "output_name = model.output_names[0]\n", "outputs_proto = response.outputs[output_name]\n", "y_proba = tf.make_ndarray(outputs_proto)\n", - "y_proba.numpy().round(2)" + "y_proba.round(2)" ] }, { @@ -430,7 +495,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ @@ -450,7 +515,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 30, "metadata": { "scrolled": true }, @@ -460,7 +525,7 @@ "tf.random.set_seed(42)\n", "\n", "model = keras.models.Sequential([\n", - " keras.layers.Flatten(input_shape=[28, 28]),\n", + " keras.layers.Flatten(input_shape=[28, 28, 1]),\n", " keras.layers.Dense(50, activation=\"relu\"),\n", " keras.layers.Dense(50, activation=\"relu\"),\n", " keras.layers.Dense(10, activation=\"softmax\")\n", @@ -473,7 +538,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -485,7 +550,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ @@ -494,7 +559,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ @@ -514,7 +579,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ @@ -529,7 +594,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 35, "metadata": {}, "outputs": [], "source": [ @@ -538,7 +603,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 36, "metadata": {}, "outputs": [], "source": [ @@ -562,7 +627,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 37, "metadata": {}, "outputs": [], "source": [ @@ -571,7 +636,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ @@ -586,7 +651,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 39, "metadata": {}, "outputs": [], "source": [ @@ -602,7 +667,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 40, "metadata": {}, "outputs": [], "source": [ @@ -619,7 +684,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ @@ -628,7 +693,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 42, "metadata": {}, "outputs": [], "source": [ @@ -637,7 +702,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 43, "metadata": {}, "outputs": [], "source": [ @@ -646,7 +711,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ @@ -665,7 +730,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ @@ -676,7 +741,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ @@ -699,7 +764,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ @@ -714,7 +779,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ @@ -747,7 +812,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 49, "metadata": {}, "outputs": [], "source": [ @@ -758,7 +823,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ @@ -774,7 +839,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 51, "metadata": {}, "outputs": [], "source": [ @@ -823,7 +888,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ @@ -867,7 +932,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 53, "metadata": {}, "outputs": [], "source": [ @@ -900,7 +965,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ @@ -921,7 +986,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ @@ -940,7 +1005,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 56, "metadata": {}, "outputs": [], "source": [ @@ -950,7 +1015,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ @@ -966,7 +1031,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 58, "metadata": {}, "outputs": [], "source": [ @@ -994,7 +1059,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 59, "metadata": {}, "outputs": [], "source": [ @@ -1043,7 +1108,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 60, "metadata": {}, "outputs": [], "source": [ @@ -1057,7 +1122,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 61, "metadata": {}, "outputs": [], "source": [ @@ -1081,7 +1146,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.7.3" } }, "nbformat": 4,