1# Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""MNIST handwritten digits dataset.""" 16 17import numpy as np 18 19from tensorflow.python.keras.utils.data_utils import get_file 20from tensorflow.python.util.tf_export import keras_export 21 22 23@keras_export('keras.datasets.mnist.load_data') 24def load_data(path='mnist.npz'): 25 """Loads the MNIST dataset. 26 27 This is a dataset of 60,000 28x28 grayscale images of the 10 digits, 28 along with a test set of 10,000 images. 29 More info can be found at the 30 [MNIST homepage](http://yann.lecun.com/exdb/mnist/). 31 32 Args: 33 path: path where to cache the dataset locally 34 (relative to `~/.keras/datasets`). 35 36 Returns: 37 Tuple of NumPy arrays: `(x_train, y_train), (x_test, y_test)`. 38 39 **x_train**: uint8 NumPy array of grayscale image data with shapes 40 `(60000, 28, 28)`, containing the training data. Pixel values range 41 from 0 to 255. 42 43 **y_train**: uint8 NumPy array of digit labels (integers in range 0-9) 44 with shape `(60000,)` for the training data. 45 46 **x_test**: uint8 NumPy array of grayscale image data with shapes 47 (10000, 28, 28), containing the test data. Pixel values range 48 from 0 to 255. 49 50 **y_test**: uint8 NumPy array of digit labels (integers in range 0-9) 51 with shape `(10000,)` for the test data. 52 53 Example: 54 55 ```python 56 (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() 57 assert x_train.shape == (60000, 28, 28) 58 assert x_test.shape == (10000, 28, 28) 59 assert y_train.shape == (60000,) 60 assert y_test.shape == (10000,) 61 ``` 62 63 License: 64 Yann LeCun and Corinna Cortes hold the copyright of MNIST dataset, 65 which is a derivative work from original NIST datasets. 66 MNIST dataset is made available under the terms of the 67 [Creative Commons Attribution-Share Alike 3.0 license.]( 68 https://creativecommons.org/licenses/by-sa/3.0/) 69 """ 70 origin_folder = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/' 71 path = get_file( 72 path, 73 origin=origin_folder + 'mnist.npz', 74 file_hash= 75 '731c5ac602752760c8e48fbffcf8c3b850d9dc2a2aedcf2cc48468fc17b673d1') 76 with np.load(path, allow_pickle=True) as f: # pylint: disable=unexpected-keyword-arg 77 x_train, y_train = f['x_train'], f['y_train'] 78 x_test, y_test = f['x_test'], f['y_test'] 79 80 return (x_train, y_train), (x_test, y_test) 81