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 the pad token.
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
.oov_char
value in the dataset. When 0, no words are
skipped. Defaults to 0
.None
.1
.num_words
or
skip_top
limits will be replaced with this character.Returns
(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.
Example
# Use the default parameters to keras.datasets.imdb.load_data
start_char = 1
oov_char = 2
index_from = 3
# Retrieve the training sequences.
(x_train, _), _ = keras.datasets.imdb.load_data(
start_char=start_char, oov_char=oov_char, index_from=index_from
)
# Retrieve the word index file mapping words to indices
word_index = keras.datasets.imdb.get_word_index()
# Reverse the word index to obtain a dict mapping indices to words
# And add `index_from` to indices to sync with `x_train`
inverted_word_index = dict(
(i + index_from, word) for (word, i) in word_index.items()
)
# Update `inverted_word_index` to include `start_char` and `oov_char`
inverted_word_index[start_char] = "[START]"
inverted_word_index[oov_char] = "[OOV]"
# Decode the first sequence in the dataset
decoded_sequence = " ".join(inverted_word_index[i] for i in x_train[0])