{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "_index.ipynb", "version": "0.3.2", "provenance": [], "private_outputs": true, "collapsed_sections": [], "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "rX8mhOLljYeM" }, "source": [ "##### Copyright 2018 The TensorFlow Authors." ] }, { "cell_type": "code", "metadata": { "cellView": "form", "colab_type": "code", "id": "BZSlp3DAjdYf", "colab": {} }, "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." ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "3wF5wszaj97Y" }, "source": [ "# Get Started with TensorFlow 1.x" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "DUNzJc4jTj6G" }, "source": [ "\n", " \n", " \n", "
\n", " Run in Google Colab\n", " \n", " View source on GitHub\n", "
" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "hiH7AC-NTniF" }, "source": [ "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 run the Colab notebook:\n", "\n", "1. Connect to a Python runtime: At the top-right of the menu bar, select *CONNECT*.\n", "2. Run all the notebook code cells: Select *Runtime* > *Run all*.\n", "\n", "For more examples and guides (including details for this program), see [Get Started with TensorFlow](https://www.tensorflow.org/get_started/).\n", "\n", "Let's get started, import the TensorFlow library into your program:" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "0trJmd6DjqBZ", "colab": {} }, "source": [ "from __future__ import absolute_import, division, print_function, unicode_literals\n", "import tensorflow as tf" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "7NAbSZiaoJ4z" }, "source": [ "Load and prepare the [MNIST](http://yann.lecun.com/exdb/mnist/) dataset. Convert the samples from integers to floating-point numbers:" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "7FP5258xjs-v", "colab": {} }, "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" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "BPZ68wASog_I" }, "source": [ "Build the `tf.keras` model by stacking layers. Select an optimizer and loss function used for training:" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "h3IKyzTCDNGo", "colab": {} }, "source": [ "model = tf.keras.models.Sequential([\n", " tf.keras.layers.Flatten(input_shape=(28, 28)),\n", " tf.keras.layers.Dense(512, activation=tf.nn.relu),\n", " tf.keras.layers.Dropout(0.2),\n", " tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n", "])\n", "\n", "model.compile(optimizer='adam',\n", " loss='sparse_categorical_crossentropy',\n", " metrics=['accuracy'])" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ix4mEL65on-w" }, "source": [ "Train and evaluate model:" ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "F7dTAzgHDUh7", "colab": {} }, "source": [ "model.fit(x_train, y_train, epochs=5)\n", "\n", "model.evaluate(x_test, y_test, verbose=2)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "T4JfEh7kvx6m" }, "source": [ "You’ve now trained an image classifier with ~98% accuracy on this dataset. See [Get Started with TensorFlow](https://www.tensorflow.org/get_started/) to learn more." ] } ] }