{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "pixDvex9KBqt" }, "source": [ "##### Copyright 2019 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "K16pBM8mKK7a" }, "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": "TfRdquslKbO3" }, "source": [ "# Load NumPy data" ] }, { "cell_type": "markdown", "metadata": { "id": "-uq3F0ggKlZb" }, "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": "-0tqX8qkXZEj" }, "source": [ "This tutorial provides an example of loading data from NumPy arrays into a `tf.data.Dataset`.\n", "\n", "This example loads the MNIST dataset from a `.npz` file. However, the source of the NumPy arrays is not important.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "-Ze5IBx9clLB" }, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "k6J3JzK5NxQ6" }, "outputs": [], "source": [ " \n", "import numpy as np\n", "import tensorflow as tf" ] }, { "cell_type": "markdown", "metadata": { "id": "G0yWiN8-cpDb" }, "source": [ "### Load from `.npz` file" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "GLHNrFM6RWoM" }, "outputs": [], "source": [ "DATA_URL = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz'\n", "\n", "path = tf.keras.utils.get_file('mnist.npz', DATA_URL)\n", "with np.load(path) as data:\n", " train_examples = data['x_train']\n", " train_labels = data['y_train']\n", " test_examples = data['x_test']\n", " test_labels = data['y_test']" ] }, { "cell_type": "markdown", "metadata": { "id": "cCeCkvrDgCMM" }, "source": [ "## Load NumPy arrays with `tf.data.Dataset`" ] }, { "cell_type": "markdown", "metadata": { "id": "tslB0tJPgB-2" }, "source": [ "Assuming you have an array of examples and a corresponding array of labels, pass the two arrays as a tuple into `tf.data.Dataset.from_tensor_slices` to create a `tf.data.Dataset`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "QN_8wwc5R7Qm" }, "outputs": [], "source": [ "train_dataset = tf.data.Dataset.from_tensor_slices((train_examples, train_labels))\n", "test_dataset = tf.data.Dataset.from_tensor_slices((test_examples, test_labels))" ] }, { "cell_type": "markdown", "metadata": { "id": "6Rco85bbkDfN" }, "source": [ "## Use the datasets" ] }, { "cell_type": "markdown", "metadata": { "id": "0dvl1uUukc4K" }, "source": [ "### Shuffle and batch the datasets" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "GTXdRMPcSXZj" }, "outputs": [], "source": [ "BATCH_SIZE = 64\n", "SHUFFLE_BUFFER_SIZE = 100\n", "\n", "train_dataset = train_dataset.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)\n", "test_dataset = test_dataset.batch(BATCH_SIZE)" ] }, { "cell_type": "markdown", "metadata": { "id": "w69Jl8k6lilg" }, "source": [ "### Build and train a model" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Uhxr8py4DkDN" }, "outputs": [], "source": [ "model = tf.keras.Sequential([\n", " tf.keras.layers.Flatten(input_shape=(28, 28)),\n", " tf.keras.layers.Dense(128, activation='relu'),\n", " tf.keras.layers.Dense(10)\n", "])\n", "\n", "model.compile(optimizer=tf.keras.optimizers.RMSprop(),\n", " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", " metrics=['sparse_categorical_accuracy'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "XLDzlPGgOHBx" }, "outputs": [], "source": [ "model.fit(train_dataset, epochs=10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "2q82yN8mmKIE" }, "outputs": [], "source": [ "model.evaluate(test_dataset)" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "numpy.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }