Neural Network in MATLAB
Neural Network in MATLAB
Network
www.techsource.com.my
www.techsource.com.my
Course Outline:
1. Neural Network Concepts
a) Introduction
b) Simple neuron model
c) MATLAB representation of neural network
Section Outline:
1. Introduction
Definition of neural network
Biological perspective of neural network
Neural network applications
Inputs Outputs
Aerospace Insurance
Automotive Manufacturing
Banking Medical
Credit Card Activity Oil & Gas
Checking Robotics
Defense Speech
Electronics Securities
Entertainment Telecommunications
Financial Transportation
Industrial
Cell
Dendrites Body Axon
p1
w1
p2 w2
(Inputs) w3 a (Output)
p3
• wR
•
• b Bias
pR
Synapses
1
p1 Transfer
w1 Summation Function
p2
w2
Σ ƒ
n a
(Inputs) p3 w3
(Output)
•
• wR
•
b
pR
1
Example:
p1
w1
Σ
n a
Inputs w2 Output
p2
b
1
If p1 = 2.5; p2 = 3; w1 = 0.5; w2 = -0.7; b = 0.3. Let’s assume the transfer
function of the neuron is hardlimit, where,
0 for n < 0
a = hardlim(n) =
1 for n ≥ 0
MATLAB® Representation of
the Simple Neuron Model
p a1 a2 a3 = y
R×1
IW1,1 LW2,1 LW3,2
n1 S1 × 1 n2 S2 × 1 n3 S3 × 1
S1 × R S2 × S1 S3 × S2
S1 × 1
f1 S2 × 1
f2 S3 × 1
f3
1 b1 1 b2 1 b3
R S1 S2 S3
S1 × 1 S2 × 1 S3 × 1
Example:
Neural network with 2 layers. 1st layer (hidden layer) consists of 2 neurons
with tangent-sigmoid (tansig) transfer functions; 2nd layer (output layer)
consists of 1 neuron with linear (purelin) transfer function.
p1 iw1,11,1 iw1,11,2
b11 b21
p= IW1,1 = b1 = LW2,1 = lw2,11,1 lw2,11,2 b2 =
p2 b12
iw1,1 2,1 iw1,1 2,2
purelin(n) = n
∴ a2 = purelin(LW2,1 tansig(IW1,1p + b1) + b2) =y
For,
0.3 -0.7
1 0.1
p= IW1,1 = b1 = 0.2
LW2,1 = 0.1 -0.2 b2 = 0.3
2 -0.2 0.5
Section Summary:
1. Introduction
Definition of neural network
Biological perspective of neural network
Neural network applications
Section Outline:
1. Perceptrons 4. Self-Organizing Maps
Introduction Introduction
The perceptron architecture Competitive learning
Training of perceptrons Self-organizing maps
Application examples Application examples
2. Linear Networks
Introduction
Architecture of linear networks
The Widrow-Hoff learning algorithm
Application examples
3. Backpropagation Networks
Introduction
Architecture of backprogation network
The backpropagation algorithm
Training algorithms
Pre- and post-processing
Application examples
©2005 Systems Sdn. Bhd.
Perceptrons
p2 p2
p1
p1
p1
p2 w1 Summation Hardlimit
w2
Σ
p3 w3 n a Output
Inputs
•
• wR
•
b
pR
1
MATLAB® Representation of
the Perceptron Neuron
Σ
n11 a11
iw1,11,1
Input Single Layer of S Neurons Output
b11
p1 1 p
IW1,1
R×1 n1 a1
S1 × R
Σ
n12 a12
S1 × 1
S1 × 1
p2 1 b1
b12 • • S1
• S1 × 1
• 1 • • R
• • •
Σ
n1S1 a1S1
pR
iw1,1S1,R
b1S1
1
a1 = hardlim(IW1,1p + b1)
©2005 Systems Sdn. Bhd.
Creating a Perceptron:
Command-Line Approach
[-2:2] p1 w1 p
IW1,1
2×1 n1 a1
n a
Σ 1×2
1×1 1×1
[-2:2] p2 w2 b1
1
b 1
1×1
2
1
% Creating a perceptron
>> net = newp([-2 2; -2 2], 1);
% Note that initial weights and biases are initialized to zeros using “initzero”
>> net.inputWeights{1,1}.initFcn
>> net.biases{1}.initFcn
% To compute the output of perceptron from input vectors [p1; p2], use the
“sim” command
>> p = [ [2; 2] [1; -2] [-2; 2] [-1; 1] ]
>> a = sim(net, p)
>> a =
1 1 1 1
This is done by adjusting the weights (W) and biases (b) of the
perceptron network according to following equations
Training of Perceptron
If the Perceptron Learning Rule is used repeatedly to adjust the
weights and biases according to the error e, the perceptron wil
eventually find weight and bias values that solve the problem,
given that the perceptron can solve it.
Example:
We can train a Perceptron network to classify two groups of data, as
illustrated below
Data x1 x2 Group
x2
p1 -3 -0.5 0
p4 Gr
ou p2 -2 -1.2 0
p
p6 1
p3 p3 -1.5 0.7 1
p8
x1 p4 -1 3 1
p1
p5 -1 -3.5 0
Gr p2
ou p7 p6 0 2 1
p
0 p5
p7 0 -2.5 0
p8 1 0.7 1
Procedures:
% Load the data points into Workspace
>> load data
% Train the perceptron network with training inputs (p) and targets (t)
>> net = train(net, p, t)
First, define the training inputs by clicking “Import…”, select group from
the list of variables. Assign a name to the inputs and indicate that this
variable should be imported as inputs.
Define the targets similarly.
Next, select the “Train…” tab and set Inputs to p and Targets to t. Click on
“Train Network” to start the training.
X Y X AND Y
0 0 0
0 1 0
1 0 0
1 1 1
Solution:
Command-line approach is demonstrated herein. The “nntool” GUI can be used alternatively.
% Define at the MATLAB® command window, the training inputs and targets
>> p = [0 0 1 1; 0 1 0 1]; % training inputs, p = [p1; p2]
>> t = [0 0 0 1]; % targets
Solution:
Command-line approach is demonstrated herein. Tne “nntool” GUI can be used alternatively.
% Define at the MATLAB® command window, the training inputs and targets
>> load train_images
>> p = [img1 img2 img3 img4];
>> t = targets;
Linear Networks
p1
p2 w1 Summation Linear
w2
Σ
p3 w3 n a Output
Inputs
•
• wR
•
b
pR
1
MATLAB® Representation of
the Linear Neuron
a = purelin(n) = n
a = purelin(Wp + b)
Σ
n11 a11
iw1,11,1
Input Layer of S1 Linear Neurons Output
b11
p1 1 p
IW1,1
R×1 n1 a1
S1 × R
Σ
n12 a12
S1 ×1
S1 × 1
p2 1 b1
b1 • • S1
• 2
S1 × 1
• 1 • • R
• • •
Σ
n1S1 a1S1
pR
iw1,1S1,R
b1S1
1
a1 = purelin(IW1,1p + b1)
©2005 Systems Sdn. Bhd.
[-2:2] p1 w1 p
IW1,1
2×1 n1 a1
n a
Σ 1×2
1×1 1×1
[-2:2] p2 w2 b1
1
b 1
1×1
2
1
% Note that initial weights and biases are initialized to zeros using “initzero”
>> net.inputWeights{1,1}.initFcn
>> net.biases{1}.initFcn
% To compute the output of linear network from input vectors [p1; p2], use
the “sim” command
>> p = [ [2; 2] [1; -2] [-2; 2] [-1; 1] ]
>> a = sim(net, p)
>> a =
0 0 0 0
The LMS algorithm adjusts the weights and biases of the linear
networks to minimize the mean square error (MSE)
1 Q 1 Q
∑ e ( k ) = ∑ ( t ( k ) − a ( k ))
2 2
MSE =
Q k =1 Q k =1
Example:
Let’s re-visit Exercise 2: Pattern Classification of the Perceptrons.
We can build a Linear Network to perform not only pattern classification but
also association tasks.
0 1 0
Group 1 0 1
0 1 0 1
0 1 0
img1 img3 timg1 timg2
Group
1
Solution:
Command-line approach is demonstrated herein. Tne “nntool” GUI can be used alternatively.
% Define at the MATLAB® command window, the training inputs and targets
>> load train_images
>> p = [img1 img2 img3 img4];
>> t = targets;
% Now, test the Linear Network with 3 images not seen previously
>> load test_images
>> test1 = sim(net, timg1)
>> test1 =
0.2271
>> test2 = sim(net, timg2)
>> test2 =
0.9686
>> test3 = sim(net, timg3)
test3 =
0.8331
©2005 Systems Sdn. Bhd.
How should we interpret the network outputs test1, test2 and test3? For that we
need to define a Similarity Measure, S
S = t − test
Where t is the target-group (i.e. 0 or 1) and test is the network output when
presented with test images.
The smaller the S is, the more similar is a test image to a particular group.
Group 0 Group 1
T = [1 1 1 U = [1 0 1
010 101
0 1 0]’ 1 1 1]’
>> load train_letters
U_odd = [1 1 1 T_odd = [1 1 1
101 010
1 1 1]’ 0 1 1]’
Solution:
Command-line approach is demonstrated herein. Tne “nntool” GUI can be used alternatively.
% Define at the MATLAB® command window, the training inputs and targets
>> load train_letters
>> p = [T U];
>> t = targets;
iw1,11,1 n11
p1
iw1,12,1
Σ lw2,11,1
b1 n21 a21
lw2,12,1
Σ
1
1
p2 n12
Inputs Σ 1
b21 Outputs
b12
1
Σ
n22 a22
• •
• •
• •
b22
n1S1
Σ
lw2,12,S1 1
pR
iw1,1S1,R
b1S1
1
MATLAB® Representation of
the Feedforward BP Network
tansig purelin
p a1 a2 = y
R×1 IW1,1 S1 ×1 LW2,1 2×1
S1 × R n1 2 × S1
n2
S1 × 1 2×1
1 b1 1 b2
S1 × 1 2×1
R S1 2
+1 Linear
logsig(n) = 1 / (1 + exp(-n)) a
n
0 +1
-1
n
0
Tangent-Sigmoid
a -1
+1 purelin(n) = n
tansig(n) = 2/(1+exp(-2*n))-1
n
0
-1
©2005 Systems Sdn. Bhd.
δ k = − ak ( 1 − ak )( tk − ak )
Please see
For hidden neuron h, Appendix 1 for
full derivation of
δ h = a h ( 1 − ah ) ∑ δ kwk , h the algorithm
k∈ Downstream ( h )
Faster Training
The MATLAB® Neural Network Toolbox also implements some
of the faster training methods, in which the training can
converge from ten to one hundred times faster than traingd
and traingdm.
Function Description
premnmx Normalize data to fall into range [-1 1].
postmnmx Inverse of premnmx. Convert data back into original range of values.
Preprocess new inputs to networks that were trained with data
tramnmx
normalized with premnmx.
prestd Normalize data to have zero mean and unity standard deviation
poststd Inverse of prestd. Convert data back into original range of values.
Preprocess new inputs to networks that were trained with data
trastd
normalized with prestd.
prepca Principal component analysis. Reduces dimension of input vector
Preprocess new inputs to networks that were trained with data
trapca
transformed with prepca.
Linear regression between network outputs and targets. Use to
postreg
determine adequacy of network fit.
X Y X XOR Y
0 0 0 Y
0 1 1
1 0 1 1 0
1 1 0
p1 p2 a
0 0 0 0 1
0 1 0
X
1 0 0
1 1 1
©2005 Systems Sdn. Bhd.
Solution:
Command-line approach is demonstrated herein. Tne “nntool” GUI can be used alternatively.
% Define at the MATLAB® command window, the training inputs and targets
>> p = [0 0 1 1; 0 1 0 1];
>> t = [0 0 0 1];
1. Training set
2. Validation set
3. Testing set
>> net.trainParam.show = 1;
>> net.trainParam.epochs = 300;
% Then, train the network with early stopping with both Validation & Test sets
>> net = init(net);
>> [net, tr] = train(net, p, t, [], [], val, test);
>> net2 = net; % network with early stopping
∴Network with early stopping can better fit the Test data set with
less discrepancies, therefore the early stopping feature can be
used to prevent overfitting of network towards the training data.
©2005 Systems Sdn. Bhd.
Network details:
Architecture: 24-48-24 network, with tansig TF and purelin TF in hidden and
output layer respectively.
Training: trainlm algorithm with 7 epochs and plot the performance function
every 1 epoch.
Data details:
Load timeseries.mat into MATLAB® workspace.
Training Data (1st to 37th days): TrainIp (inputs), TrainTgt (targets)
Testing Data (38th to 40th days): TestIp (query inputs), TestTgt (actual values)
Solution:
% Load the Time Series data into MATLAB® Workspace
>> load timeseries
% Prepare the data for testing the network (predicting 38th to 40th days)
>> PN_Test = tramnmx(TestIp,minp,maxp);
% Convert the testing output into prediction values for comparison purpose
>> [queryInputs predictOutputs] = postmnmx(PN_Test, minp, maxp, …
TN_Test, mint, maxt);
Homework: Try to subdivide the training data [TrainIp TrainTgt] into Training
& Validation Sets to accertain whether the use of early stopping would
improve the prediction accuracy.
A
A G
W P
Q Z
0 0 1 0 0 Letter A = [0 0 1 0 0 … Target A = [1 0 0 0 0 …
0 1 0 1 0 01010… 00000…
01010… 00000…
0 1 0 1 0 00000…
10001…
1 0 0 0 1 11111… 00000…
10001… 0]’;
1 1 1 1 1
1 0 0 0 1]’;
1 0 0 0 1
1 0 0 0 1
0 1 0 A B C
1 1 0
1 1 1 1 0 0
Inputs 1 1 0 0 1 0 Outputs
Neural
0 0 1
(Alphabets) (Targets)
Network
1 1 0
0 0 0 0
1 1
0 0 0 0
1 1
0 0 1
Network details:
Architecture: 35-10-26 network, with logsig TFs in hidden and output layers.
Training: traingdx algorithm with 500 epochs and plot the performance
function every 1 epoch. Performance goal is 0.001.
Data details:
Load training inputs and targets into workspace by typing
[alphabets, targets] = prprob;
% Next, we create a noisy ‘J’ to test the network can still identify it correctly…
>> noisyJ = alphabets(:,10)+randn(35,1)*0.2;
>> figure; plotchar(noisyJ);
>> output2 = sim(network1, noisyJ);
>> output2 = compet(output2);
>> answer2 = find(compet(output2) == 1);
>> figure; plotchar(alphabets(:,answer2));
Self-Organizing Maps
Self-organizing in networks is one of the most fascinating
topics in the neural network field. Such networks can learn to
detect regularities and correlations in their input and adapt their
future responses to that input accordingly.
Competitive Learning
S1 × R IW1,1
a1
p S1 × 1
|| ndist || S1 × 1
R×1 n1
S1 × 1
C
1 b1
R S1 × 1
S1
Supposed that the ith neuron wins, the elements of the ith row of
the input weight matrix are adjusted according to following
formula:
iIW
1,1(q) = iIW1,1(q-1) + α(p(q) – iIW1,1(q-1))
p(1)
Let’s see whether the competitive
network is able to identify the
classification structure…
% The weights are initialized to the center of input ranges with ‘midpoint’ fcn
>>net.IW{1,1}
ans =
0.5 0.5
0.5 0.5
∴The network is able to classfy the input vectors into two classess, those
who close to (1,1), class 1 and those close to origin (0,0), class 2. If we look
at the adjusted weights,
>> net.IW{1,1}
ans =
0.8500 0.8500
0.1000 0.1501
∴Note that the first-row weight vector (associated with 1st neuron) is near to
input vectors close to (1,1), which the second-row weight vector (associated
with 2nd neuron) is near to input vectors close to (0,0).
Try to build a competitive network with 8 neurons and train for 1000
epochs. Superimpose the trained network weights onto the same figure.
Try to experiement with the number of neurons and conclude on the
accuracy of the classification.
©2005 Systems Sdn. Bhd.
Solution:
% Create and train the competitive network
>> net = newc([0 1; 0 1], 8, 0.1); % Learning rate is set to 0.1
>> net.trainParam.epochs = 1000;
>> net = train(net, P);
% Plot and compare the input vectors and cluster centres determined by the
competitive network
>> w = net.IW{1,1};
>> figure, plot(P(1,:),P(2,:),‘+r’);
>> hold on, plot(w(:,1), w(:,2), ‘ob’);
Self-Organizing Maps
Similar to competitive neural networks, self-organizing maps
(SOMs) can learn the distribution of the input vectors. The
distinction between these two networks is that the SOM can also
learn the topology of the input vectors.
The neighborhood Ni*(d) contains the indices for all the neurons
that lie within a radius d of the winning neuron i*.
Ni(d) = {j, dij ≤ d}
S1 × R IW1,1
p n1 a1
R×1
|| ndist ||
S1 × 1
C S1 × 1
S1
R
The weights of the SOM are updated using the learnsom function,
where the winning neuron’s weights are updated proportional to α
and the weights of neurons in its neighbourhood are altered
proportional to ½ of α.
Try alter the size of the SOM and learning parameters and draw
conclusion on how it affects the result.
©2005 Systems Sdn. Bhd.
Section Summary:
1. Perceptrons 4. Self-Organizing Maps
Introduction Introduction
The perceptron architecture Competitive learning
Training of perceptrons Self-organizing maps
Application examples Application examples
2. Linear Networks
Introduction
Architecture of linear networks
The Widrow-Hoff learning algorithm
Application examples
3. Backpropagation Networks
Introduction
Architecture of backprogation network
The backpropagation algorithm
Training algorithms
Pre- and post-processing
Application examples
©2005 Systems Sdn. Bhd.
8000 8500
Demand (MW)
7500 8000
Weekends Sat
Demand (MW)
7000 Spring 7500
5500 6000
5000 5500
4500 5000
0 5 10 15 20 25 30 35 40 45 50 0 5 10 15 20 25 30 35 40 45 50
Time in half-hourly records Time in half-hourly records
Note: NSW electricity demand data (1996 – 1998) courtesy of NEMMCO, Australia
©2005 Systems Sdn. Bhd.
Solution:
Step 1: Formulating Inputs and Outputs of Neural Network
By analysing the time-series data, a 3-input and 1-output neural network is
proposed to predict next-day hourly electricity demand,
Inputs: Output:
p1 = L(d,t) a1 = L(d+1, t)
p2 = L(d,t) - L(d-1,t)
p3 = Lm(d+1,t) - Lm(d,t)
Where,
L(d, t): Electricity demand for day, d, and hour, t
L(d+1, t): Electricity demand for next day, (d+1), and hour, t
L(d-1, t): Electricity demand for previous day, (d-1), and hour t
Lm(a, b) = ½ [ L(a-k, b) + L(a-2k, b)]
k = 5 for Weekdays Model & k = 2 for Weekends Model
©2005 Systems Sdn. Bhd.
www.techsource.com.my
The End
Kindly return your Evaluation Form