|
70 | 70 | "source": [
|
71 | 71 | "This short introduction uses [Keras](https://www.tensorflow.org/guide/keras/overview) to:\n",
|
72 | 72 | "\n",
|
73 |
| - "1. Build a neural network that classifies images.\n", |
| 73 | + "1. Load a prebuilt dataset.\n", |
| 74 | + "1. Build a neural network machine learning model that classifies images.\n", |
74 | 75 | "2. Train this neural network.\n",
|
75 |
| - "3. And, finally, evaluate the accuracy of the model." |
| 76 | + "3. Evaluate the accuracy of the model." |
76 | 77 | ]
|
77 | 78 | },
|
78 | 79 | {
|
|
81 | 82 | "id": "hiH7AC-NTniF"
|
82 | 83 | },
|
83 | 84 | "source": [
|
84 |
| - "This is a [Google Colaboratory](https://colab.research.google.com/notebooks/welcome.ipynb) notebook file. Python programs are run directly in the browser—a great way to learn and use TensorFlow. To follow this tutorial, run the notebook in Google Colab by clicking the button at the top of this page.\n", |
| 85 | + "This tutorial is a [Google Colaboratory](https://colab.research.google.com/notebooks/welcome.ipynb) notebook. Python programs are run directly in the browser—a great way to learn and use TensorFlow. To follow this tutorial, run the notebook in Google Colab by clicking the button at the top of this page.\n", |
85 | 86 | "\n",
|
86 | 87 | "1. In Colab, connect to a Python runtime: At the top-right of the menu bar, select *CONNECT*.\n",
|
87 | 88 | "2. Run all the notebook code cells: Select *Runtime* > *Run all*."
|
|
93 | 94 | "id": "nnrWf3PCEzXL"
|
94 | 95 | },
|
95 | 96 | "source": [
|
96 |
| - "Download and install TensorFlow 2. Import TensorFlow into your program:\n", |
| 97 | + "## Set up TensorFlow\n", |
97 | 98 | "\n",
|
98 |
| - "Note: Upgrade `pip` to install the TensorFlow 2 package. See the [install guide](https://www.tensorflow.org/install) for details." |
| 99 | + "Import TensorFlow into your program to get started:" |
99 | 100 | ]
|
100 | 101 | },
|
101 | 102 | {
|
|
116 | 117 | "id": "7NAbSZiaoJ4z"
|
117 | 118 | },
|
118 | 119 | "source": [
|
119 |
| - "Load and prepare the [MNIST dataset](http://yann.lecun.com/exdb/mnist/). Convert the samples from integers to floating-point numbers:" |
| 120 | + "If you are following along in your own development environment, rather than [Colab](https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/quickstart/beginner.ipynb), see the [install guide](https://www.tensorflow.org/install) for setting up TensorFlow for development.\n", |
| 121 | + "\n", |
| 122 | + "Note: Make sure you have upgraded to the latest `pip` to install the TensorFlow 2 package if you are using your own development environment. See the [install guide](https://www.tensorflow.org/install) for details.\n", |
| 123 | + "\n", |
| 124 | + "## Load a dataset\n", |
| 125 | + "\n", |
| 126 | + "Load and prepare the [MNIST dataset](http://yann.lecun.com/exdb/mnist/). Convert the sample data from integers to floating-point numbers:" |
120 | 127 | ]
|
121 | 128 | },
|
122 | 129 | {
|
|
139 | 146 | "id": "BPZ68wASog_I"
|
140 | 147 | },
|
141 | 148 | "source": [
|
142 |
| - "Build the `tf.keras.Sequential` model by stacking layers. Choose an optimizer and loss function for training:" |
| 149 | + "## Build a machine learning model\n", |
| 150 | + "\n", |
| 151 | + "Build a `tf.keras.Sequential` model by stacking layers." |
143 | 152 | ]
|
144 | 153 | },
|
145 | 154 | {
|
|
164 | 173 | "id": "l2hiez2eIUz8"
|
165 | 174 | },
|
166 | 175 | "source": [
|
167 |
| - "For each example the model returns a vector of \"[logits](https://developers.google.com/machine-learning/glossary#logits)\" or \"[log-odds](https://developers.google.com/machine-learning/glossary#log-odds)\" scores, one for each class." |
| 176 | + "For each example, the model returns a vector of [logits](https://developers.google.com/machine-learning/glossary#logits) or [log-odds](https://developers.google.com/machine-learning/glossary#log-odds) scores, one for each class." |
168 | 177 | ]
|
169 | 178 | },
|
170 | 179 | {
|
|
185 | 194 | "id": "tgjhDQGcIniO"
|
186 | 195 | },
|
187 | 196 | "source": [
|
188 |
| - "The `tf.nn.softmax` function converts these logits to \"probabilities\" for each class: " |
| 197 | + "The `tf.nn.softmax` function converts these logits to *probabilities* for each class: " |
189 | 198 | ]
|
190 | 199 | },
|
191 | 200 | {
|
|
205 | 214 | "id": "he5u_okAYS4a"
|
206 | 215 | },
|
207 | 216 | "source": [
|
208 |
| - "Note: It is possible to bake this `tf.nn.softmax` in as the activation function for the last layer of the network. While this can make the model output more directly interpretable, this approach is discouraged as it's impossible to\n", |
209 |
| - "provide an exact and numerically stable loss calculation for all models when using a softmax output. " |
| 217 | + "Note: It is possible to bake the `tf.nn.softmax` function into the activation function for the last layer of the network. While this can make the model output more directly interpretable, this approach is discouraged as it's impossible to provide an exact and numerically stable loss calculation for all models when using a softmax output. " |
210 | 218 | ]
|
211 | 219 | },
|
212 | 220 | {
|
|
215 | 223 | "id": "hQyugpgRIyrA"
|
216 | 224 | },
|
217 | 225 | "source": [
|
218 |
| - "The `losses.SparseCategoricalCrossentropy` loss takes a vector of logits and a `True` index and returns a scalar loss for each example." |
| 226 | + "Define a loss function for training using `losses.SparseCategoricalCrossentropy`, which takes a vector of logits and a `True` index and returns a scalar loss for each example." |
219 | 227 | ]
|
220 | 228 | },
|
221 | 229 | {
|
|
235 | 243 | "id": "SfR4MsSDU880"
|
236 | 244 | },
|
237 | 245 | "source": [
|
238 |
| - "This loss is equal to the negative log probability of the true class:\n", |
239 |
| - "It is zero if the model is sure of the correct class.\n", |
| 246 | + "This loss is equal to the negative log probability of the true class: The loss is zero if the model is sure of the correct class.\n", |
240 | 247 | "\n",
|
241 | 248 | "This untrained model gives probabilities close to random (1/10 for each class), so the initial loss should be close to `-tf.math.log(1/10) ~= 2.3`."
|
242 | 249 | ]
|
|
252 | 259 | "loss_fn(y_train[:1], predictions).numpy()"
|
253 | 260 | ]
|
254 | 261 | },
|
| 262 | + { |
| 263 | + "cell_type": "markdown", |
| 264 | + "metadata": { |
| 265 | + "id": "ada44eb947d4" |
| 266 | + }, |
| 267 | + "source": [ |
| 268 | + "Before you start training, configure and compile the model using Keras `Model.compile`. Set the [`optimizer`](https://www.tensorflow.org/api_docs/python/tf/keras/optimizers) class to `adam`, set the `loss` to the `loss_fn` function you defined earlier, and specify a metric to be evaluated for the model by setting the `metrics` parameter to `accuracy`." |
| 269 | + ] |
| 270 | + }, |
255 | 271 | {
|
256 | 272 | "cell_type": "code",
|
257 | 273 | "execution_count": null,
|
|
271 | 287 | "id": "ix4mEL65on-w"
|
272 | 288 | },
|
273 | 289 | "source": [
|
274 |
| - "The `Model.fit` method adjusts the model parameters to minimize the loss: " |
| 290 | + "## Train and evaluate your model\n", |
| 291 | + "\n", |
| 292 | + "Use the `Model.fit` method to adjust your model parameters and minimize the loss: " |
275 | 293 | ]
|
276 | 294 | },
|
277 | 295 | {
|
|
347 | 365 | "source": [
|
348 | 366 | "probability_model(x_test[:5])"
|
349 | 367 | ]
|
| 368 | + }, |
| 369 | + { |
| 370 | + "cell_type": "markdown", |
| 371 | + "metadata": { |
| 372 | + "id": "-47O6_GLdRuT" |
| 373 | + }, |
| 374 | + "source": [ |
| 375 | + "## Conclusion\n", |
| 376 | + "\n", |
| 377 | + "Congratulations! You have trained a machine learning model using a prebuilt dataset using the [Keras](https://www.tensorflow.org/guide/keras/overview) API.\n", |
| 378 | + "\n", |
| 379 | + "For more examples of using Keras, check out the [tutorials](https://www.tensorflow.org/tutorials/keras/). To learn more about building models with Keras, read the [guides](https://www.tensorflow.org/guide/keras). If you want learn more about loading and preparing data, see the tutorials on [image data loading](https://www.tensorflow.org/tutorials/load_data/images) or [CSV data loading](https://www.tensorflow.org/tutorials/load_data/csv).\n" |
| 380 | + ] |
350 | 381 | }
|
351 | 382 | ],
|
352 | 383 | "metadata": {
|
|
0 commit comments