Keras Build-in Datasets
RusNOTEs | caorussell | May 19, 2020, 1:45 a.m.

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.

  • MNIST digits classification dataset
  • CIFAR10 small images classification dataset
  • CIFAR100 small images classification dataset
  • IMDB movie review sentiment classification dataset
  • Reuters newswire classification dataset
  • Fashion MNIST dataset, an alternative to MNIST
  • Boston Housing price regression dataset


MNIST digits classification dataset

load_data function

tf.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

  • path: path where to cache the dataset locally (relative to ~/.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.


CIFAR10 small images classification dataset

load_data function

tf.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).


CIFAR100 small images classification dataset

load_data function

tf.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

  • label_mode: one of "fine", "coarse". If it is "fine" the category labels are the fine-grained labels, if it is "coarse" the output labels are the coarse-grained superclasses.

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

  • ValueError: in case of invalid label_mode.

IMDB movie review sentiment classification dataset

load_data function

tf.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

  • path: where to cache the data (relative to ~/.keras/dataset).
  • num_words: integer or None. Words are ranked by how often they occur (in the training set) and only the 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.
  • skip_top: skip the top N most frequently occurring words (which may not be informative). These words will appear as oov_char value in the dataset. Defaults to 0, so no words are skipped.
  • maxlen: int or None. Maximum sequence length. Any longer sequence will be truncated. Defaults to None, which means no truncation.
  • seed: int. Seed for reproducible data shuffling.
  • start_char: int. The start of a sequence will be marked with this character. Defaults to 1 because 0 is usually the padding character.
  • oov_char: int. The out-of-vocabulary character. Words that were cut out because of the num_words or skip_top limits will be replaced with this character.
  • index_from: int. Index actual words with this index and higher.
  • **kwargs: Used for backwards compatibility.

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

  • ValueError: in case 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 function

tf.keras.datasets.imdb.get_word_index(path="imdb_word_index.json")

Retrieves a dict mapping words to their index in the IMDB dataset.

Arguments

  • path: where to cache the data (relative to ~/.keras/dataset).

Returns

The word index dictionary. Keys are word strings, values are their index.


Reuters newswire classification dataset

load_data function

tf.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

  • path: where to cache the data (relative to ~/.keras/dataset).
  • num_words: integer or None. Words are ranked by how often they occur (in the training set) and only the 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.
  • skip_top: skip the top N most frequently occurring words (which may not be informative). These words will appear as oov_char value in the dataset. Defaults to 0, so no words are skipped.
  • maxlen: int or None. Maximum sequence length. Any longer sequence will be truncated. Defaults to None, which means no truncation.
  • test_split: Float between 0 and 1. Fraction of the dataset to be used as test data. Defaults to 0.2, meaning 20% of the dataset is used as test data.
  • seed: int. Seed for reproducible data shuffling.
  • start_char: int. The start of a sequence will be marked with this character. Defaults to 1 because 0 is usually the padding character.
  • oov_char: int. The out-of-vocabulary character. Words that were cut out because of the num_words or skip_top limits will be replaced with this character.
  • index_from: int. Index actual words with this index and higher.
  • **kwargs: Used for backwards compatibility.

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 function

tf.keras.datasets.reuters.get_word_index(path="reuters_word_index.json")

Retrieves a dict mapping words to their index in the Reuters dataset.

Arguments

  • path: where to cache the data (relative to ~/.keras/dataset).

Returns

The word index dictionary. Keys are word strings, values are their index.


Fashion MNIST dataset, an alternative to MNIST

load_data function

tf.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.


Boston Housing price regression dataset

load_data function

tf.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

  • path: path where to cache the dataset locally (relative to ~/.keras/datasets).
  • test_split: fraction of the data to reserve as test set.
  • seed: Random seed for shuffling the data before computing the test split.

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$.