Advertisement

Neural nets

Started by August 21, 2007 10:27 AM
4 comments, last by Timkin 17 years, 2 months ago
Hey, I'm wanting to get into Neural nets but I can't understand how to program them. I understand that they basically have a threshold and if all the inputs exceed it, it trips. I guess I might have an odd outtake on them from working with beam robotics. I don't get how to program them. can anyone link me to a tutorial on them in C++?
One of the first hits on google for 'neural network tutorials'...
Advertisement
you want to start with a perceptron

it's the easiest, here's a tutorial, no code tho

http://www.cprogramming.com/tutorial/AI/perceptron.html

neural nets generally work in this way, but they're bigger, and some are linked up in different ways.

if that's no help, let me know, and i'll try my best to explain.
There are loads of different kinds of neural nets . The perceptron is a really good place to start to understand the basic principles but ideally you want to move away from using it in a realistic setting as its too simple to be of any use . These notes from a learning from data course i took are a pretty good starting point . http://homepages.inf.ed.ac.uk/amos/lfd/lectures/lfd_2005_layered_nn.pdf
Hey here's a neural net implementation I knoocked up. All the vaiables beginning with m are member variables and need to be given an appropriate value before calling this function. Variables beginning with k are constants and need to be given an appropriate value.

This implementation is designed for speed and memory performance. If you've got any questions about why things were done the way they were done I should be back on the foums soon enough. I didn't bother with a sigmoid function but that's trivial to add if you want

voidcNeuralNet::ProcessNet(         Real* lpaWeights,        Real* lpaInputs,        Real* lpaOutputs ){    Real    larOutputs[ kuMaxNeurons ];    UInt32  luWeightIndex = 0;    UInt32  luNeuronIndex;    UInt32  luNeuronIndexPerLayer;    UInt32  luPreviousRowNeuronIndex;    UInt32  luPreviousRowFirstNeuronIndex = 0;    UInt32  luInputIndex;    UInt32  luOutputIndex;    UInt32  luLayerIndex;    // We divide the weights up like this...    // muNumInputs * muNumNeuronsPerLayer    // Multiply inputs into weights    for ( luNeuronIndex = 0; luNeuronIndex < muNumNeuronsPerLayer; ++luNeuronIndex )    {        larOutputs[ luNeuronIndex ] = 0.0f;        // Bias        larOutputs[ luNeuronIndex ] += lpaWeights[ luWeightIndex++ ];        for ( luInputIndex = 0; luInputIndex < muNumInputs; ++luInputIndex )        {            larOutputs[ luNeuronIndex ] += lpaInputs[ luInputIndex ] * lpaWeights[ luWeightIndex++ ];        }    }    // muNumNeuronsPerLayer * muNumNeuronsPerLayer * ( muNumHiddenLayers - 1 )    // Process hidden layers, start on the second hidden layer as the first one was processed above    for ( luLayerIndex = 0; luLayerIndex < muNumHiddenLayers - 1; ++luLayerIndex )    {        for ( luNeuronIndexPerLayer = 0; luNeuronIndexPerLayer < muNumNeuronsPerLayer; ++luNeuronIndexPerLayer )        {            larOutputs[ luNeuronIndex ] = 0.0f;            // Bias            larOutputs[ luNeuronIndex ] += lpaWeights[ luWeightIndex++ ];            for ( luPreviousRowNeuronIndex = 0; luPreviousRowNeuronIndex < muNumNeuronsPerLayer; ++luPreviousRowNeuronIndex )            {                larOutputs[ luNeuronIndex ] += larOutputs[ luPreviousRowFirstNeuronIndex + luPreviousRowNeuronIndex ] * lpaWeights[ luWeightIndex++ ];            }            ++luNeuronIndex;        }        luPreviousRowFirstNeuronIndex += muNumNeuronsPerLayer;    }    // muNumOutputs * muNumNeuronsPerLayer    // Process outputs    for ( luOutputIndex = 0; luOutputIndex < muNumOutputs; ++luOutputIndex )    {        lpaOutputs[ luOutputIndex ] = 0.0f;        // Bias        lpaOutputs[ luOutputIndex ] += lpaWeights[ luWeightIndex++ ];        for ( luPreviousRowNeuronIndex = 0; luPreviousRowNeuronIndex < muNumNeuronsPerLayer; ++luPreviousRowNeuronIndex )        {            lpaOutputs[ luOutputIndex ] += larOutputs[ luPreviousRowFirstNeuronIndex + luPreviousRowNeuronIndex ] * lpaWeights[ luWeightIndex++ ];        }    }}
A simple implementation of a multilayer perceptron (feedforward network)
MATRIX W_i,W_o;VECTOR b_i,b_o;VECTOR x,y,h;h = tansfer_function(W_i*x+b_i);y = W_o*h+b_0;// x: input vector (m components)// h: hidden vector (n components)// y: output vector (p components)// W_i: input layer weights (n x m)// W_o: output layer weights (p x n)// b_i: input layer bias (n components)// b_o: output layer bias (p components)// transfer_function(): a function computing the activation//                      of each hidden (e.g., sigmoid, tanh, Heaviside)


There's nothing more to it (other than your implementation of your VECTOR and MATRIX classes). You can add more layers by treating the output of each layer as the input to the next layer (so just add more layer matrices W_j and bias vectors of appropriate sizes).

Now, the interesting problem is how to train it (i.e., learn the values of the matrices W_i and W_o and the bias vectors b_i and b_o)! ;)

Cheers,

Timkin

This topic is closed to new replies.

Advertisement