#! /usr/bin/env python3 # def montana ( ): #*****************************************************************************80 # ## montana uses keras to solve a simple 1d regression case. # # Discussion: # # This is intended to be a short, simple, quick keras example. # # The data is available as internal functions, and so this code # can be run standalone. # # We are searching for the weights ( a, b ) in a formula: # y = a * x + b # that will best match a set of data. # # We have 100 pairs of (x,y) data for training, and 10 pairs for testing. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 07 November 2019 # # Author: # # John Burkardt. # # Reference: # # Francois Chollet, # Deep Learning with Python, # Manning, 2018, # ISBN: 9781617294433. # import keras import matplotlib.pyplot as plt import numpy as np import platform print ( '' ) print ( 'montana:' ) print ( ' python version: %s' % ( platform.python_version ( ) ) ) print ( ' keras version: %s' % ( keras.__version__ ) ) print ( ' Neural network to solve a 1d regression problem.' ) print ( ' Find weights (a,b) so that the formula' ) print ( ' y = a x + b' ) print ( ' best approximates a set of (x,y) data.' ) print ( ' The data is read from internal functions.' ) print ( ' There are 100 pairs of (x,y) data for training.' ) print ( ' The resulting (a,b) are applied to 10 pairs of (x,y) test data.' ) # # Load the datasets. # train = montana_train_data ( ) print ( '' ) print ( ' Training data shape: ', train.shape ) train_data = train[:,0] train_targets = train[:,1] train_num = len ( train_data ) test = montana_test_data ( ) print ( ' Test data shape: ', test.shape ) test_data = test[:,0] test_targets = test[:,1] test_num = len ( test_data ) # # Show the first three pairs of data and targets. # print ( 'First 3 training items:', train_data[0:3] ) print ( 'First 3 training targets:', train_targets[0:3] ) # # Create the keras model. # from keras import models from keras import layers model = models.Sequential ( ) model.add ( layers.Dense ( input_dim = 1, units = 1 ) ) model.compile ( optimizer = 'sgd', loss = 'mse' ) # # Train the model. # print ( '' ) print ( 'Training the model:' ) print ( '' ) for step in range ( 101 ): cost = model.train_on_batch ( train_data, train_targets ) if ( step % 10 == 0 ): print ( 'step %d: training cost = %g' % ( step, cost ) ) # # Test the model. # print ( '' ) print ( 'Testing the model:' ) print ( '' ) cost = model.evaluate ( test_data, test_targets, batch_size = test_num ) W, b = model.layers[0].get_weights ( ) print ( '' ) print ( 'Results:' ) print ( '' ) print ( 'Test cost:', cost ) print ( 'Weights = ', W ) print ( 'Bias = ', b ) # # Plot the training data (blue) and testing data (red), # and the regression line (red). # xmin = -1.0 xmax = 2.0 plt.plot ( [xmin, xmax ], [ b + W[0] * xmin, b + W[0] * xmax], 'r-' ) plt.plot ( train_data, train_targets, 'bo' ) plt.plot ( test_data, test_targets, 'ro' ) plt.grid ( True ) plt.xlabel ( '<--- X --->', fontsize = 16 ) plt.ylabel ( '<--- Y --->', fontsize = 16 ) plt.title ( 'Data: train (blue), test (red)', fontsize = 16 ) filename = 'montana.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved as "%s"' % ( filename ) ) # # Terminate. # print ( '' ) print ( 'montana:' ) print ( ' Normal end of execution.' ) return def montana_train_data ( ): #*****************************************************************************80 # ## montana_train_data supplies the training data. # import numpy as np data = np.array ( \ [[ 0.598499, 2.03206 ], [ 0.60241, 2.44061 ], [ 0.528659, 2.37146 ], [ 1.14069, 3.13872 ], [-0.229003, 1.25336 ], [ 1.25808, 3.95505 ], [ 1.65164, 5.08722 ], [-0.535303, 1.2575 ], [ 1.01164, 3.58375 ], [ 0.930335, 2.84445 ], [ 1.17088, 3.19438 ], [ 1.24618, 4.05287 ], [ 1.45564, 3.91006 ], [-0.706274, 0.636657 ], [-0.989338, 1.12846 ], [-0.307571, 1.35216 ], [-0.963235, 0.943437 ], [ 0.0606251, 1.25155 ], [-0.444156, 1.19095 ], [ 1.37831, 4.11453 ], [ 0.836516, 2.54402 ], [ 1.69829, 4.18295 ], [-0.0538967, 0.865774 ], [ 0.219966, 1.8477 ], [ 1.94078, 5.13762 ], [ 0.626196, 2.6553 ], [-0.273116, 1.41491 ], [-0.243275, 1.56301 ], [ 0.695467, 2.35417 ], [-0.599989, 0.996534 ], [ 1.62878, 5.12322 ], [ 0.440996, 1.95425 ], [-0.150022, 0.745887 ], [ 1.67663, 4.61879 ], [ 1.4829, 4.22713 ], [ 1.93991, 5.01128 ], [ 1.6017, 4.65738 ], [-0.419483, 1.5348 ], [ 0.363472, 1.45974 ], [ 1.41555, 4.0826 ], [ 1.24169, 3.51175 ], [ 1.75988, 4.41841 ], [-0.33859, 1.23152 ], [ 1.99386, 5.1235 ], [ 0.80839, 2.6468 ], [-0.893445, 1.26048 ], [ 0.666563, 1.96845 ], [-0.0410724, 1.74292 ], [ 0.506314, 2.31123 ], [ 0.599365, 2.24982 ], [ 0.98754, 2.53438 ], [ 0.374827, 1.70017 ], [-0.670371, 0.986843 ], [ 1.18981, 3.36155 ], [ 1.35261, 4.17052 ], [-0.576131, 1.34247 ], [ 1.51606, 4.36953 ], [ 0.208179, 1.84875 ], [ 0.219271, 1.76952 ], [ 0.111999, 1.60956 ], [ 1.75105, 4.69809 ], [ 0.472038, 2.18206 ], [ 1.89797, 5.0723 ], [-0.790837, 1.00822 ], [ 0.086146, 1.07676 ], [-0.86452, 0.845198 ], [ 0.00721245, 1.1904 ], [ 0.221532, 1.53029 ], [-0.300679, 0.789804 ], [-0.388771, 1.04663 ], [ 1.80441, 4.73272 ], [ 0.73114, 2.87377 ], [ 0.872665, 3.19095 ], [ 1.52891, 3.92502 ], [-0.470547, 0.812532 ], [ 1.55232, 3.90986 ], [-0.102594, 1.19779 ], [ 1.50326, 4.06359 ], [ 1.87327, 4.85862 ], [-0.655811, 1.04308 ], [-0.650234, 0.982951 ], [-0.862747, 1.05028 ], [ 0.744127, 2.63398 ], [-0.705918, 0.790914 ], [-0.360235, 0.819268 ], [-0.48344, 1.03121 ], [ 1.91945, 5.01807 ], [-0.405291, 0.995278 ], [-0.466338, 1.42394 ], [-0.184033, 1.73316 ], [-0.182296, 1.27067 ], [ 1.4159, 3.82413 ], [-0.48002, 0.977773 ], [-0.0765423, 1.68685 ], [ 0.396682, 2.00995 ], [ 1.4209, 3.93803 ], [ 1.45746, 3.89205 ], [-0.40407, 1.3084 ], [-0.542388, 1.27157 ], [ 1.06106, 2.95062 ]] ) return data def montana_test_data ( ): #*****************************************************************************80 # ## montana_test_data supplies the test data. # import numpy as np data = np.array ( \ [[-0.748273, 0.950892], [-0.315139, 1.30002 ], [-0.312186, 1.13169 ], [ 0.492757, 1.55160 ], [ 0.833893, 2.80304 ], [-0.349539, 1.36128 ], [-0.802062, 1.18364 ], [ 1.046400, 3.37080 ], [ 0.758936, 2.15484 ], [-0.283329, 1.29814 ]] ) return data if ( __name__ == '__main__' ): montana ( )