:78 Function create_function() is deprecated [8192]
Filename | |
---|---|
code/example1.py | |
code/example2.py | |
code/first_network.py | |
code/mnist_loader.py | |
code/model1.py | |
code/model2.py | |
code/model3.py | |
code/plotting_routines.py | |
code/second_network.py |
diff --git a/code/example1.py b/code/example1.py new file mode 100644 index 0000000..4399910 --- /dev/null +++ b/code/example1.py @@ -0,0 +1,52 @@ +# +# Introduction to Neural Networks. +# Given at SciNet, September 25 2017, by Erik Spence. +# +# This file, example1.py, contains a routine to generate the data for +# the first example. +# + +####################################################################### + + +""" +example1.py contains a routine for generating the data for the class' +first example. + +""" + + +####################################################################### + + +import sklearn.datasets as skd +import sklearn.model_selection as skms + + +####################################################################### + + +def get_data(n): + + """ + This functon generates n data points generated using + scikit-learn's make_blobs function. The data is then split into + training and testing data sets, and returned. + + Inputs: + - n: int, the number of points to return. + + Outputs: two arrays of size 0.8 * n, 0.2 * n, randomly generated + using scikit-learn's make_blobs routine. + + """ + + # Generate the data. + pos, value = skd.make_blobs(n, centers = 2, + center_box = (-3, 3)) + + # Split the data into training and testing data sets, and return. + return skms.train_test_split(pos, value, + test_size = 0.2) + +####################################################################### diff --git a/code/example2.py b/code/example2.py new file mode 100644 index 0000000..24f8ec7 --- /dev/null +++ b/code/example2.py @@ -0,0 +1,51 @@ +# +# Introduction to Neural Networks. +# Given at SciNet, September 25 2017, by Erik Spence. +# +# This file, example2.py, contains a routine to generate the data +# for the second example. +# + +####################################################################### + + +""" +example2.py contains a routine for generating the data for the class' +second example. + +""" + + +####################################################################### + + +import sklearn.datasets as skd +import sklearn.model_selection as skms + + +####################################################################### + + +def get_data(n): + + """This functon generates n data points generated using + scikit-learn's make_circles function. The data is then split into + training and testing data sets, and returned. + + Inputs: + - n: int, the number of points to return. + + Outputs: two arrays of size 0.8 * n, 0.2 * n, randomly generated + using scikit-learn's make_circles routine. + + """ + + # Generate the data. + pos, value = skd.make_circles(n, noise = 0.1) + + # Split the data into training and testing data sets, and return. + return skms.train_test_split(pos, value, + test_size = 0.2) + + +####################################################################### diff --git a/code/first_network.py b/code/first_network.py index 9a06572..619390c 100644 --- a/code/first_network.py +++ b/code/first_network.py @@ -1,6 +1,6 @@ # # Introduction to Neural Networks. -# Given at SciNet, May 30 2017, by Erik Spence. +# Given at SciNet, September 25 2017, by Erik Spence. # # This file, first_network.py, contains the implementation of our # first neural network. diff --git a/code/mnist_loader.py b/code/mnist_loader.py index 59d3e71..572a3c9 100644 --- a/code/mnist_loader.py +++ b/code/mnist_loader.py @@ -1,6 +1,6 @@ # # Introduction to Neural Networks. -# Given at SciNet, May 30 2017, by Erik Spence. +# Given at SciNet, September 25 2017, by Erik Spence. # # This file, mnist_loader.py, contains the code needed to load the # MNIST dataset. The code borrows heavily from @@ -28,16 +28,17 @@ except: import gzip import numpy as np +import keras.utils as ku ####################################################################### -def load_mnist_1D(filename = '../data/mnist.pkl.gz'): +def load_mnist_1D_large(filename = '../data/mnist.pkl.gz'): """ - Returns the MNIST data as a tuple containing the training data, - the validation data, and the test data. + Returns the full MNIST data set as a tuple containing the training + data, the validation data, and the test data. Inputs: @@ -53,8 +54,8 @@ def load_mnist_1D(filename = '../data/mnist.pkl.gz'): - 2D array of floats of shape (50000, 784), containing the pixel values for each image. - - integer vector of length 50000, containing the value of - the number in the image. + - a one-hot-encoded array of shape (50000, 10), containing + the value of the number in the image. - validation_data: same as training_data, except with length 10000 @@ -74,9 +75,58 @@ def load_mnist_1D(filename = '../data/mnist.pkl.gz'): f.close() # Return the values. - return training_data[0], training_data[1], \ - validation_data[0], validation_data[1], \ - test_data[0], test_data[1] + return training_data[0], ku.to_categorical(training_data[1]), \ + validation_data[0], ku.to_categorical(validation_data[1]), \ + test_data[0], ku.to_categorical(test_data[1]) + + +####################################################################### + + +def load_mnist_1D_small(filename = '../data/mnist.pkl.gz'): + + """ + Returns a small version of the MNIST data as a tuple containing + the training data, the validation data, and the test data. + + Inputs: + + - filename: string, name of the file containing the data. + + Outputs: + + - tuple, containing the training, validation and test data. These + take the form: + + - training_data: tuple, consisting of: + + - 2D array of floats of shape (500, 784), containing the + pixel values for each image. + + - integer vector of length 500, containing the value of + the number in the image. + + - validation_data: same as training_data, except with length + 200 + + - test_data: same as training_data, except with length + 100 + + """ + + # Open the file. + f = gzip.open(filename, 'rb') + + # Load the data. + training_data, validation_data, test_data = cPickle.load(f) + + # Close the file. + f.close() + + # Return the values. + return training_data[0][0:500, :], training_data[1][0:500], \ + validation_data[0][0:200, :], validation_data[1][0:200], \ + test_data[0][0:100, :], test_data[1][0:100] ####################################################################### @@ -114,7 +164,8 @@ def load_mnist_2D(filename = ''): """ # Get the data. - tr_d, tr_v, va_d, va_v, te_d, te_v = load_mnist_1D(filename = filename) + tr_d, tr_v, va_d, va_v, te_d, te_v = \ + load_mnist_1D(filename = filename) # Reshape the data. training_inputs = np.array([x.reshape(28, 28, 1) for x in tr_d]) @@ -122,6 +173,7 @@ def load_mnist_2D(filename = ''): test_inputs = np.array([x.reshape(28, 28, 1) for x in te_d]) # Return the data. - return training_inputs, tr_v, validation_inputs, va_v, \ - test_inputs, te_v + return training_inputs, ku.to_categorical(tr_v, 10), \ + validation_inputs, ku.to_categorical(va_v, 10), \ + test_inputs, ku.to_categorical(te_v, 10) diff --git a/code/model1.py b/code/model1.py new file mode 100644 index 0000000..e54c6e8 --- /dev/null +++ b/code/model1.py @@ -0,0 +1,63 @@ +# +# Introduction to Neural Networks. +# Given at SciNet, September 25 2017, by Erik Spence. +# +# This file, model1.py, contains a routine to generate the model for +# the first Keras example. +# + +####################################################################### + + +""" +model1.py contains a routine which generates the model for the class' +first Keras example. + +""" + + +####################################################################### + + +import keras.models as km +import keras.layers as kl + + +####################################################################### + + +def get_model(numnodes, input_size = 784, output_size = 10): + + """ + This function returns a simple Keras model, consisting of a + re-implementation of the second_network.py neural network, with + numnodes in the hidden layer. + + Inputs: + - numnodes: int, the number of nodes in the hidden layer. + + - intput_size: int, the size of the input data, default = 784. + + - output_size: int, the number of nodes in the output layer, + default = 10. + + Output: the constructed Keras model. + + """ + + # Initialize the model. + model = km.Sequential() + + # Add a hidden fully-connected layer. + model.add(kl.Dense(numnodes, + input_dim = input_size, + activation = 'sigmoid')) + + # Add the output layer. + model.add(kl.Dense(output_size, activation = 'sigmoid')) + + # Return the model. + return model + + +####################################################################### diff --git a/code/model2.py b/code/model2.py new file mode 100644 index 0000000..bc2a5be --- /dev/null +++ b/code/model2.py @@ -0,0 +1,64 @@ +# +# Introduction to Neural Networks. +# Given at SciNet, September 25 2017, by Erik Spence. +# +# This file, model2.py, contains a routine to generate the model for +# the second Keras example. +# + +####################################################################### + + +""" +model2.py contains a routine which generates the model for the class' +second Keras example. + +""" + + +####################################################################### + + +import keras.models as km +import keras.layers as kl + + +####################################################################### + + +def get_model(numnodes, input_size = 784, output_size = 10): + + """ + This function returns a simple Keras model, consisting of a + re-implementation of the second_network.py neural network, with + numnodes in the hidden layer. It is the same as model1.py, except + for the activation functions. + + Inputs: + - numnodes: int, the number of nodes in the hidden layer. + + - intput_size: int, the size of the input data, default = 784. + + - output_size: int, the number of nodes in the output layer, + default = 10. + + Output: the constructed Keras model. + + """ + + # Initialize the model. + model = km.Sequential() + + # Add a hidden fully-connected layer. + model.add(kl.Dense(numnodes, + input_dim = input_size, + activation = 'tanh')) + + # Add the output layer. + model.add(kl.Dense(output_size, activation = 'softmax')) + + # Return the model. + return model + + +####################################################################### diff --git a/code/model3.py b/code/model3.py new file mode 100644 index 0000000..e4299a3 --- /dev/null +++ b/code/model3.py @@ -0,0 +1,78 @@ +# +# Introduction to Neural Networks. +# Given at SciNet, September 25 2017, by Erik Spence. +# +# This file, model3.py, contains a routine to generate the model for +# the third Keras example. +# + +####################################################################### + + +""" +model3.py contains a routine which generates the model for the class' +third Keras example. + +""" + + +####################################################################### + + +import keras.models as km +import keras.layers as kl + + +####################################################################### + + +def get_model(numfm, numnodes, input_shape = (28, 28, 1), + output_size = 10): + + """ + This function returns a convolution neural network Keras model, + with numfm feature maps and numnodes neurons in the + fully-connected layer. + + Inputs: + - numfm: int, the number of feature maps in the convolution layer. + + - numnodes: int, the number of nodes in the fully-connected layer. + + - intput_shape: tuple, the shape of the input data, + default = (28, 28, 1). + + - output_size: int, the number of nodes in the output layer, + default = 10. + + Output: the constructed Keras model. + + """ + + # Initialize the model. + model = km.Sequential() + + # Add a 2D convolution layer, with numfm feature maps. + model.add(kl.Conv2D(numfm, kernel_size = (5, 5), + input_shape = input_shape, + activation = 'relu')) + + # Add a max pooling layer. + model.add(kl.MaxPooling2D(pool_size = (2, 2), + strides = (2, 2))) + + # Convert the network from 2D to 1D. + model.add(kl.Flatten()) + + # Add a fully-connected layer. + model.add(kl.Dense(numnodes, + activation = 'tanh')) + + # Add the output layer. + model.add(kl.Dense(10, activation = 'softmax')) + + # Return the model. + return model + + +####################################################################### diff --git a/code/plotting_routines.py b/code/plotting_routines.py index a9b4ad3..122f5ec 100644 --- a/code/plotting_routines.py +++ b/code/plotting_routines.py @@ -1,6 +1,6 @@ # # Introduction to Neural Networks. -# Given at SciNet, May 30 2017, by Erik Spence. +# Given at SciNet, September 25 2017, by Erik Spence. # # This file, plotting_routines.py, contains some simple plotting # routines. @@ -11,7 +11,7 @@ """ plotting_routines.py contains two routines for plotting the class' -data. +data, for the first two examples. """ @@ -135,3 +135,6 @@ def plot_decision_boundary(x, v, model, predict_function, **kwargs): plot_dots(x, v) plt.show() + + +####################################################################### diff --git a/code/second_network.py b/code/second_network.py index f685537..55d1121 100644 --- a/code/second_network.py +++ b/code/second_network.py @@ -1,6 +1,6 @@ # # Introduction to Neural Networks. -# Given at SciNet, May 30 2017, by Erik Spence. +# Given at SciNet, September 25 2017, by Erik Spence. # # This file, second_network.py, contains the implementation of our # second neural network.