#! /usr/bin/env python3 # def mnist ( ): #*****************************************************************************80 # ## mnist uses keras to classify the MNIST digit data. # # Discussion: # # This script is a very simple example that demonstrates the use of keras. # # The MNIST digit dataset, which is built in to keras, is used. # # Licensing: # # This code is distributed under the GNU LGPL 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 ( 'mnist:' ) print ( ' python version: %s' % ( platform.python_version ( ) ) ) print ( ' keras version: %s' % ( keras.__version__ ) ) print ( ' Neural network to classify the MNIST digit image data.' ) # # Import and load the dataset. # from keras.datasets import mnist ( train_images, train_labels ), ( test_images, test_labels ) = mnist.load_data() # # Display the shape, size, and type of the training and testing items. # print ( '' ) print ( ' Number of training items = %d' % ( len ( train_labels ) ) ) print ( ' Training data shape = (%d,%d,%d)' % ( train_images.shape ) ) print ( ' Sample training labels:' ) print ( train_labels ) print ( '' ) print ( ' Number of testing items = %d' % ( len ( test_labels ) ) ) print ( ' Training data shape = (%d,%d,%d)' % ( train_images.shape ) ) print ( ' Sample testing labels:' ) print ( test_labels ) print ( '' ) # # Define the network architecture. # from keras import models from keras import layers network = models.Sequential() network.add ( layers.Dense ( 512, activation = 'relu', input_shape = ( 28 * 28, ) ) ) network.add ( layers.Dense ( 10, activation = 'softmax' ) ) # # Compile the network. # network.compile ( \ optimizer = 'rmsprop', \ loss = 'categorical_crossentropy', \ metrics = [ 'accuracy' ] ) # # Convert the training data from uint8 arrays of shape (60000,28,28) to # float32 arrays of shape (60000,28*28), and similarly for the test data. # train_images = train_images.reshape ( ( 60000, 28 * 28 ) ) train_images = train_images.astype ( 'float32' ) / 255 test_images = test_images.reshape ( ( 10000, 28 * 28 ) ) test_images = test_images.astype ( 'float32' ) / 255 # # Convert the labels from numeric values to categorical values. # from keras.utils import to_categorical train_labels = to_categorical ( train_labels ) test_labels = to_categorical ( test_labels ) # # Train the network. # network.fit ( train_images, train_labels, epochs = 5, batch_size = 128 ) # # Test the network. # test_loss, test_acc = network.evaluate ( test_images, test_labels ) # # Print the test accuracy. # print ( '' ) print ( 'test_loss: %g' % ( test_loss ) ) print ( 'test_acc: %g' % ( test_acc ) ) # # Terminate. # print ( '' ) print ( 'mnist:' ) 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 # # Parameters: # # None # import time t = time.time ( ) print ( time.ctime ( t ) ) return None if ( __name__ == '__main__' ): timestamp ( ) mnist ( ) timestamp ( )