Chapter 14
The nn (Neural Networks) Module
On this page
The nn module provides a high-level interface between Picat and the FANN1 neural networks library, which implements feedforward neural networks.2 A feedforward neural network consists of neurons organized in layers from an input layer to an output layer, possibly with a number of hidden layers. A feedforward network represents a function from input to output. Neurons in a layer (except for the input layer) are connected to neurons in the previous layer. The connections have weights. The neurons in the input layer receive the input. The information is propagated forward through the layers until it reaches the output layer, where the output is returned. The information that a neuron receives is determined by the connected predecessor neurons, the weights of the connections, and an activation function. The connection weights of a neural network are normally adjusted through training on a given set of input-output pairs, called training data. Once a neural network is trained, it can be used to predict the output for a given input.
The following gives an example program which creates a neural network for the xor function, and trains it on a set of data stored in a file:
import nn.
main =>
NN = new_nn({2,3,1}),
nn_train(NN, "xor.data"),
nn_save(NN, "xor.net"),
nn_destroy_all.
The function new_nn({2,3,1}) returns a neural network with three layers, where the input layer has 2 neurons, the hidden layer has 3 neurons, and the output layer has 1 neuron. The program does not specify any activation functions used between layers, which means that the default activation function, which is sym_sigmoid, will be used. The predicate nn_train(NN, "xor.data") trains the neural network with the training data stored in the file "xor.data". The user is able to specify an algorithm to be used in the training and several parameters that affect the behavior of the algorithm, such as the maximum number of iterations (called epochs), the learning rate, and the error function. This example does not specify a training algorithm or any of the training parameters, which means that the default algorithm, which is rprop, is used with the default setting. The predicate nn_save saves the trained neural network into a file named "xor.net". The predicate nn_destroy_all clears the neural network and the internal data structures used during training.
The text file "xor.data" contains the following training data:
4 2 1
-1 -1
-1
-1 1
1
1 -1
1
1 1
-1
The three integers in the first line state, respectively, that the number of input-output pairs is 4, the number of input values is 2, and the number of output values is 1. The remaining lines give the input-output pairs.
The following program performs the same task as the above program, except that it trains the neural network with internal data:
import nn.
main =>
NN = new_nn({2,3,1}),
nn_train(NN, [({-1,-1}, -1),
({-1,1}, 1),
({1,-1}, 1),
({1,1}, -1)]),
nn_save(NN, "xor.net"),
nn_destroy_all.
The predicate nn_train is overloaded. When the second argument is a file name, Picat reads training data from the file. Otherwise, Picat expects a collection (a list or an array) of input-output pairs.
The following example program illustrates how to use a trained network:
import nn.
main =>
NN = nn_load("xor.net"),
printf("xor(-1, 1) = %w\n", nn_run(NN, {-1,1})),
nn_destroy_all.
The function nn_load loads a neural network. The function nn_run uses the network to predict the output for an input.
14.1 Creating, Printing, and Destroying Neural Networks
- new_nn() = : This function creates a fully connected neural network of the structure , which is a collection of positive integers indicating the number of neurons in each layer.
- new_standard_nn() = : This function is the same as new_nn().
- new_sparse_nn(, ) = : This function creates a sparse neural network that has the structure and the connection rate . The connection rate determines the sparseness of the network, with 1 indicating that the network is fully connected, and 0 indicating that the network is not connected at all.
- new_sparse_nn() = : This function creates a sparse neural network that has the structure and the connection rate 0.5.
- nn_print(): This predicate prints the attributes of the neural network , including the connections, the weights, the activation functions, and some other parameters.
- nn_destroy(): This predicate destroys the neural network .
- nn_destroy_all: This predicate destroys all the neural networks and the internal data structures.
14.2 Activation Functions
An activation function for a neuron determines how information is propagated to it from its predecessor neurons. When a new neural network is created, it uses the default activation function sym_sigmoid for all of its non-input neurons. The following predicates can be utilized to set activation functions.
-
nn_set_activation_function_layer(, , ): This predicate sets the activation function to for all the neurons in layer in the neural network . Let the number of layers in be . Since the first layer is numbered 1, must satisfy . The following activation functions are available:
- linear
- threshold
- sym_threshold: symmetric threshold.
- sigmoid
- step_sigmoid: stepped sigmoid
- sym_sigmoid: symmetric sigmoid
- elliot: an alternative to sigmoid
- sym_elliot: symmetric elliot
- gaussian
- sym_gaussian: symmetric Gaussian
- linear_piece
- sym_linear_piece: symmetric linear piece
- sin
- sym_sin: symmetric sin
- cos
- sym_cos: symmetric cos
The default activation function is sym_sigmoid. Each of these functions has a corresponding name in FANN. Please refer to the FANN documentation for a more detailed description of these functions.
- nn_set_activation_function_hidden(, ): This predicate sets the activation function to for all of the hidden layers in the neural network .
- nn_set_activation_function_output(, ): This predicate sets the activation function to for the output layer in the neural network .
- nn_set_activation_steepness_layer(, , ): This predicate sets the activation steepness for all of the neurons in , where and ( is the number of layers in ). A higher steepness value means more aggressive training. The default steepness is 0.5.
- nn_set_activation_steepness_hidden(, ): This predicate sets the activation steepness to for all of the hidden layers in the neural network .
- nn_set_activation_steepness_output(, ): This predicate sets the activation steepness to for the output layer in the neural network .
14.3 Training Data
A training dataset can be supplied to FANN either through a text file or a Picat collection. A training dataset file must have the following format:
num_train_data num_input num_output
input_data separated by spaces
output_data separated by spaces
.
.
.
input_data separated by spaces
output_data separated by spaces
A training dataset stored in a Picat collection must be either a list or an array of input-output pairs. An input-output pair has the form (, ), where is an array of numbers or a single number, and so is .
- nn_train_data_size() = : This function returns the number of input-output pairs in the dataset , which is either a file name or a Picat collection.
- nn_train_data_get(, ) = : This function returns the th pair in the dataset . Notice that while this function takes linear time when is a list, it takes constant time when is a file name or an array.3
- nn_train_data_load() = : This function loads a dataset stored in into a Picat array, and returns the array.
- nn_train_data_save(, ): This predicate saves a dataset in a Picat collection into a file named .
14.4 Training Neural Networks
-
nn_train(, , ): This predicate trains the neural network using the dataset under the control of training options . The following training options are supported:
- maxep(): is the maximum number of epochs for training.
- report(): report progress every epochs.
- derror(): is the desired error in training.
- train_func(): is the training algorithm to be used, which is one of the following: batch, inc, qprop (quick prop), rprop, and sprop (sarprop).
- lrate(): is the learning rate.
- momentum(): is the learning momentum.
- error_func(): is the error function to be used, which is either linear or tanh.
- stop_func(): is the stop function to be used, which is either bit or mse.
- bfl(): is the bit fail limit.
- qp_decay(): is the qprop decay.
- qp_mu() : is the qprop mu factor.
- rp_increase(): is the rprop increase factor.
- rp_decrease(): is the rprop decrease factor.
- rp_deltamin(): is the rprop delta min.
- rp_deltamax(): is the rprop delta max.
- rp_deltazero(): is the rprop delta zero.
- sp_weight(): is the sarprop weight decay.
- sp_thresh(): is the sarprop step error threshold factor.
- sp_shift(): is the sarprop step error shift.
- sp_temp(): is the sarprop temperature.
- scale(, , , ): use training data scaling.
- inscale(, ): use input data scaling.
- outscale(, ): use output data scaling.
- nn_train(, ): This predicate is the same as nn_train(, , []).
14.5 Saving and Loading Neural Networks
- nn_save(, ): This predicate saves the neural network to a file named .
- nn_load() = : This function creates a neural network from a FANN neural network file named .
14.6 Running Neural Networks
-
nn_run(, , ) = : This function predicts the output for a given input using the trained neural network , where is an array of numbers or a single number, and is a list of testing options. The supported testing options include:
- scaleIn(): indicates whether or not the input is scaled, with -1 meaning descale, 1 meaning scale, and 0 meaning nothing.
- scaleOut(): indicates whether or not the output is scaled, with -1 meaning descale, 1 meaning scale, and 0 meaning nothing.
- resetMSE: resets the current mean squared error of the network.
- nn_run(, ) = : This function is the same as nn_run(, , []).
2The Picat-FANN interface was implemented by Sanders Hernandez.
3The data is loaded into an internal array when the dataset is accessed the first time.