Lightweight Neural Network
Main Page | Directories | File List | Globals

network.c File Reference


Functions

void net_randomize (network_t *net, float range)
 Assign random values to all weights in the network.
void net_reset_deltas (network_t *net)
 Set deltas of the network to 0.
void net_use_bias (network_t *net, int flag)
 Enable or disable use of bias.
static void allocate_layer (layer_t *layer, int no_of_neurons)
 [Internal] Allocate memory for the neurons in a layer of a network.
static void allocate_weights (layer_t *lower, layer_t *upper)
 [Internal] Allocate memory for the weights connecting two layers.
network_t * net_allocate_l (int no_of_layers, const int *arglist)
 Allocate memory for a network.
network_t * net_allocate (int no_of_layers,...)
 Allocate memory for a network.
void net_free (network_t *net)
 Free memory allocated for a network.
void net_set_momentum (network_t *net, float momentum)
 Change the momentum of a network.
float net_get_momentum (const network_t *net)
 Retrieve the momentum of a network.
void net_set_learning_rate (network_t *net, float learning_rate)
 Change the learning rate of a network.
float net_get_learning_rate (const network_t *net)
 Retrieve the momentum of a network.
int net_get_no_of_inputs (const network_t *net)
 Retrieve the number of inputs of a network.
int net_get_no_of_outputs (const network_t *net)
 Retrieve the number of outputs of a network.
int net_get_no_of_layers (const network_t *net)
 Retrieve the number of layers of a network.
int net_get_no_of_weights (const network_t *net)
 Retrieve the number of weights of a network.
void net_set_weight (network_t *net, int l, int nl, int nu, float weight)
 Set a weight of a network.
float net_get_weight (const network_t *net, int l, int nl, int nu)
 Retrieve a weight of a network.
float net_get_bias (const network_t *net, int l, int nu)
 Retrieve a bias weight of a network.
void net_set_bias (network_t *net, int l, int nu, float weight)
 Retrieve a bias weight of a network.
int net_fprint (FILE *file, const network_t *net)
 Write a network to a file.
network_t * net_fscan (FILE *file)
 Read a network from a file.
int net_print (const network_t *net)
 Write a network to a stdout.
int net_save (const char *filename, const network_t *net)
 Write a network to a file.
network_t * net_load (const char *filename)
 Read a network from a file.
int net_fbprint (FILE *file, const network_t *net)
 Write a network to a binary file.
network_t * net_fbscan (FILE *file)
 Read a network from a binary file.
int net_bsave (const char *filename, const network_t *net)
 Write a network to a binary file.
network_t * net_bload (const char *filename)
 Read a network from a binary file.
static void set_input (network_t *net, const float *input)
 [Internal] Copy inputs to input layer of a network.
static void get_output (const network_t *net, float *output)
 [Interal] Copy outputs from output layer of a network.
static float sigma (float x)
 [Internal] Activation function of a neuron.
static void propagate_layer (layer_t *lower, layer_t *upper)
 [Internal] Forward propagate inputs from one layer to next layer.
static void forward_pass (network_t *net)
 [Internal] Forward propagate inputs through a network.
float net_compute_output_error (network_t *net, const float *target)
 Compute the output error of a network.
float net_get_output_error (const network_t *net)
 Retrieve the output error of a network.
static void backpropagate_layer (layer_t *lower, layer_t *upper)
 [Internal] Backpropagate error from one layer to previous layer.
static void backward_pass (network_t *net)
 [Internal] Backpropagate output error through a network.
static void adjust_weights (network_t *net)
 [Internal] Adjust weights based on (backpropagated) output error.
void net_compute (network_t *net, const float *input, float *output)
 Compute outputs of a network for given inputs.
void net_train (network_t *net)
 Train a network.
static void adjust_deltas_batch (network_t *net)
 [Internal] Adjust deltas based on (backpropagated) output error.
static void adjust_weights_batch (network_t *net)
 [Internal] Adjust weights based on deltas determined by batch training.
void net_begin_batch (network_t *net)
 Begin training in batch mode.
void net_train_batch (network_t *net)
 Train a network in batch mode.
void net_end_batch (network_t *net)
 End training in batch mode adjusting weights.
void net_jolt (network_t *net, float factor, float range)
 Make small random changes to the weights of a network.
void net_add_neurons (network_t *net, int layer, int neuron, int number, float range)
 Add neurons to a network.
void net_remove_neurons (network_t *net, int layer, int neuron, int number)
 Remove neurons from a network.
network_t * net_copy (const network_t *net)
 Copy a network.
void net_overwrite (network_t *dest, const network_t *src)
 Overwrite one network with another.

Detailed Description

Lightweight backpropagation neural network.

This is a lightweight library implementating a neural network for use in C and C++ programs. It is intended for use in applications that just happen to need a simply neural network and do not want to use needlessly complex neural network libraries. It features multilayer feedforward perceptron neural networks, sigmoidal activation function with bias, backpropagation training with settable learning rate and momentum, and backpropagation training in batches.


Function Documentation

static void adjust_deltas_batch network_t *  net  )  [inline, static]
 

[Internal] Adjust deltas based on (backpropagated) output error.

Parameters:
net Pointer to a neural network.

static void adjust_weights_batch network_t *  net  )  [inline, static]
 

[Internal] Adjust weights based on deltas determined by batch training.

Parameters:
net Pointer to a neural network.

static void allocate_layer layer_t *  layer,
int  no_of_neurons
[static]
 

[Internal] Allocate memory for the neurons in a layer of a network.

Parameters:
layer Pointer to layer of a neural network.
no_of_neurons Integer.
Allocate memory for a list of no_of_neuron + 1 neurons in the specified layer. The extra neuron is used for the bias.

static void allocate_weights layer_t *  lower,
layer_t *  upper
[static]
 

[Internal] Allocate memory for the weights connecting two layers.

Parameters:
lower Pointer to one layer of a neural network.
upper Pointer to the next layer of a neural network.
Allocate memory for the weights connecting two layers of a neural network. The neurons in these layers should previously have been allocated with allocate_layer().

void net_add_neurons network_t *  net,
int  layer,
int  neuron,
int  number,
float  range
 

Add neurons to a network.

Parameters:
net Pointer to a neural network.
layer Integer
neuron Integer
number Integer
range Floating point number

network_t* net_allocate int  no_of_layers,
  ...
 

Allocate memory for a network.

Parameters:
no_of_layers Integer.
... Sequence of integers.
Returns:
Pointer to newly allocated network.
Allocate memory for a neural network with no_of_layer layers, including the input and output layer. The number of neurons in each layer is given as ..., starting with the input layer and ending with the output layer.

network_t* net_allocate_l int  no_of_layers,
const int *  arglist
 

Allocate memory for a network.

Parameters:
no_of_layers Integer.
arglist Pointer to sequence of integers.
Returns:
Pointer to newly allocated network.
Allocate memory for a neural network with no_of_layer layers, including the input and output layer. The number of neurons in each layer is given in arglist, with arglist[0] being the number of neurons in the input layer and arglist[no_of_layers-1] the number of neurons in the output layer.

void net_begin_batch network_t *  net  ) 
 

Begin training in batch mode.

Parameters:
net Pointer to a neural network.
Note that batch training does not care about momentum.

network_t* net_bload const char *  filename  ) 
 

Read a network from a binary file.

Parameters:
filename Pointer to name of file to read from.
Returns:
Pointer to the read neural network on success, NULL on failure.

int net_bsave const char *  filename,
const network_t *  net
 

Write a network to a binary file.

Parameters:
filename Pointer to name of file to write to.
net Pointer to a neural network.
Returns:
0 on success, a negative number on failure.
Write a binary representation of a neural network to a file. Note that the resulting file is not necessarily portable across platforms.

void net_compute network_t *  net,
const float *  input,
float *  output
 

Compute outputs of a network for given inputs.

Parameters:
net Pointer to a neural network.
input Pointer to sequence of floating point numbers.
output Pointer to sequence of floating point numbers or NULL.
Compute outputs of a neural network for given inputs by forward propagating the inputs through the layers. If output is non-NULL, the outputs are copied to output (otherwise they are only stored internally in the network). Note that the outputs of the neural network will always lie in the interval (0,1); the caller will have to rescale them if neccesary.

float net_compute_output_error network_t *  net,
const float *  target
 

Compute the output error of a network.

Parameters:
net Pointer to a neural network.
target Pointer to a sequence of floating point numbers.
Returns:
Output error of the neural network.
Before calling this routine, net_compute() should have been called to compute the ouputs for given inputs. This routine compares the actual output of the neural network (which is stored internally in the neural network) and the intended output (in target). The return value is the half the square of the Euclidean distance between the actual output and the target. This routine also prepares the network for backpropagation training by storing (internally in the neural network) the errors associated with each of the outputs. Note that the targets shoud lie in the interval [0,1], since the outputs of the neural network will always lie in the interval (0,1).

network_t* net_copy const network_t *  net  ) 
 

Copy a network.

Parameters:
net Pointer to a neural network.
Returns:
Pointer to a copy of the neural network.

void net_end_batch network_t *  net  ) 
 

End training in batch mode adjusting weights.

Parameters:
net Pointer to a neural network.
Adjust the weights in the neural network according to the average delta of all patterns in the batch.

int net_fbprint FILE *  file,
const network_t *  net
 

Write a network to a binary file.

Parameters:
file Pointer to file descriptor.
net Pointer to a neural network.
Returns:
0 on success, a negative number on failure.
Write a binary representation of a neural network to a file. Note that the resulting file is not necessarily portable across platforms.

network_t* net_fbscan FILE *  file  ) 
 

Read a network from a binary file.

Parameters:
file Pointer to a file descriptor.
Returns:
Pointer to the read neural network on success, NULL on failure.

int net_fprint FILE *  file,
const network_t *  net
 

Write a network to a file.

Parameters:
file Pointer to file descriptor.
net Pointer to a neural network.
Returns:
0 on success, a negative number of failure.

void net_free network_t *  net  ) 
 

Free memory allocated for a network.

Parameters:
net Pointer to a neural network.

network_t* net_fscan FILE *  file  ) 
 

Read a network from a file.

Parameters:
file Pointer to a file descriptor.
Returns:
Pointer to the read neural network on success, NULL on failure.

float net_get_bias const network_t *  net,
int  l,
int  nu
 

Retrieve a bias weight of a network.

Parameters:
net Pointer to a neural network.
l Number of layer.
nu Number of the layer.
Returns:
Bias weight of the neuron numbered nu in the layer numbered l.
[internal] Bias is implemented by having an extra neuron in every layer. The output of this neuron is permanently set to 1. The bias weight returned by this routine is simply the weight from this extra neuron in the layer numbered l-1 to the neuron numbered nu in the layer numbered l.

float net_get_learning_rate const network_t *  net  ) 
 

Retrieve the momentum of a network.

Parameters:
net Pointer to a neural network.
Returns:
Learning rate of the neural work.

float net_get_momentum const network_t *  net  ) 
 

Retrieve the momentum of a network.

Parameters:
net Pointer to a neural network.
Returns:
Momentum of the neural work.

int net_get_no_of_inputs const network_t *  net  ) 
 

Retrieve the number of inputs of a network.

Parameters:
net Pointer to a neural network.
Returns:
Number of neurons in the input layer of the neural network.

int net_get_no_of_layers const network_t *  net  ) 
 

Retrieve the number of layers of a network.

Parameters:
net Pointer to a neural network.
Returns:
Number of layers, including the input and output layers, of the neural network.

int net_get_no_of_outputs const network_t *  net  ) 
 

Retrieve the number of outputs of a network.

Parameters:
net Pointer to a neural network.
Returns:
Number of neurons in the output layer of the neural network.

int net_get_no_of_weights const network_t *  net  ) 
 

Retrieve the number of weights of a network.

Parameters:
net Pointer to a neural network.
Returns:
The total number of weights in the neural network.

float net_get_output_error const network_t *  net  ) 
 

Retrieve the output error of a network.

Parameters:
net Pointer to a neural network.
Returns:
Output error of the neural network.
Before calling this routine, net_compute() and net_compute_output_error() should have been called to compute outputs for given inputs and to acually compute the output error. This routine merely returns the output error (which is stored internally in the neural network).

float net_get_weight const network_t *  net,
int  l,
int  nl,
int  nu
 

Retrieve a weight of a network.

Parameters:
net Pointer to a neural network.
l Number of lower layer.
nl Number of neuron in the lower layer.
nu Number of neuron in the next layer.
Returns:
Weight connecting the neuron numbered nl in the layer numbered l with the neuron numbered nu in the layer numbered l+1.

void net_jolt network_t *  net,
float  factor,
float  range
 

Make small random changes to the weights of a network.

Parameters:
net Pointer to a neural network.
factor Floating point number.
range Floating point number.
All weights in the neural network that are in absolute value smaller than range become a random value from the interval [-range,range]. All other weights get multiplied by a random value from the interval [1-factor,1+factor].

network_t* net_load const char *  filename  ) 
 

Read a network from a file.

Parameters:
filename Pointer to name of file to read from.
Returns:
Pointer to the read neural network on success, NULL on failure.

void net_overwrite network_t *  dest,
const network_t *  src
 

Overwrite one network with another.

Parameters:
dest Pointer to a neural network.
src Pointer to a neural network.
The neural network dest becomes a copy of the neural network src. Note that dest must be an allocated neural network and its original contents is discarded (with net_free()).

int net_print const network_t *  net  ) 
 

Write a network to a stdout.

Parameters:
net Pointer to a neural network.
Returns:
0 on success, a negative number on failure.

void net_randomize network_t *  net,
float  range
 

Assign random values to all weights in the network.

Parameters:
net Pointer to neural network.
range Floating point number.
All weights in the neural network are assigned a random value from the interval [-range,range].

void net_remove_neurons network_t *  net,
int  layer,
int  neuron,
int  number
 

Remove neurons from a network.

Parameters:
net Pointer to a neural network.
layer Integer
neuron Integer
number Integer

void net_reset_deltas network_t *  net  ) 
 

Set deltas of the network to 0.

Parameters:
net Pointer to neural network.

int net_save const char *  filename,
const network_t *  net
 

Write a network to a file.

Parameters:
filename Pointer to name of file to write to.
net Pointer to a neural network.
Returns:
0 on success, a negative number on failure.

void net_set_bias network_t *  net,
int  l,
int  nu,
float  weight
 

Retrieve a bias weight of a network.

Parameters:
net Pointer to a neural network.
l Number of layer.
nu Number of the layer.
weight Floating point number. Set the bias weight of the neuron numbered nu in the layer numbered l.
[internal] Bias is implemented by having an extra neuron in every layer. The output of this neuron is permanently set to 1. This routine simply sets the the weight from this extra neuron in the layer numbered l-1 to the neuron numbered nu in the layer numbered l.

void net_set_learning_rate network_t *  net,
float  learning_rate
 

Change the learning rate of a network.

Parameters:
net Pointer to a neural network.
learning_rate Floating point number.

void net_set_momentum network_t *  net,
float  momentum
 

Change the momentum of a network.

Parameters:
net Pointer to a neural network.
momentum Floating point number.

void net_set_weight network_t *  net,
int  l,
int  nl,
int  nu,
float  weight
 

Set a weight of a network.

Parameters:
net Pointer to a neural network.
l Number of lower layer.
nl Number of neuron in the lower layer.
nu Number of neuron in the next layer.
weight Floating point number. The weight connecting the neuron numbered nl in the layer numbered l with the neuron numbered nu in the layer numbered l+1 is set to weight.

void net_train network_t *  net  ) 
 

Train a network.

Parameters:
net Pointer to a neural network.
Before calling this routine, net_compute() and net_compute_output_error() should have been called to compute outputs for given inputs and to prepare the neural network for training by computing the output error. This routine performs the actual training by backpropagating the output error through the layers.

void net_train_batch network_t *  net  ) 
 

Train a network in batch mode.

Parameters:
net Pointer to a neural network.
Before calling this routine, net_begin_batch() should have been called (at the start of the batch) to begin batch training. Furthermore, for the current input/target pair, net_compute() and net_compute_output_error() should have been called to compute outputs for given the inputs and to prepare the neural network for training by computing the output error using the given targets. This routine performs the actual training by backpropagating the output error through the layers, but does not change the weights. The weights will be changed when (at the end of the batch) net_end_batch() is called.

void net_use_bias network_t *  net,
int  flag
 

Enable or disable use of bias.

Parameters:
net Pointer to neural network.
flag Boolean. Disable use of bias if flag is zero; enable otherwise. By default, bias is enabled.
Copyright © 2003-2005
Peter van Rossum
SourceForge Doxygen Updated
28 July 2005