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

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.

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 (Input, Output), where Input is an array of numbers or a single number, and so is Output.

14.4 Training Neural Networks

14.5 Saving and Loading Neural Networks

14.6 Running Neural Networks

1http://leenissen.dk/fann/wp/

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.