![](https://csdnimg.cn/release/download_crawler_static/86138117/bg1.jpg)
Simple Neural Network.
The enclosed M-files implement a very simple 2 layer neural network.
The three M-files are:
• NN2weights
• NN2train
• NN2
NN2weights - creates the initial random weights used in the training process
InitWeights=NN2weights(netsize)
netsize is a vector contiaining the number of input units and output units.
If you have a network with two input units and one neuron netsize = [ 2 1]
InitWeights is a structure containing four fields.
InitWeights.inputs is the weights connecting the input layer to the output neurons.
The weights associated with a specific neuron are stored along the rows, so if you had a network with two inputs; I1
and I2,
and two outputs; O1 and O2, with connecting weights W11, W12, W21, W22 with the first number being the input
node, and the
second number being the output node. the weight matrix would be stored as
InitWeights.inputs=[W11 W21;W12 W22]
This makes the computation of the activation function much simpler.
NN2 - implements the neural network
OutputValue=NN2(trainedweights,inputvector,af)
trainedweights - weight values that implement the desired function of the neural network
input vector - column vector containing the input of the neural network
af - activation function wanting to be used
'p' for perceptron learning rule
anything else for logisitic learning rule
Lets assume we have trained our network to implement the logical AND function with two inputs and we wish to use
that network
the function call would be
result=NN2(tweights,[1 1]','p')
NN2train - function used to train the two layer neural network
trainedweights=NN2train(InitWeights,TDIN,TDOUT,LR,ErTh,af)
InitWeights -obtained by using the function NN2weights(netsize)
LR - Learning rate of the neural network
ErTh - Error Threshold, the value for which the training algorithm tries to achieve by altering the weights
af - activation function to be used in training; 'p' for perceptron or anything else for the logistic function
TDIN - input training data
TDOUT - output training data
To continue with the above AND function
TDIN = [0 0; 0 1; 1 0; 1 1]'
TDOUT = [0 0 0 1]
LR=.1
ErTh = .01
af = 'p'
trainedweights=NN2train(InitWeights,TDIN,TDOUT,LR,ErTh,af)
These trained weights can then be used to run the neural network.