{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "rX8mhOLljYeM" }, "source": [ "##### Copyright 2019 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "BZSlp3DAjdYf" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "3wF5wszaj97Y" }, "source": [ "# TensorFlow 2 quickstart for beginners" ] }, { "cell_type": "markdown", "metadata": { "id": "DUNzJc4jTj6G" }, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " View on TensorFlow.org\n", " \n", " Run in Google Colab\n", " \n", " View source on GitHub\n", " \n", " Download notebook\n", "
" ] }, { "cell_type": "markdown", "metadata": { "id": "04QgGZc9bF5D" }, "source": [ "This short introduction uses [Keras](https://www.tensorflow.org/guide/keras/overview) to:\n", "\n", "1. Load a prebuilt dataset.\n", "1. Build a neural network machine learning model that classifies images.\n", "2. Train this neural network.\n", "3. Evaluate the accuracy of the model." ] }, { "cell_type": "markdown", "metadata": { "id": "hiH7AC-NTniF" }, "source": [ "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", "\n", "1. In Colab, connect to a Python runtime: At the top-right of the menu bar, select *CONNECT*.\n", "2. To run all the code in the notebook, select **Runtime** > **Run all**. To run the code cells one at a time, hover over each cell and select the **Run cell** icon.\n", "\n", "![Run cell icon](images/beginner/run_cell_icon.png)" ] }, { "cell_type": "markdown", "metadata": { "id": "nnrWf3PCEzXL" }, "source": [ "## Set up TensorFlow\n", "\n", "Import TensorFlow into your program to get started:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "0trJmd6DjqBZ" }, "outputs": [], "source": [ "import tensorflow as tf\n", "print(\"TensorFlow version:\", tf.__version__)" ] }, { "cell_type": "markdown", "metadata": { "id": "7NAbSZiaoJ4z" }, "source": [ "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", "\n", "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", "\n", "## Load a dataset\n", "\n", "Load and prepare the MNIST dataset. The pixel values of the images range from 0 through 255. Scale these values to a range of 0 to 1 by dividing the values by `255.0`. This also converts the sample data from integers to floating-point numbers:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "7FP5258xjs-v" }, "outputs": [], "source": [ "mnist = tf.keras.datasets.mnist\n", "\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "x_train, x_test = x_train / 255.0, x_test / 255.0" ] }, { "cell_type": "markdown", "metadata": { "id": "BPZ68wASog_I" }, "source": [ "## Build a machine learning model\n", "\n", "Build a `tf.keras.Sequential` model:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "h3IKyzTCDNGo" }, "outputs": [], "source": [ "model = tf.keras.models.Sequential([\n", " tf.keras.layers.Flatten(input_shape=(28, 28)),\n", " tf.keras.layers.Dense(128, activation='relu'),\n", " tf.keras.layers.Dropout(0.2),\n", " tf.keras.layers.Dense(10)\n", "])" ] }, { "cell_type": "markdown", "metadata": { "id": "l2hiez2eIUz8" }, "source": [ "[`Sequential`](https://www.tensorflow.org/guide/keras/sequential_model) is useful for stacking layers where each layer has one input [tensor](https://www.tensorflow.org/guide/tensor) and one output tensor. Layers are functions with a known mathematical structure that can be reused and have trainable variables. Most TensorFlow models are composed of layers. This model uses the [`Flatten`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Flatten), [`Dense`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense), and [`Dropout`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dropout) layers.\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "OeOrNdnkEEcR" }, "outputs": [], "source": [ "predictions = model(x_train[:1]).numpy()\n", "predictions" ] }, { "cell_type": "markdown", "metadata": { "id": "tgjhDQGcIniO" }, "source": [ "The `tf.nn.softmax` function converts these logits to *probabilities* for each class: " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "zWSRnQ0WI5eq" }, "outputs": [], "source": [ "tf.nn.softmax(predictions).numpy()" ] }, { "cell_type": "markdown", "metadata": { "id": "he5u_okAYS4a" }, "source": [ "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. " ] }, { "cell_type": "markdown", "metadata": { "id": "hQyugpgRIyrA" }, "source": [ "Define a loss function for training using `losses.SparseCategoricalCrossentropy`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "RSkzdv8MD0tT" }, "outputs": [], "source": [ "loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)" ] }, { "cell_type": "markdown", "metadata": { "id": "SfR4MsSDU880" }, "source": [ "The loss function takes a vector of ground truth values and a vector of logits and returns a scalar loss for each example. 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", "\n", "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`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "NJWqEVrrJ7ZB" }, "outputs": [], "source": [ "loss_fn(y_train[:1], predictions).numpy()" ] }, { "cell_type": "markdown", "metadata": { "id": "ada44eb947d4" }, "source": [ "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`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "9foNKHzTD2Vo" }, "outputs": [], "source": [ "model.compile(optimizer='adam',\n", " loss=loss_fn,\n", " metrics=['accuracy'])" ] }, { "cell_type": "markdown", "metadata": { "id": "ix4mEL65on-w" }, "source": [ "## Train and evaluate your model\n", "\n", "Use the `Model.fit` method to adjust your model parameters and minimize the loss: " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "y7suUbJXVLqP" }, "outputs": [], "source": [ "model.fit(x_train, y_train, epochs=5)" ] }, { "cell_type": "markdown", "metadata": { "id": "4mDAAPFqVVgn" }, "source": [ "The `Model.evaluate` method checks the model's performance, usually on a [validation set](https://developers.google.com/machine-learning/glossary#validation-set) or [test set](https://developers.google.com/machine-learning/glossary#test-set)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "F7dTAzgHDUh7" }, "outputs": [], "source": [ "model.evaluate(x_test, y_test, verbose=2)" ] }, { "cell_type": "markdown", "metadata": { "id": "T4JfEh7kvx6m" }, "source": [ "The image classifier is now trained to ~98% accuracy on this dataset. To learn more, read the [TensorFlow tutorials](https://www.tensorflow.org/tutorials/)." ] }, { "cell_type": "markdown", "metadata": { "id": "Aj8NrlzlJqDG" }, "source": [ "If you want your model to return a probability, you can wrap the trained model, and attach the softmax to it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "rYb6DrEH0GMv" }, "outputs": [], "source": [ "probability_model = tf.keras.Sequential([\n", " model,\n", " tf.keras.layers.Softmax()\n", "])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "cnqOZtUp1YR_" }, "outputs": [], "source": [ "probability_model(x_test[:5])" ] }, { "cell_type": "markdown", "metadata": { "id": "-47O6_GLdRuT" }, "source": [ "## Conclusion\n", "\n", "Congratulations! You have trained a machine learning model using a prebuilt dataset using the [Keras](https://www.tensorflow.org/guide/keras/overview) API.\n", "\n", "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" ] } ], "metadata": { "colab": { "name": "beginner.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }