Neural_Network package

Submodules

Neural_Network.neuralnet module

Neural Network

A module to train a neural network with an arbitrary number of layers and and arbitrary number of neurons per layer. Use as:

from neuralnet import NN_hwr
nn = NN_hwr([list containing number of neurons per layer])
nn.train_nn(Input training data, label_data, Number of iterations,
            Number of examples per batch, learning_rate)
class Neural_Network.neuralnet.NN_hwr(num_neurons_list)

Bases: object

A template class for a generic neural network. Initialise with a list with each element being the number of neurons in that layer For the init function, the parameters are

Parameters:num_neurons_list (list) – Create a neural network number of neuron per layer given by the list
accuracy(X_test, y_test)

Computes the accuracy with which the Network, once trained classifies the digits.

Parameters:
  • X_test (ndarray) – Test data in the same format as that for which the network was trained
  • y_test (ndarray) – Labels for the test data. Same format as the labels for training data
Returns:

Float value of the accuracy in percentage

back_prop(training_example)

Computes the partial derivatives of the cost function with respect to the weights and biases of the network training_example is a tuple with element fisrt element an np array and the second, an array of length 10 of zeros everywhere except at the image label where there is a 1

Parameters:training_example (tuple) – Tuple with first element as the input data and the second being its label
Returns:a tuple of numpy arrays containing the required derivatives
cost_derivative(activation, y)

Computes the derivative of the cost function given output activations and the labels

Parameters:
  • activation (ndarray) – Activations of the neurons for a given input
  • y (list) – The expected output for the input
Returns:

Float value of the cost function of the Neural Network

cost_function(X_train, y_train)

Computes the quadratic cost function of the Neural Network

Parameters:
  • X_train (ndarray) – Input data for training
  • y_train (ndarray) – Labels corresponding to the inputs
Returns:

Float value of the cost function of the Neural Network

forward_prop(x_train)

Computes the activations and weighted inputs of the neurons in the network for the given input data.

Parameters:x_train (ndarray) – The input for the first layer which needs to be forwards propogated
Returns:A tuple of lists containing activations and weighted inputs
train_batch(batch, learning_rate)

Trains the network with one subset of the training data. Input is the subset of training data for witch the network is to be trained. Learning rate governs the rate at which the Weights and biases change in the gradient descent algorithm

Parameters:
  • batch (ndarray) – An array of training examples with each being a tuple containing the input data and its label.
  • learning_rate (float) – The learning rate which determines the step size in gradient descent
Returns:

None

train_nn(X_train, y_train, n_epochs, batch_size, learning_rate)

Trains the neural network with the test data. n_epochs is the number sweeps over the whole data. batch_size is the number of training example per batch in the stochastic gradient descent. X_train and y_train are the images and labels in the training data. X_train must be a 2-D array with only one row and y_train is an array of length 10 of zeros everywhere except at the image label (where there is a 1)

Parameters:
  • X_train (ndarray) – Numpy array containing the input training data
  • y_train (ndarray) – Numpy array containing the labels for training. Formatted as an array of arrays with 1 at the label position and 0 everywhere else
  • n_epochs (int) – The number of sweeps over the data-set in the Stochastic Gradient Descent
  • batch_size (int) – Number of training examples per batch in the Stochastic Gradient Descent
  • learning_rate (fload) – The learning rate which determines the step size in gradient descent
Returns:

None

Neural_Network.neuralnet.sigmoid(z)

Evaluates the sigmoid function at the given input

Parameters:z (array-like) – Could be a number, list or Numpy array for which sigmoid is to be evaluated
Returns:numpy array
Neural_Network.neuralnet.sigmoid_derivative(z)

Evaluates the derivative of the sigmoid function at the given input

Parameters:z (array-like) – Could be a number, list or Numpy array for which sigmoid derivative is to be evaluated
Returns:Numpy array

Neural_Network.train_network module

Useful functionality for training Neural Network

Functions to unpack the data from the MNIST database, uncompress the data and display it

Neural_Network.train_network.display_data(imgs, nrows=1, ncols=1, nx_pixels=28, ny_pixels=28)

Dispay the images given in X.

Parameters:
  • nrows (int) – The number of plots per column. The default value is 1
  • ncols (int) – The number of plots per row. The default value is 1
  • nx_pixels (int) – The number of pixels along axis 1. The default value is 28
  • ny_pixels (int) – The number of pixels along axis 2. The default value is 28
Returns:

None.Displays the Image Data

Neural_Network.train_network.load_data(path)

Loads the image data from the path provided and returns the images and labels

Parameters:path (string) – The path to the file where the training/test data is present
Returns:A dictionary object containing the input data and labels Keys of the dict object – ‘X_train’ and ‘y_train’ or ‘X_test’ and ‘y_test’
Neural_Network.train_network.unpack_dat(imgpath, labpath)

Unpack images and labels obtained online from MNIST Database

Parameters:
  • imgpath (string) – The path for the packed image file of MNIST Database
  • labpath (string) – The path for the packed label file of MNIST Database
Returns:

Tuple of list of image pixel values and label values.

Module contents