The tf.keras.datasets
module provide a few toy datasets (already-vectorized, in Numpy format) that can be used for debugging a model or creating simple code examples.
load_data
functiontf.keras.datasets.mnist.load_data(path="mnist.npz")
Loads the MNIST dataset.
This is a dataset of 60,000 28x28 grayscale images of the 10 digits, along with a test set of 10,000 images. More info can be found at the MNIST homepage.
Arguments
~/.keras/datasets
).Returns
Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test)
.
x_train, x_test: uint8 arrays of grayscale image data with shapes (num_samples, 28, 28).
y_train, y_test: uint8 arrays of digit labels (integers in range 0-9) with shapes (num_samples,).
License: Yann LeCun and Corinna Cortes hold the copyright of MNIST dataset, which is a derivative work from original NIST datasets. MNIST dataset is made available under the terms of the Creative Commons Attribution-Share Alike 3.0 license.
load_data
functiontf.keras.datasets.cifar10.load_data()
Loads CIFAR10 dataset.
This is a dataset of 50,000 32x32 color training images and 10,000 test images, labeled over 10 categories. See more info at the CIFAR homepage.
Returns
Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test)
.
x_train, x_test: uint8 arrays of RGB image data with shape (num_samples, 3, 32, 32)
if tf.keras.backend.image_data_format()
is 'channels_first'
, or (num_samples, 32, 32, 3)
if the data format is 'channels_last'
.
y_train, y_test: uint8 arrays of category labels (integers in range 0-9) each with shape (num_samples, 1).
load_data
functiontf.keras.datasets.cifar100.load_data(label_mode="fine")
Loads CIFAR100 dataset.
This is a dataset of 50,000 32x32 color training images and 10,000 test images, labeled over 100 fine-grained classes that are grouped into 20 coarse-grained classes. See more info at the CIFAR homepage.
Arguments
Returns
Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test)
.
x_train, x_test: uint8 arrays of RGB image data with shape (num_samples, 3, 32, 32)
if tf.keras.backend.image_data_format()
is 'channels_first'
, or (num_samples, 32, 32, 3)
if the data format is 'channels_last'
.
y_train, y_test: uint8 arrays of category labels with shape (num_samples, 1).
Raises
label_mode
.load_data
functiontf.keras.datasets.imdb.load_data(
path="imdb.npz",
num_words=None,
skip_top=0,
maxlen=None,
seed=113,
start_char=1,
oov_char=2,
index_from=3,
**kwargs
)
Loads the IMDB dataset.
This is a dataset of 25,000 movies reviews from IMDB, labeled by sentiment (positive/negative). Reviews have been preprocessed, and each review is encoded as a list of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer "3" encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: "only consider the top 10,000 most common words, but eliminate the top 20 most common words".
As a convention, "0" does not stand for a specific word, but instead is used to encode any unknown word.
Arguments
~/.keras/dataset
).num_words
most frequent words are kept. Any less frequent word will appear as oov_char
value in the sequence data. If None, all words are kept. Defaults to None, so all words are kept.oov_char
value in the dataset. Defaults to 0, so no words are skipped.num_words
or skip_top
limits will be replaced with this character.Returns
Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test)
.
x_train, x_test: lists of sequences, which are lists of indexes (integers). If the num_words argument was specific, the maximum possible index value is num_words - 1
. If the maxlen
argument was specified, the largest possible sequence length is maxlen
.
y_train, y_test: lists of integer labels (1 or 0).
Raises
maxlen
is so low that no input sequence could be kept.Note that the 'out of vocabulary' character is only used for words that were present in the training set but are not included because they're not making the num_words
cut here. Words that were not seen in the training set but are in the test set have simply been skipped.
get_word_index
functiontf.keras.datasets.imdb.get_word_index(path="imdb_word_index.json")
Retrieves a dict mapping words to their index in the IMDB dataset.
Arguments
~/.keras/dataset
).Returns
The word index dictionary. Keys are word strings, values are their index.
load_data
functiontf.keras.datasets.reuters.load_data(
path="reuters.npz",
num_words=None,
skip_top=0,
maxlen=None,
test_split=0.2,
seed=113,
start_char=1,
oov_char=2,
index_from=3,
**kwargs
)
Loads the Reuters newswire classification dataset.
This is a dataset of 11,228 newswires from Reuters, labeled over 46 topics.
This was originally generated by parsing and preprocessing the classic Reuters-21578 dataset, but the preprocessing code is no longer packaged with Keras. See this github discussion for more info.
Each newswire is encoded as a list of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer "3" encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: "only consider the top 10,000 most common words, but eliminate the top 20 most common words".
As a convention, "0" does not stand for a specific word, but instead is used to encode any unknown word.
Arguments
~/.keras/dataset
).num_words
most frequent words are kept. Any less frequent word will appear as oov_char
value in the sequence data. If None, all words are kept. Defaults to None, so all words are kept.oov_char
value in the dataset. Defaults to 0, so no words are skipped.num_words
or skip_top
limits will be replaced with this character.Returns
Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test)
.
x_train, x_test: lists of sequences, which are lists of indexes (integers). If the num_words argument was specific, the maximum possible index value is num_words - 1
. If the maxlen
argument was specified, the largest possible sequence length is maxlen
.
y_train, y_test: lists of integer labels (1 or 0).
Note: The 'out of vocabulary' character is only used for words that were present in the training set but are not included because they're not making the num_words
cut here. Words that were not seen in the training set but are in the test set have simply been skipped.
get_word_index
functiontf.keras.datasets.reuters.get_word_index(path="reuters_word_index.json")
Retrieves a dict mapping words to their index in the Reuters dataset.
Arguments
~/.keras/dataset
).Returns
The word index dictionary. Keys are word strings, values are their index.
load_data
functiontf.keras.datasets.fashion_mnist.load_data()
Loads the Fashion-MNIST dataset.
This is a dataset of 60,000 28x28 grayscale images of 10 fashion categories, along with a test set of 10,000 images. This dataset can be used as a drop-in replacement for MNIST. The class labels are:
Label | Description |
---|---|
0 | T-shirt/top |
1 | Trouser |
2 | Pullover |
3 | Dress |
4 | Coat |
5 | Sandal |
6 | Shirt |
7 | Sneaker |
8 | Bag |
9 | Ankle boot |
Returns
Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test)
.
x_train, x_test: uint8 arrays of grayscale image data with shape (num_samples, 28, 28).
y_train, y_test: uint8 arrays of labels (integers in range 0-9) with shape (num_samples,).
License: The copyright for Fashion-MNIST is held by Zalando SE. Fashion-MNIST is licensed under the MIT license.
load_data
functiontf.keras.datasets.boston_housing.load_data(
path="boston_housing.npz", test_split=0.2, seed=113
)
Loads the Boston Housing dataset.
This is a dataset taken from the StatLib library which is maintained at Carnegie Mellon University.
Samples contain 13 attributes of houses at different locations around the Boston suburbs in the late 1970s. Targets are the median values of the houses at a location (in k$).
The attributes themselves are defined in the StatLib website.
Arguments
~/.keras/datasets
).Returns
Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test)
.
x_train, x_test: numpy arrays with shape (num_samples, 13)
containing either the training samples (for x_train), or test samples (for y_train).
y_train, y_test: numpy arrays of shape (num_samples,)
containing the target scalars. The targets are float scalars typically between 10 and 50 that represent the home prices in k$.