Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/datasets/mnist.py: 50%

12 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

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 keras.src.utils.data_utils import get_file 

20 

21# isort: off 

22from tensorflow.python.util.tf_export import keras_export 

23 

24 

25@keras_export("keras.datasets.mnist.load_data") 

26def load_data(path="mnist.npz"): 

27 """Loads the MNIST dataset. 

28 

29 This is a dataset of 60,000 28x28 grayscale images of the 10 digits, 

30 along with a test set of 10,000 images. 

31 More info can be found at the 

32 [MNIST homepage](http://yann.lecun.com/exdb/mnist/). 

33 

34 Args: 

35 path: path where to cache the dataset locally 

36 (relative to `~/.keras/datasets`). 

37 

38 Returns: 

39 Tuple of NumPy arrays: `(x_train, y_train), (x_test, y_test)`. 

40 

41 **x_train**: uint8 NumPy array of grayscale image data with shapes 

42 `(60000, 28, 28)`, containing the training data. Pixel values range 

43 from 0 to 255. 

44 

45 **y_train**: uint8 NumPy array of digit labels (integers in range 0-9) 

46 with shape `(60000,)` for the training data. 

47 

48 **x_test**: uint8 NumPy array of grayscale image data with shapes 

49 (10000, 28, 28), containing the test data. Pixel values range 

50 from 0 to 255. 

51 

52 **y_test**: uint8 NumPy array of digit labels (integers in range 0-9) 

53 with shape `(10000,)` for the test data. 

54 

55 Example: 

56 

57 ```python 

58 (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() 

59 assert x_train.shape == (60000, 28, 28) 

60 assert x_test.shape == (10000, 28, 28) 

61 assert y_train.shape == (60000,) 

62 assert y_test.shape == (10000,) 

63 ``` 

64 

65 License: 

66 Yann LeCun and Corinna Cortes hold the copyright of MNIST dataset, 

67 which is a derivative work from original NIST datasets. 

68 MNIST dataset is made available under the terms of the 

69 [Creative Commons Attribution-Share Alike 3.0 license.]( 

70 https://creativecommons.org/licenses/by-sa/3.0/) 

71 """ 

72 origin_folder = ( 

73 "https://storage.googleapis.com/tensorflow/tf-keras-datasets/" 

74 ) 

75 path = get_file( 

76 path, 

77 origin=origin_folder + "mnist.npz", 

78 file_hash=( # noqa: E501 

79 "731c5ac602752760c8e48fbffcf8c3b850d9dc2a2aedcf2cc48468fc17b673d1" 

80 ), 

81 ) 

82 with np.load(path, allow_pickle=True) as f: 

83 x_train, y_train = f["x_train"], f["y_train"] 

84 x_test, y_test = f["x_test"], f["y_test"] 

85 

86 return (x_train, y_train), (x_test, y_test) 

87