#! /usr/bin/env python3 # def imdb_decode ( item = 20 ): #*****************************************************************************80 # ## imdb_decode decodes a movie review from keras's IMDB database. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 12 November 2019 # # Author: # # Original version by Francois Chollet; # This version by John Burkardt. # # Reference: # # Francois Chollet, # Deep Learning with Python, # Manning, 2018, # ISBN: 9781617294433. # # Input: # # integer item: the index of the review to be decoded. # 0 <= item < 25000. # import keras import numpy as np import platform print ( '' ) print ( 'imdb_decode:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' keras version: %s' % ( keras.__version__ ) ) print ( ' Demonstrate how to decode a movie review from the IMDB dataset.' ) # # Import the movie review dataset. # from keras.datasets import imdb # # Load the movie review dataset. # Only take the first 10,000 most frequent words. # ( train_data, train_labels ), ( test_data, test_labels ) = \ imdb.load_data ( num_words = 10000 ) # # As an example, list a data item and its label. # 0 means a negative review, and 1 means a positive review. # print ( '' ) print ( ' Data item #', item ) print ( '' ) print ( train_data[item] ) print ( '' ) print ( ' Label (0=negative, 1=positive):' ) print ( '' ) print ( train_labels[item] ) # # Show how to recover the actual words of a review from the word index. # word_index is a dictionary mapping words to an integer index. # word_index = imdb.get_word_index() # # We create a reverse dictionary, mapping integer indices to words. # reverse_word_index = dict ( [(value, key) for (key, value) in word_index.items()] ) # # We decode the review; note that our indices were offset by 3 # because 0, 1 and 2 are reserved indices for "padding", # "start of sequence", and "unknown". # decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[item]]) print ( '' ) print ( ' Decoded review #', item ) print ( '' ) print ( decoded_review ) # # Terminate. # print ( '' ) print ( 'imdb_decode:' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## TIMESTAMP prints the date as a timestamp. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return None if ( __name__ == '__main__' ): timestamp ( ) imdb_decode ( 10 ) timestamp ( )