#! /usr/bin/env python3 # def newswire ( ): #*****************************************************************************80 # ## newswire uses keras to classify newswires from the Reuters dataset. # # Discussion: # # Each newswire is to be assigned to one of 46 separate classes. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 April 2019 # # Author: # # The original text is by Francois Chollet; # some modifications by John Burkardt. # # Reference: # # Francois Chollet, # Deep Learning with Python, # Manning, 2018, # ISBN: 9781617294433. # import keras import platform print ( '' ) print ( 'newswire:' ) print ( ' python version: %s' % ( platform.python_version ( ) ) ) print ( ' keras version: %s' % ( keras.__version__ ) ) print ( ' Neural network to classify the Reuters newswire data.' ) # # Import and load the dataset. # from keras.datasets import reuters ( train_data, train_labels ), ( test_data, test_labels ) = reuters.load_data ( num_words = 10000 ) # # Display the size of the training and testing items. # print ( '' ) print ( ' Number of training data items = %d' % ( len ( train_data ) ) ) print ( ' Number of test data items = %d' % ( len ( test_data ) ) ) # # Exhibit the contents of one item of the training data: # print ( ' Sample train_data[10]:' ) print ( train_data[10] ) # # Show how to convert the numeric data back into text. # word_index = reuters.get_word_index ( ) reverse_word_index = dict ( [ ( value, key ) for ( key, value ) in word_index.items ( ) ] ) decoded_newswire = ''.join ( [ reverse_word_index.get ( i - 3, '?' ) for i in train_data[0] ] ) # # The label that is associated with a data item is the topic index, between 0 and 45. # print ( ' Sample train_label[10]:' ) print ( train_labels[10] ) # # Vectorize the data. # import numpy as np def vectorize_sequences ( sequences, dimension = 10000 ): results = np.zeros ( ( len ( sequences ), dimension ) ) for i, sequence in enumerate ( sequences ): results[i,sequence] = 1.0 return results x_train = vectorize_sequences ( train_data ) x_test = vectorize_sequences ( test_data ) # # Vectorize the labels using one-hot encoding. # (A vector of 0's with a single 1). # from keras.utils.np_utils import to_categorical one_hot_train_labels = to_categorical ( train_labels ) one_hot_test_labels = to_categorical ( test_labels ) # # Define the network architecture. # from keras import models from keras import layers model = models.Sequential() model.add ( layers.Dense ( 64, activation = 'relu', input_shape = ( 10000, ) ) ) model.add ( layers.Dense ( 64, activation = 'relu' ) ) model.add ( layers.Dense ( 46, activation = 'softmax' ) ) # # Compile the network. # model.compile ( \ optimizer = 'rmsprop', \ loss = 'categorical_crossentropy', \ metrics = [ 'accuracy' ] ) # # Select 1000 data items for validation. # x_val = x_train[:1000] partial_x_train = x_train[1000:] y_val = one_hot_train_labels[:1000] partial_y_train = one_hot_train_labels[1000:] # # Train the network for 20 epochs # history = model.fit ( \ partial_x_train, \ partial_y_train, \ epochs = 20, \ batch_size = 512, \ validation_data = ( x_val, y_val ) ) # # Display the loss curve. # import matplotlib.pyplot as plt loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range ( 1, len ( loss ) + 1 ) plt.plot ( epochs, loss, 'bo', label = 'Training loss' ) plt.plot ( epochs, val_loss, 'b', label = 'Validation loss' ) plt.title ( 'Training and validation loss' ) plt.xlabel ( 'Epochs' ) plt.ylabel ( 'Loss' ) plt.legend ( ) filename = 'newswire_loss.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved in "%s"' % ( filename ) ) plt.show ( ) # # Display the accuracy curve. # plt.clf ( ) acc = history.history['acc'] val_acc = history.history['val_acc'] plt.plot ( epochs, acc, 'bo', label = 'Training accuracy' ) plt.plot ( epochs, val_acc, 'b', label = 'Validation accuracy' ) plt.title ( 'Training and validation accuracy' ) plt.xlabel ( 'Epochs' ) plt.ylabel ( 'Loss' ) plt.legend ( ) filename = 'newswire_accuracy.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved in "%s"' % ( filename ) ) plt.show ( ) # # Terminate. # print ( '' ) print ( 'newswire:' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## TIMESTAMP prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # # Parameters: # # None # import time t = time.time ( ) print ( time.ctime ( t ) ) return None if ( __name__ == '__main__' ): timestamp ( ) newswire ( ) timestamp ( )