#! /usr/bin/env python3 # def boston_housing_external ( ): #*****************************************************************************80 # ## boston_housing_external uses keras to analyze Boston housing data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 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 numpy as np import platform print ( '' ) print ( 'boston_housing_external:' ) print ( ' python version: %s' % ( platform.python_version ( ) ) ) print ( ' keras version: %s' % ( keras.__version__ ) ) print ( ' Neural network to classify the Boston housing data.' ) print ( ' The data is read from external files, not internal datasets.' ) # # Load the datasets from external files. # # Here is the statement that would load the data from the built in dataset: # # ( train_data, train_targets ), ( test_data, test_targets ) = boston_housing.load_data ( ) # data = np.loadtxt ( 'boston_housing.txt' ) data = data[:,1:] print ( data.shape ) train_index = np.loadtxt ( 'boston_housing_train_index.txt', dtype = 'int' ) train_index = train_index - 1 test_index = np.loadtxt ( 'boston_housing_test_index.txt', dtype = 'int' ) test_index = test_index - 1 # # Set up the training and test targets (selling prices) # targets = data[:,13] train_targets = targets[train_index] print ( train_targets.shape ) test_targets = targets[test_index] print ( test_targets.shape ) # # Set up the training and test data. # train_data = data[train_index,:] print ( train_data.shape ) test_data = data[test_index,:] print ( test_data.shape ) # # The rest of the program is the same as "boston_housing.py"... # # Exhibit the contents of one item of the training data: # print ( train_targets[0:3], train_targets[-3:] ) # # Normalize the data. # mean = train_data.mean ( axis = 0 ) train_data = train_data - mean std = train_data.std ( axis = 0 ) train_data = train_data / std test_data = test_data - mean test_data = test_data / std # # Define the network architecture. # # Because we are going to instantiate the same model several times, we # implement it as a function. # from keras import models from keras import layers def build_model ( ): model = models.Sequential() model.add ( layers.Dense ( 64, activation = 'relu', input_shape = ( train_data.shape[1], ) ) ) model.add ( layers.Dense ( 64, activation = 'relu' ) ) model.add ( layers.Dense ( 1 ) ) model.compile ( \ optimizer = 'rmsprop', \ loss = 'mse', \ metrics = [ 'mae' ] ) return model # # Do K-fold validation # k = 4 num_val_samples = len ( train_data ) // k num_epochs = 100 all_scores = [] for i in range ( k ): print ( ' Processing fold #', i ) val_data = train_data[i*num_val_samples:(i+1)*num_val_samples] val_targets = train_targets[i*num_val_samples:(i+1)*num_val_samples] partial_train_data = np.concatenate ( \ [ train_data[:i*num_val_samples], \ train_data[:(i+1)*num_val_samples:]], \ axis = 0 ) partial_train_targets = np.concatenate ( \ [ train_targets[:i*num_val_samples], \ train_targets[:(i+1)*num_val_samples:]], \ axis = 0 ) model = build_model ( ) model.fit ( \ partial_train_data, \ partial_train_targets, \ epochs = num_epochs, \ batch_size = 1, \ verbose = 0 ) val_mse, val_mae = model.evaluate ( val_data, val_targets, verbose = 0 ) all_scores.append ( val_mae ) print ( all_scores ) print ( np.mean ( all_scores ) ) # # Save the validation logs at each fold. # num_epochs = 500 all_mae_histories = [] for i in range ( k ): print ( ' Processing fold #', i ) val_data = train_data[i*num_val_samples:(i+1)*num_val_samples] val_targets = train_targets[i*num_val_samples:(i+1)*num_val_samples] partial_train_data = np.concatenate ( \ [ train_data[:i*num_val_samples], \ train_data[:(i+1)*num_val_samples:]], \ axis = 0 ) partial_train_targets = np.concatenate ( \ [ train_targets[:i*num_val_samples], \ train_targets[:(i+1)*num_val_samples:]], \ axis = 0 ) model = build_model ( ) history = model.fit ( \ partial_train_data, \ partial_train_targets, \ validation_data = ( val_data, val_targets ), \ epochs = num_epochs, \ batch_size = 1, \ verbose = 0 ) mae_history = history.history [ 'val_mean_absolute_error' ] all_mae_histories.append ( mae_history ) # # Build the history of successive k-fold validation scores. # average_mae_history = [ \ np.mean ( [ x[i] for x in all_mae_histories ] ) for i in range ( num_epochs ) ] # # Display the validation scores. # import matplotlib.pyplot as plt plt.plot ( range ( 1, len ( average_mae_history ) + 1 ), average_mae_history ) plt.xlabel ( 'Epochs' ) plt.ylabel ( 'Validation MAE' ) plt.title ( 'Evolution of validation MAEs' ) filename = 'boston_housing_external_validation_mae.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved in "%s"' % ( filename ) ) plt.show ( ) # # Redisplay, omitting first 10 values. # def smooth_curve ( points, factor = 0.9 ): smoothed_points = [] for point in points: if ( smoothed_points ): previous = smoothed_points[-1] smoothed_points.append ( previous * factor + point * ( 1.0 - factor ) ) else: smoothed_points.append ( point ) return smoothed_points smoothed_mae_history = smooth_curve ( average_mae_history[10:] ) plt.plot ( range ( 1, len ( smoothed_mae_history ) + 1 ), smoothed_mae_history ) plt.xlabel ( 'Epochs' ) plt.ylabel ( 'Validation MAE' ) plt.title ( 'Evolution of validation MAEs (smoothed)' ) filename = 'boston_housing_external_validation_mae_smoothed.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved in "%s"' % ( filename ) ) plt.show ( ) # # Training the final model. # model = build_model ( ) model.fit ( train_data, train_targets, epochs = 80, batch_size = 16, verbose = 0 ) test_mse_score, test_mae_score = model.evaluate ( test_data, test_targets ) print ( test_mae_score ) # # Terminate. # print ( '' ) print ( 'boston_housing_external:' ) 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 ( ) boston_housing_external ( ) timestamp ( )