You are on page 1of 6

Problem Sheet for

Soft-Computing, Artificial Intelligence and Neural Network Lab (EE 753/2)

Develop the following programs in the MATLAB environment:

1. Develop a Competitive Learning Neural Network to classify the following patterns into required
number of groups. The number of groups should be flexible and may change according to the
input data set.

X1 = [1 2 3 4 5]
X2 = [1.1 2.1 3.1 4.1 5.1]
X3 = [2 3 4 5 6]
X4 = [2.1 3.1 4.1 5.1 6.1]
X5 = [3 4 5 6 7]
X6 = [3.1 4.1 5.1 6.1 7.1]

Now consider a few unknown test patterns. Find the group in which these test patterns are classified
and verify your results.

2. Develop a LVQ Neural Network to classify the following patterns into three predetermined groups
as: {X1, X2} Group1, {X3, X4} Group 2, {X5, X6} Group 3 and {X7} Group 4

Where,
X1 = [0.1 0.2 0.3 0.4 0.5]
X2 = [0.11 0.22 0.33 0.44 0.55]
X3 = [1 2 3 4 5]
X4 = [1.1 2.1 3.1 4.1 5.1]
X5 = [10.1 12.1 13.1 14.1 15.1]
X6 = [10.2 12.2 13.2 14.2 15.2]
X7 = [20.0 30.0 40.0 50.0 60.0]

Now consider a few unknown test patterns. Find the group in which these test patterns are classified
and verify your results.

Page 1 of 6
Outline of the Program Structures

1. Pattern Classification by Competitive Learning Neural Network:

MATLAB Code:

clc;
% Consider FOUR input patterns as follows:
%
% X1 = [1 2 3 4 5]
% X2 = [1.1 2.1 3.1 4.1 5.1]
% X3 = [2 3 4 5 6]
% X4 = [2.1 3.1 4.1 5.1 6.1]
% X5 = [3 4 5 6 7]
% X6 = [3.1 4.1 5.1 6.1 7.1]
%
% Represent these input patterns in Matrix form:
%
x = [1.0 2.0 3.0 4.0 5.0;
1.1 2.1 3.1 4.1 5.1;
2.0 3.0 4.0 5.0 6.0;
2.1 3.1 4.1 5.1 6.1;
3.0 4.0 5.0 6.0 7.0;
3.1 4.1 5.1 6.1 7.1];
%
% However, data has to be input colum wise to the ANN, and hence
% transposition is necessary
%
q = x'
%
% You can now verify the transposed input data (which is in a column
% wise format)
% q=
% 1.0000 1.1000 2.0000 2.1000 3.0000 3.1000
% 2.0000 2.1000 3.0000 3.1000 4.0000 4.1000
% 3.0000 3.1000 4.0000 4.1000 5.0000 5.1000
% 4.0000 4.1000 5.0000 5.1000 6.0000 6.1000
% 5.0000 5.1000 6.0000 6.1000 7.0000 7.1000
%
% Visibly, there are three distinct classes in the input data.
%
% *********************************************************************
% ***********Creating the Competitive Learning ANN Architecture ******
% ************************************************************************
% To classify these 6 input patterns, create a two layer
% Competitive Learning ANN with FIVE input elements
% (the number of input neurons must be same as the dimension of the input data)
% ranging from 1.0 to 7.1 (the overall min-max range of the data)
%
% Note that, in the "newc" the first argument indicates the ranges of each of the FIVE nput elements,
% and the second argument says that there are to be two neurons in the output.

Page 2 of 6
% Note that the number of output neurons decides the maximum number of
% output classes to be created by ANN. In this case there are THREE distinct
% classes to be created...the network created can be visualized by
% view(net) command.
%
net = newc([1.0 7.1; 1.0 7.1; 1.0 7.1; 1.0 7.1; 1.0 7.1],3);
view(net)
%
% The weights are initialized to the centres of the input ranges with the function midpoint.
%
wts1 = net.IW{1,1}
%
% You can check to see these initial values as follows:
% wts =
% 4.0500 4.0500 4.0500 4.0500 4.0500
% 4.0500 4.0500 4.0500 4.0500 4.0500
% 4.0500 4.0500 4.0500 4.0500 4.0500
%
% These weights are indeed the values at the midpoint of the range (0 to 1) of the inputs,
% as you would expect when using midpoint for initialization.
%
% Now you have a network, but you need to train it to do the classification.
% Each neuron competes to respond to an input vector.
% If the biases are all 0, the neuron whose weight vector is closest to p gets the highest net input
% and, therefore, wins the competition and outputs 1.
% All other neuron's output is 0. You want to adjust the winning neuron so as to move it closer to the input.
%
% *******************************
% Training
% *******************************
% Now train the network for 50 epochs. You can use either train or adapt.
%
net.trainParam.epochs = 500
net = train(net,q);
%
% For each epoch, all training vectors (or sequences) are each presented once in a different random order
% with the network and weight and bias values updated after each individual presentation.
% Next, supply the original vectors as input to the network, simulate the network, and
% finally convert its output vectors to class indices.
%
a = sim(net,q);
class_index = vec2ind(a)
%
% This yields
% class_index =
%
% 1 1 2 2 3 3
%
% Note that the network is trained to classify the input vectors into two groups,
% First two vectors are put into class 1, next two vectors are put into
% class 2. Last two vectors are put in group 3.
% It might be interesting to look at the final weights and biases.
wts1 = net.IW{1,1}
%
Page 3 of 6
% Now the generalization ability of the classifier is tested with unknown
% input patterns.
%
q1= [1.2 2.2 3.2 4.2 5.2;
2.2 3.2 4.3 5.2 6.2;
3.2 4.2 5.2 6.2 7.2];
% Now simulate the network with the new unknown inputs and check in which
% class/ group these unknown inputs are classified.
a = sim(net, q1');
class_index = vec2ind(a)
% THe final classification results are
% class_index =
%
%1 2 3

2. Pattern Classification by LVQ Neural Network:

MATLAB Code:

clc;
% Consider input patterns as follows:

% X1=[0.1 0.2 0.3 0.4 0.5];


% X2=[0.11 0.22 0.33 0.44 0.55];
% X3=[1 2 3 4 5];
% X4=[1.1 2.1 3.1 4.1 5.1]
% X5=[10.1 12.1 13.1 14.1 15.1]
% X6=[10.5 12.3 13.6 14.5 15.9]
% X7=[11.1 12.5 13.7 13.9 16.1]
%
% Represent these input patterns in Matrix form:
%
x = [0.1 0.2 0.3 0.4 0.5;
0.11 0.22 0.33 0.44 0.55;
1.0 2.0 3.0 4.0 5.0;
1.1 2.1 3.1 4.1 5.1;
10.1 12.1 13.1 14.1 15.1;
10.2 12.2 13.2 14.2 15.2;
20.0 30.0 40.0 50.0 60.0];
% However, data has to be input colum wise to the ANN, and hence
% transposition is necessary
%
q = x'
%
% You can now verify the transposed input data (which is in a column
% wise format)
%
% Visibly, there are FOUR distinct classes in the input data.
Page 4 of 6
% The objective is now to classify these 4 input pattrens into 4
% pre-defined classes (let us call them class 1, class 2, class 3, class 4).
%
% Since LVQ is a supervised learning algorithm, the Target output classes
% for each vector has to be defined.
% In our example,1st and 2nd input vector belong to Class 1,
% 3rd and 4th vector belong to Class 2,
% 5th and 6th vector belongs to class 3
% 7th vector belongs to class 4
% Let us now define the target classes for the 7 input vectors as follows:

Tc=[1 1 2 2 3 3 4]
%
% Now use the command "newlvq" with the proper arguments
% Note that the 1st argument defined the range of input vector, followed
% by number of hidden layer neurons (chosen as 10). The third set of
% arguments stand for the class percentage of the input vectors. Here 2
% input vetors belong to class 1, 2 vectors belong to
% class 2, another 2 vectors belong to class 3, but just 1 vector belongs
% to class 4. Hence the class representation of the vectors are 2/7, 2/7,
% 2/7 and 1/7
%
net = newlvq(minmax(q),20,[2/7 2/7 2/7 1/7]);
view(net)
%
% Next convert the Tc matrix to target vectors.
%
T = ind2vec(Tc)
%
% This gives a sparse matrix T that can be displayed in full with
%
targets = full(T)
%
% which gives
% targets =
% 1 1 0 0
% 0 0 1 1
%
% *******************************
% Training
% *******************************
% Now train the network for 200 epochs. You can use either train or adapt.
%
net.trainParam.epochs = 200;
net = train(net,q,T);
% After training is completed, Simulate the network with the same input set
% to verify the success of training
a = sim(net,q);
% Output Class index of the input patterns can be checked as:
%
class_index = vec2ind(a)
%
% This yields
% class_index =
Page 5 of 6
%
% 1 1 2 2 3 3 4
%
% Note that the network is trained to classify the input vectors into two pre-defined groups,
% First two vectors are put into class 1, and the other two vectors are put into class 2.
%
% Now it is necessary to chechk the classification accuracy for unseen
% vectors. Let us choose three unseen input vectors as:
%
q1= [1.2 2.2 3.3 4.4 5.5;
10.1 12.3 13.5 14.4 15.3;
21 31 41 51 61];
%
% Now simulate the network with these new unknown inputs
a = sim(net,q1');
%
% which should belong to Class 2, 3 and 4 respectively
% Output Class index of this unseen input patterns can be checked as:
%
class_index = vec2ind(a)
%
% This yields
% class_index = 2 3 4

Page 6 of 6

You might also like