|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "##### Copyright 2019 The TensorFlow Authors." |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 8, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", |
| 17 | + "# you may not use this file except in compliance with the License.\n", |
| 18 | + "# You may obtain a copy of the License at\n", |
| 19 | + "#\n", |
| 20 | + "# https://www.apache.org/licenses/LICENSE-2.0\n", |
| 21 | + "#\n", |
| 22 | + "# Unless required by applicable law or agreed to in writing, software\n", |
| 23 | + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", |
| 24 | + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", |
| 25 | + "# See the License for the specific language governing permissions and\n", |
| 26 | + "# limitations under the License." |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "metadata": {}, |
| 32 | + "source": [ |
| 33 | + "# TensorFlow Addons Callbacks: TimeStopping" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "<table class=\"tfo-notebook-buttons\" align=\"left\">\n", |
| 41 | + " <td>\n", |
| 42 | + " <a target=\"_blank\" href=\"https://www.tensorflow.org/addons/tutorials/time_stopping\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n", |
| 43 | + " </td>\n", |
| 44 | + " <td>\n", |
| 45 | + " <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/addons/blob/master/docs/tutorials/time_stopping.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n", |
| 46 | + " </td>\n", |
| 47 | + " <td>\n", |
| 48 | + " <a target=\"_blank\" href=\"https://github.com/tensorflow/addons/blob/master/docs/tutorials/time_stopping.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n", |
| 49 | + " </td>\n", |
| 50 | + " <td>\n", |
| 51 | + " <a href=\"https://storage.googleapis.com/tensorflow_docs/addons/docs/tutorials/time_stopping.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n", |
| 52 | + " </td>\n", |
| 53 | + "</table>" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "## Overview\n", |
| 61 | + "This notebook will demonstrate how to use TimeStopping Callback in TensorFlow Addons." |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "markdown", |
| 66 | + "metadata": {}, |
| 67 | + "source": [ |
| 68 | + "## Setup" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": 9, |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [], |
| 76 | + "source": [ |
| 77 | + "!pip install -q --no-deps tensorflow-addons~=0.6" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": 10, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "try:\n", |
| 87 | + " # %tensorflow_version only exists in Colab.\n", |
| 88 | + " %tensorflow_version 2.x\n", |
| 89 | + "except Exception:\n", |
| 90 | + " pass" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": 11, |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "import tensorflow as tf\n", |
| 100 | + "import tensorflow_addons as tfa\n", |
| 101 | + "\n", |
| 102 | + "import tensorflow.keras as keras\n", |
| 103 | + "from tensorflow.keras.datasets import mnist\n", |
| 104 | + "from tensorflow.keras.models import Sequential\n", |
| 105 | + "from tensorflow.keras.layers import Dense, Dropout, Flatten" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "metadata": {}, |
| 111 | + "source": [ |
| 112 | + "## Import and Normalize Data" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": 12, |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [], |
| 120 | + "source": [ |
| 121 | + "# the data, split between train and test sets\n", |
| 122 | + "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", |
| 123 | + "# normalize data\n", |
| 124 | + "x_train, x_test = x_train / 255.0, x_test / 255.0" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "metadata": {}, |
| 130 | + "source": [ |
| 131 | + "## Build Simple MNIST CNN Model" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 13, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "# build the model using the Sequential API\n", |
| 141 | + "model = Sequential()\n", |
| 142 | + "model.add(Flatten(input_shape=(28, 28)))\n", |
| 143 | + "model.add(Dense(128, activation='relu'))\n", |
| 144 | + "model.add(Dropout(0.2))\n", |
| 145 | + "model.add(Dense(10, activation='softmax'))\n", |
| 146 | + "\n", |
| 147 | + "model.compile(optimizer='adam',\n", |
| 148 | + " loss = 'sparse_categorical_crossentropy',\n", |
| 149 | + " metrics=['accuracy'])" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "markdown", |
| 154 | + "metadata": {}, |
| 155 | + "source": [ |
| 156 | + "## Simple TimeStopping Usage" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": 14, |
| 162 | + "metadata": { |
| 163 | + "scrolled": true |
| 164 | + }, |
| 165 | + "outputs": [ |
| 166 | + { |
| 167 | + "name": "stdout", |
| 168 | + "output_type": "stream", |
| 169 | + "text": [ |
| 170 | + "Train on 60000 samples, validate on 10000 samples\n", |
| 171 | + "Epoch 1/100\n", |
| 172 | + "60000/60000 [==============================] - 2s 28us/sample - loss: 0.3357 - accuracy: 0.9033 - val_loss: 0.1606 - val_accuracy: 0.9533\n", |
| 173 | + "Epoch 2/100\n", |
| 174 | + "60000/60000 [==============================] - 1s 23us/sample - loss: 0.1606 - accuracy: 0.9525 - val_loss: 0.1104 - val_accuracy: 0.9669\n", |
| 175 | + "Epoch 3/100\n", |
| 176 | + "60000/60000 [==============================] - 1s 24us/sample - loss: 0.1185 - accuracy: 0.9645 - val_loss: 0.0949 - val_accuracy: 0.9704\n", |
| 177 | + "Epoch 4/100\n", |
| 178 | + "60000/60000 [==============================] - 1s 25us/sample - loss: 0.0954 - accuracy: 0.9713 - val_loss: 0.0854 - val_accuracy: 0.9740\n", |
| 179 | + "Timed stopping at epoch 4 after training for 0:00:05\n" |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "data": { |
| 184 | + "text/plain": [ |
| 185 | + "<tensorflow.python.keras.callbacks.History at 0x110af0ef0>" |
| 186 | + ] |
| 187 | + }, |
| 188 | + "execution_count": 14, |
| 189 | + "metadata": {}, |
| 190 | + "output_type": "execute_result" |
| 191 | + } |
| 192 | + ], |
| 193 | + "source": [ |
| 194 | + "# initialize TimeStopping callback \n", |
| 195 | + "time_stopping_callback = tfa.callbacks.TimeStopping(seconds=5, verbose=1)\n", |
| 196 | + "\n", |
| 197 | + "# train the model with tqdm_callback\n", |
| 198 | + "# make sure to set verbose = 0 to disable\n", |
| 199 | + "# the default progress bar.\n", |
| 200 | + "model.fit(x_train, y_train,\n", |
| 201 | + " batch_size=64,\n", |
| 202 | + " epochs=100,\n", |
| 203 | + " callbacks=[time_stopping_callback],\n", |
| 204 | + " validation_data=(x_test, y_test))" |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": null, |
| 210 | + "metadata": {}, |
| 211 | + "outputs": [], |
| 212 | + "source": [] |
| 213 | + } |
| 214 | + ], |
| 215 | + "metadata": { |
| 216 | + "kernelspec": { |
| 217 | + "display_name": "Python 3", |
| 218 | + "language": "python", |
| 219 | + "name": "python3" |
| 220 | + }, |
| 221 | + "language_info": { |
| 222 | + "codemirror_mode": { |
| 223 | + "name": "ipython", |
| 224 | + "version": 3 |
| 225 | + }, |
| 226 | + "file_extension": ".py", |
| 227 | + "mimetype": "text/x-python", |
| 228 | + "name": "python", |
| 229 | + "nbconvert_exporter": "python", |
| 230 | + "pygments_lexer": "ipython3", |
| 231 | + "version": "3.6.2" |
| 232 | + } |
| 233 | + }, |
| 234 | + "nbformat": 4, |
| 235 | + "nbformat_minor": 2 |
| 236 | +} |
0 commit comments