CINXE.COM
IMDB movie review sentiment classification dataset
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="Keras documentation"> <meta name="author" content="Keras Team"> <link rel="shortcut icon" href="https://keras.io/img/favicon.ico"> <link rel="canonical" href="https://keras.io/api/datasets/imdb/" /> <!-- Social --> <meta property="og:title" content="Keras documentation: IMDB movie review sentiment classification dataset"> <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> <meta name="twitter:title" content="Keras documentation: IMDB movie review sentiment classification dataset"> <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> <meta name="twitter:card" content="summary"> <title>IMDB movie review sentiment classification dataset</title> <!-- Custom fonts for this template --> <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700;800&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;600;700;800&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400&display=swap" rel="stylesheet"> <!-- Bootstrap core CSS --> <link href="/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom styles for this template --> <link href="/css/docs.css?v=2" rel="stylesheet"> <link href="/css/monokai.css" rel="stylesheet"> <!-- Google Tag Manager --> <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-5DNGF4N'); </script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-175165319-128', 'auto'); ga('send', 'pageview'); </script> <!-- End Google Tag Manager --> <script async defer src="https://buttons.github.io/buttons.js"></script> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> </head> <body> <!-- Google Tag Manager (noscript) --> <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5DNGF4N" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> <!-- End Google Tag Manager (noscript) --> <div class="k-page"> <div class="hidden"> None </div> <nav class="nav__container"> <div class="nav__wrapper"> <div class="nav__controls--mobile"> <button class="nav__menu--button"><i class="icon--menu"></i></button> <button class="nav__menu--close"><i class="icon--close"></i></button> <a href="/"> <img class="nav__logo nav__logo--mobile" src="/img/k-logo.png" /> </a> <button class="nav__search--mobile"> <i class="icon__search--mobile"></i> </button> </div> <form class="nav__search nav__search-form--mobile"> <input class="nav__search--input" type="search" placeholder="SEARCH" aria-label="Search" /> <button class="nav__search--button" type="submit"> <i class="icon--search"></i> </button> </form> <div class="k-nav nav__mobile-menu" id="nav-menu"> <!-- version with just the active item visible --> <div class="nav flex-column nav-pills" role="tablist" aria-orientation="vertical"> <a class="nav-link" href="/getting_started/" role="tab" aria-selected="">Getting started</a> <a class="nav-link" href="/guides/" role="tab" aria-selected="">Developer guides</a> <a class="nav-link" href="/examples/" role="tab" aria-selected="">Code examples</a> <a class="nav-link active" href="/api/" role="tab" aria-selected="">Keras 3 API documentation</a> <a class="nav-sublink" href="/api/models/">Models API</a> <a class="nav-sublink" href="/api/layers/">Layers API</a> <a class="nav-sublink" href="/api/callbacks/">Callbacks API</a> <a class="nav-sublink" href="/api/ops/">Ops API</a> <a class="nav-sublink" href="/api/optimizers/">Optimizers</a> <a class="nav-sublink" href="/api/metrics/">Metrics</a> <a class="nav-sublink" href="/api/losses/">Losses</a> <a class="nav-sublink" href="/api/data_loading/">Data loading</a> <a class="nav-sublink active" href="/api/datasets/">Built-in small datasets</a> <a class="nav-sublink2" href="/api/datasets/mnist/">MNIST digits classification dataset</a> <a class="nav-sublink2" href="/api/datasets/cifar10/">CIFAR10 small images classification dataset</a> <a class="nav-sublink2" href="/api/datasets/cifar100/">CIFAR100 small images classification dataset</a> <a class="nav-sublink2 active" href="/api/datasets/imdb/">IMDB movie review sentiment classification dataset</a> <a class="nav-sublink2" href="/api/datasets/reuters/">Reuters newswire classification dataset</a> <a class="nav-sublink2" href="/api/datasets/fashion_mnist/">Fashion MNIST dataset, an alternative to MNIST</a> <a class="nav-sublink2" href="/api/datasets/california_housing/">California Housing price regression dataset</a> <a class="nav-sublink" href="/api/applications/">Keras Applications</a> <a class="nav-sublink" href="/api/mixed_precision/">Mixed precision</a> <a class="nav-sublink" href="/api/distribution/">Multi-device distribution</a> <a class="nav-sublink" href="/api/random/">RNG API</a> <a class="nav-sublink" href="/api/utils/">Utilities</a> <a class="nav-link" href="/2/api/" role="tab" aria-selected="">Keras 2 API documentation</a> <a class="nav-link" href="/keras_tuner/" role="tab" aria-selected="">KerasTuner: Hyperparam Tuning</a> <a class="nav-link" href="/keras_hub/" role="tab" aria-selected="">KerasHub: Pretrained Models</a> </div> </div> <a href="/"> <img class="nav__logo nav__logo--desktop" src="/img/logo.png" alt="keras.io logo" /> </a> <div class="nav__menu"> <ul class="nav__item--container"> <li class="nav__item"> <a class="nav__link" href="/getting_started/">Get started</a> </li> <li class="nav__item"> <a class="nav__link" href="/guides/">Guides</a> </li> <li class="nav__item"> <a class="nav__link nav__link--active" href="/api/">API Docs</a> </li> <li class="nav__item"> <a class="nav__link" href="/examples/">Examples</a> </li> <li class="nav__item"> <a class="nav__link" href="/keras_tuner/">Keras Tuner</a> </li> <li class="nav__item"> <a class="nav__link" href="/keras_hub/">Keras Hub</a> </li> </ul> <form class="nav__search"> <input class="nav__search--input" type="search" placeholder="SEARCH" aria-label="Search" /> <button class="nav__search--button" type="submit"> <i class="icon--search"></i> </button> </form> </div> </div> </nav> <div class="page__container flex__container"> <div class="nav__side-nav" id="nav-menu"> <div class="nav flex-column nav-pills" role="tablist" aria-orientation="vertical"> <a class="nav-link active" href="/api/" role="tab" aria-selected=""> Keras 3 API documentation </a> <div class="nav-expanded-panel"> <a class="nav-sublink" href="/api/models/">Models API</a> <a class="nav-sublink" href="/api/layers/">Layers API</a> <a class="nav-sublink" href="/api/callbacks/">Callbacks API</a> <a class="nav-sublink" href="/api/ops/">Ops API</a> <a class="nav-sublink" href="/api/optimizers/">Optimizers</a> <a class="nav-sublink" href="/api/metrics/">Metrics</a> <a class="nav-sublink" href="/api/losses/">Losses</a> <a class="nav-sublink" href="/api/data_loading/">Data loading</a> <a class="nav-sublink active" href="/api/datasets/">Built-in small datasets</a> <a class="nav-sublink2" href="/api/datasets/mnist/">MNIST digits classification dataset</a> <a class="nav-sublink2" href="/api/datasets/cifar10/">CIFAR10 small images classification dataset</a> <a class="nav-sublink2" href="/api/datasets/cifar100/">CIFAR100 small images classification dataset</a> <a class="nav-sublink2 active" href="/api/datasets/imdb/">IMDB movie review sentiment classification dataset</a> <a class="nav-sublink2" href="/api/datasets/reuters/">Reuters newswire classification dataset</a> <a class="nav-sublink2" href="/api/datasets/fashion_mnist/">Fashion MNIST dataset, an alternative to MNIST</a> <a class="nav-sublink2" href="/api/datasets/california_housing/">California Housing price regression dataset</a> <a class="nav-sublink" href="/api/applications/">Keras Applications</a> <a class="nav-sublink" href="/api/mixed_precision/">Mixed precision</a> <a class="nav-sublink" href="/api/distribution/">Multi-device distribution</a> <a class="nav-sublink" href="/api/random/">RNG API</a> <a class="nav-sublink" href="/api/utils/">Utilities</a> </div> <a class="nav-link" href="/2/api/" role="tab" aria-selected=""> Keras 2 API documentation </a> </div> </div> <div class="k-main"> <div class='k-main-inner' id='k-main-id'> <div class='k-content'> <div class='k-location-slug'> <span class="k-location-slug-pointer">►</span> <a href='/api/'>Keras 3 API documentation</a> / <a href='/api/datasets/'>Built-in small datasets</a> / IMDB movie review sentiment classification dataset </div> <h1 id="imdb-movie-review-sentiment-classification-dataset">IMDB movie review sentiment classification dataset</h1> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.9.1/keras/src/datasets/imdb.py#L12">[source]</a></span></p> <h3 id="loaddata-function"><code>load_data</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">imdb</span><span class="o">.</span><span class="n">load_data</span><span class="p">(</span> <span class="n">path</span><span class="o">=</span><span class="s2">"imdb.npz"</span><span class="p">,</span> <span class="n">num_words</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">skip_top</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">maxlen</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="mi">113</span><span class="p">,</span> <span class="n">start_char</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">oov_char</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">index_from</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span> <span class="p">)</span> </code></pre></div> <p>Loads the <a href="https://ai.stanford.edu/~amaas/data/sentiment/">IMDB dataset</a>.</p> <p>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".</p> <p>As a convention, "0" does not stand for a specific word, but instead is used to encode the pad token.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>path</strong>: where to cache the data (relative to <code>~/.keras/dataset</code>).</li> <li><strong>num_words</strong>: integer or None. Words are ranked by how often they occur (in the training set) and only the <code>num_words</code> most frequent words are kept. Any less frequent word will appear as <code>oov_char</code> value in the sequence data. If None, all words are kept. Defaults to <code>None</code>.</li> <li><strong>skip_top</strong>: skip the top N most frequently occurring words (which may not be informative). These words will appear as <code>oov_char</code> value in the dataset. When 0, no words are skipped. Defaults to <code>0</code>.</li> <li><strong>maxlen</strong>: int or None. Maximum sequence length. Any longer sequence will be truncated. None, means no truncation. Defaults to <code>None</code>.</li> <li><strong>seed</strong>: int. Seed for reproducible data shuffling.</li> <li><strong>start_char</strong>: int. The start of a sequence will be marked with this character. 0 is usually the padding character. Defaults to <code>1</code>.</li> <li><strong>oov_char</strong>: int. The out-of-vocabulary character. Words that were cut out because of the <code>num_words</code> or <code>skip_top</code> limits will be replaced with this character.</li> <li><strong>index_from</strong>: int. Index actual words with this index and higher.</li> </ul> <p><strong>Returns</strong></p> <ul> <li><strong>Tuple of Numpy arrays</strong>: <code>(x_train, y_train), (x_test, y_test)</code>.</li> </ul> <p><strong><code>x_train</code>, <code>x_test</code></strong>: lists of sequences, which are lists of indexes (integers). If the num_words argument was specific, the maximum possible index value is <code>num_words - 1</code>. If the <code>maxlen</code> argument was specified, the largest possible sequence length is <code>maxlen</code>.</p> <p><strong><code>y_train</code>, <code>y_test</code></strong>: lists of integer labels (1 or 0).</p> <p><strong>Note</strong>: 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 <code>num_words</code> cut here. Words that were not seen in the training set but are in the test set have simply been skipped.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.9.1/keras/src/datasets/imdb.py#L143">[source]</a></span></p> <h3 id="getwordindex-function"><code>get_word_index</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">imdb</span><span class="o">.</span><span class="n">get_word_index</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="s2">"imdb_word_index.json"</span><span class="p">)</span> </code></pre></div> <p>Retrieves a dict mapping words to their index in the IMDB dataset.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>path</strong>: where to cache the data (relative to <code>~/.keras/dataset</code>).</li> </ul> <p><strong>Returns</strong></p> <p>The word index dictionary. Keys are word strings, values are their index.</p> <p><strong>Example</strong></p> <div class="codehilite"><pre><span></span><code><span class="c1"># Use the default parameters to keras.datasets.imdb.load_data</span> <span class="n">start_char</span> <span class="o">=</span> <span class="mi">1</span> <span class="n">oov_char</span> <span class="o">=</span> <span class="mi">2</span> <span class="n">index_from</span> <span class="o">=</span> <span class="mi">3</span> <span class="c1"># Retrieve the training sequences.</span> <span class="p">(</span><span class="n">x_train</span><span class="p">,</span> <span class="n">_</span><span class="p">),</span> <span class="n">_</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">imdb</span><span class="o">.</span><span class="n">load_data</span><span class="p">(</span> <span class="n">start_char</span><span class="o">=</span><span class="n">start_char</span><span class="p">,</span> <span class="n">oov_char</span><span class="o">=</span><span class="n">oov_char</span><span class="p">,</span> <span class="n">index_from</span><span class="o">=</span><span class="n">index_from</span> <span class="p">)</span> <span class="c1"># Retrieve the word index file mapping words to indices</span> <span class="n">word_index</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">imdb</span><span class="o">.</span><span class="n">get_word_index</span><span class="p">()</span> <span class="c1"># Reverse the word index to obtain a dict mapping indices to words</span> <span class="c1"># And add `index_from` to indices to sync with `x_train`</span> <span class="n">inverted_word_index</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="n">index_from</span><span class="p">,</span> <span class="n">word</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">word</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="ow">in</span> <span class="n">word_index</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="p">)</span> <span class="c1"># Update `inverted_word_index` to include `start_char` and `oov_char`</span> <span class="n">inverted_word_index</span><span class="p">[</span><span class="n">start_char</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"[START]"</span> <span class="n">inverted_word_index</span><span class="p">[</span><span class="n">oov_char</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"[OOV]"</span> <span class="c1"># Decode the first sequence in the dataset</span> <span class="n">decoded_sequence</span> <span class="o">=</span> <span class="s2">" "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">inverted_word_index</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">x_train</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> </code></pre></div> <hr /> </div> <div class='k-outline'> <div class='k-outline-depth-1'> <a href='#imdb-movie-review-sentiment-classification-dataset'>IMDB movie review sentiment classification dataset</a> </div> <div class='k-outline-depth-3'> <a href='#loaddata-function'><code>load_data</code> function</a> </div> <div class='k-outline-depth-3'> <a href='#getwordindex-function'><code>get_word_index</code> function</a> </div> </div> </div> </div> </div> </div> <footer> <div class="footer__container"> <a href="https://policies.google.com/terms">Terms</a> <div>|</div> <a href="https://policies.google.com/privacy">Privacy</a> </div> </footer> <script src="/js/index.js"></script> </body> </html>