{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "_index.ipynb", "version": "0.3.2", "views": {}, "default_view": {}, "provenance": [] } }, "cells": [ { "metadata": { "id": "rX8mhOLljYeM", "colab_type": "text" }, "cell_type": "markdown", "source": [ "##### Copyright 2018 The TensorFlow Authors.\n", "\n", "Licensed under the Apache License, Version 2.0 (the \"License\");" ] }, { "metadata": { "id": "BZSlp3DAjdYf", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } }, "cellView": "form" }, "cell_type": "code", "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": [] }, { "metadata": { "id": "3wF5wszaj97Y", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# Get Started with TensorFlow" ] }, { "metadata": { "id": "DUNzJc4jTj6G", "colab_type": "text" }, "cell_type": "markdown", "source": [ "\n", " \n", " \n", " \n", "
\n", " View on TensorFlow.org\n", " \n", " Run in Google Colab\n", " \n", " View source on GitHub\n", "
" ] }, { "metadata": { "id": "hiH7AC-NTniF", "colab_type": "text" }, "cell_type": "markdown", "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:" ] }, { "metadata": { "id": "0trJmd6DjqBZ", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "import tensorflow as tf" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "7NAbSZiaoJ4z", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Load and prepare the [MNIST](http://yann.lecun.com/exdb/mnist/) dataset. Convert the samples from integers to floating-point numbers:" ] }, { "metadata": { "id": "7FP5258xjs-v", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "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": [] }, { "metadata": { "id": "BPZ68wASog_I", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Build the `tf.keras` model by stacking layers. Select an optimizer and loss function used for training:" ] }, { "metadata": { "id": "h3IKyzTCDNGo", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "model = tf.keras.models.Sequential([\n", " tf.keras.layers.Flatten(),\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": [] }, { "metadata": { "id": "ix4mEL65on-w", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Train and evaluate model:" ] }, { "metadata": { "id": "F7dTAzgHDUh7", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "model.fit(x_train, y_train, epochs=5)\n", "\n", "model.evaluate(x_test, y_test)" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "T4JfEh7kvx6m", "colab_type": "text" }, "cell_type": "markdown", "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." ] } ] }