imdb sentiment classification dataset import json import numpy as np from keras apiexport import kerasexport from keras utils fileutils import getfile from keras utils pythonutils import removelongseq kerasexportkeras datasets imdb loaddata def loaddata pathimdb npz numwordsnone skiptop0 maxlennone seed113 startchar1 oovchar2 indexfrom3 kwargs originfolder https storage googleapis comtensorflowtfkerasdatasets path getfile fnamepath originoriginfolder imdb npz filehash noqa e501 69664113be75683a8fe16e3ed0ab59fda8886cb3cd7ada244f7d9544e4676b9f with np loadpath allowpickletrue as f xtrain labelstrain fxtrain fytrain xtest labelstest fxtest fytest rng np random randomstateseed indices np arangelenxtrain rng shuffleindices xtrain xtrainindices labelstrain labelstrainindices indices np arangelenxtest rng shuffleindices xtest xtestindices labelstest labelstestindices if startchar is not none xtrain startchar w indexfrom for w in x for x in xtrain xtest startchar w indexfrom for w in x for x in xtest elif indexfrom xtrain w indexfrom for w in x for x in xtrain xtest w indexfrom for w in x for x in xtest if maxlen xtrain labelstrain removelongseqmaxlen xtrain labelstrain xtest labelstest removelongseqmaxlen xtest labelstest if not xtrain or not xtest raise valueerror after filtering for sequences shorter than maxlen fstrmaxlen no sequence was kept increase maxlen xs xtrain xtest labels np concatenatelabelstrain labelstest if not numwords numwords maxmaxx for x in xs by convention use 2 as oov word reserve indexfrom 3 by default characters 0 padding 1 start 2 oov if oovchar is not none xs w if skiptop w numwords else oovchar for w in x for x in xs else xs w for w in x if skiptop w numwords for x in xs idx lenxtrain xtrain ytrain np arrayxs idx dtypeobject labels idx xtest ytest np arrayxsidx dtypeobject labelsidx return xtrain ytrain xtest ytest kerasexportkeras datasets imdb getwordindex def getwordindexpathimdbwordindex json use the default parameters to keras datasets imdb loaddata retrieve the training sequences retrieve the word index file mapping words to indices reverse the word index to obtain a dict mapping indices to words and add indexfrom to indices to sync with xtrain update invertedwordindex to include startchar and oovchar decode the first sequence in the dataset originfolder https storage googleapis comtensorflowtfkerasdatasets path getfile fnamepath originoriginfolder imdbwordindex json filehashbfafd718b763782e994055a2d397834f with openpath as f return json loadf loads the imdb dataset https ai stanford edu amaas data sentiment 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 args 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 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 when 0 no words are skipped defaults to 0 maxlen int or none maximum sequence length any longer sequence will be truncated none means no truncation defaults to none seed int seed for reproducible data shuffling start_char int the start of a sequence will be marked with this character 0 is usually the padding character defaults to 1 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 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 noqa e501 by convention use 2 as oov word reserve index_from 3 by default characters 0 padding 1 start 2 oov retrieves a dict mapping words to their index in the imdb dataset args path where to cache the data relative to keras dataset returns the word index dictionary keys are word strings values are their index example python 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
import json

import numpy as np

from keras.api_export import keras_export
from keras.utils.file_utils import get_file
from keras.utils.python_utils import remove_long_seq


@keras_export("keras.datasets.imdb.load_data")
def 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,
):
    
    origin_folder = (
        "https://storage.googleapis.com/tensorflow/tf-keras-datasets/"
    )
    path = get_file(
        fname=path,
        origin=origin_folder + "imdb.npz",
        file_hash=(  
            "69664113be75683a8fe16e3ed0ab59fda8886cb3cd7ada244f7d9544e4676b9f"
        ),
    )
    with np.load(path, allow_pickle=True) as f:
        x_train, labels_train = f["x_train"], f["y_train"]
        x_test, labels_test = f["x_test"], f["y_test"]

    rng = np.random.RandomState(seed)
    indices = np.arange(len(x_train))
    rng.shuffle(indices)
    x_train = x_train[indices]
    labels_train = labels_train[indices]

    indices = np.arange(len(x_test))
    rng.shuffle(indices)
    x_test = x_test[indices]
    labels_test = labels_test[indices]

    if start_char is not None:
        x_train = [[start_char] + [w + index_from for w in x] for x in x_train]
        x_test = [[start_char] + [w + index_from for w in x] for x in x_test]
    elif index_from:
        x_train = [[w + index_from for w in x] for x in x_train]
        x_test = [[w + index_from for w in x] for x in x_test]

    if maxlen:
        x_train, labels_train = remove_long_seq(maxlen, x_train, labels_train)
        x_test, labels_test = remove_long_seq(maxlen, x_test, labels_test)
        if not x_train or not x_test:
            raise ValueError(
                "After filtering for sequences shorter than maxlen="
                f"{str(maxlen)}, no sequence was kept. Increase maxlen."
            )

    xs = x_train + x_test
    labels = np.concatenate([labels_train, labels_test])

    if not num_words:
        num_words = max(max(x) for x in xs)

    
    
    
    if oov_char is not None:
        xs = [
            [w if (skip_top <= w < num_words) else oov_char for w in x]
            for x in xs
        ]
    else:
        xs = [[w for w in x if skip_top <= w < num_words] for x in xs]

    idx = len(x_train)
    x_train, y_train = np.array(xs[:idx], dtype="object"), labels[:idx]
    x_test, y_test = np.array(xs[idx:], dtype="object"), labels[idx:]
    return (x_train, y_train), (x_test, y_test)


@keras_export("keras.datasets.imdb.get_word_index")
def get_word_index(path="imdb_word_index.json"):
    
    origin_folder = (
        "https://storage.googleapis.com/tensorflow/tf-keras-datasets/"
    )
    path = get_file(
        fname=path,
        origin=origin_folder + "imdb_word_index.json",
        file_hash="bfafd718b763782e994055a2d397834f",
    )
    with open(path) as f:
        return json.load(f)