|
Lightweight Neural Network - Code Snippets |
|
|
|
|
Computing a neural network
This code snippet shows how to compute the outputs of a neural network
with lwneuralnet.
#include "lwneuralnet.h"
float *input;
float *output;
nework_t *net;
net = net_load(filename of neural network);
Set input to an array of floats that forms the input of the network;
Set output to where you want the ouput of the network stored;
net_compute(net, input, output);
Use the network outputs in output[0], ... ouput[net_no_of_ouputs(net)-1];
Training a neural network
The following code snippet shows how you typically train a
neural network with lwneuralnet. It changes all the weights
once for each input/target pair in a training set. You would
typically repeat the loop quite a few times until you're
satisfied with the performance of the network.
#include "lwneuralnet.h"
float *input;
float *target;
network_t *net;
int i;
Read or create the neural network;
for (i = 0; i < number of elements in training set; i++)
{
Set input to i-th input element of training set;
Set target to intended output for this input;
net_compute(net, input, NULL);
net_compute_output_error(net, target);
net_train(net);
}
Training a neural network in batches
The following code snippet does the same; the only difference is
that the weights only get changed after all input/target pairs
from the training set have been considered.
#include "lwneuralnet.h"
float *input;
float *target;
network_t *net;
int i;
Read or create the neural network;
net_begin_batch(net);
for (i = 0; i < number of elements in training set; i++)
{
Set input to i-th input element of training set;
Set target to intended output for this input;
net_compute(net, input, NULL);
net_compute_output_error(net, target);
net_train_batch(net);
}
net_end_batch(net);
Training a neural network and using a validation set to
stop training
The previous two code snippets show how to train a neural
network, but it is not yet clear when to stop training.
This code snippets gives one way to determine that.
Training will stop when the average error on the validation
set drops below a certain value or when training has been
going on for a long while.
#include "lwneuralnet.h"
float *input;
float *target;
float error;
network_t *net;
int i, j, count;
Read or create the neural network;
count = 0;
do
{
/* Train the network with all pairs in the training set. */
for (i = 0; i < number of elements in training set; i++)
{
Set input to i-th input element of training set;
Set target to intended output for this input;
net_compute(net, input, NULL);
net_compute_output_error(net, target);
net_train(net);
count++;
}
/* Compute the average error for all pairs in the validation set. */
error = 0.0;
for (j = 0; j < number of elements in validation set; j++)
{
Set input to i-th input element of validation set;
Set target to intended output for this input;
net_compute(net, input, NULL);
net_compute_output_error(net, target);
error += net_get_output_error(net);
}
error /= number of elements in validation set;
/* Keep going until either the average error is small enough
* or a large amount of trainings have been completed. */
} while ((error > maximal error that is still acceptable) &&
(count < maximal number of trainings you want to do));
|
Copyright © 2003-2005 Peter van Rossum |
|
Updated 26 October 2003 |
|
|