Lecture 14 - Associative Neural Networks Using Matlab
Lecture 14 - Associative Neural Networks Using Matlab
Qadri Hamarsheh
1
Dr. Qadri Hamarsheh
Example 2: Write an M–file to store the vectors (–1 –1 –1 –1) and (–1 –1 1 1) in
an auto associative net. Find the weight matrix. Test the net with (1 1 1 1) as
input.
The MATLAB program for the auto association problem is as follows:
Program
clc;
clear;
x=[–1 –1 –1 –1;–1 –1 1 1];
t=[1 1 1 1];
w=zeros (4, 4);
for i=1:2
w=w + x(i,1:4)'*x(i,1:4);
end
yin = t*w;
for i=1:4
if yin(i)>0
y(i)=1;
else
y(i)=–1;
end
end
disp ('The calculated weight matrix');
disp (w);
if x(1,1:4)==y(1:4) | x(2,1:4)==y(1:4)
disp ('The vector is a Known Vector');
else
disp ('The vector is a unknown vector');
end
Output
The calculated weight matrix
2 2 0 0
2 2 0 0
0 0 2 2
0 0 2 2
The vector is an unknown vector.
Example 3: Write an M–file to calculate the weights for the following
patterns using hetero associative neural net for mapping four input vectors
to two output vectors.
S1 S2 S3 S4 t1 t2
1 1 0 0 1 0
1 0 1 0 1 0
1 1 1 0 0 1
0 1 1 0 0 1
2
Dr. Qadri Hamarsheh
Solution
%Hetero associative NN for mapping input vectors to output vectors
clc;
clear;
x= [1 1 0 0; 1 0 1 0; 1 1 1 0; 0 1 1 0];
t= [1 0; 1 0; 0 1; 0 1];
w=zeros (4, 2);
for i=1:4
w=w+x (i, 1:4)'*t(i,1:2);
end
disp('Weight matrix');
disp(w);
Output
Weight matrix
2 1
1 2
1 2
0 0
Example 4: Write a MATLAB program to store the vector (1 1 1 0) in bipolar
binary form and calculate the weight matrix for Hopfield net.
%The MATLAB program for calculating the weight matrix is as follows
%Discrete Hopfield net
clc;
clear;
x=[1 1 1 0];
w=(2*x'–1)*(2*x–1);
for i=1:4
w (i, i)=0;
end
disp('Weight matrix');
disp(w);
Output
Weight matrix
0 1 1 -1
1 0 1 -1
1 1 0 -1
-1 -1 -1 0
Example 5: Auto-associative Memories (continuous):
%Auto-associative Memories (continuous)
x1=[-0.3; 0.9; -0.2];
x2=[0.44; -0.7; 0.9];
x3=[0.9; 0.6; 0.8];
Total_M = x1*x1' + x2*x2' + x3*x3';
estimate_x1= Total_M *x1;
estimate_x2= Total_M *x2;
estimate_x3= Total_M *x3;
%Estimates are not perfect because of non-orthogonality of the vectors
3
Dr. Qadri Hamarsheh
4
Dr. Qadri Hamarsheh
x1test=sign (w*x1');
x2test=sign (w*x2');
x3test=sign (w*x3');
%Convergence and attractors.
%Can the memory recall the stored patterns from distorted inputs
% patterns? Define a few new patters which are distorted versions of
%the original ones:
x1d= [1 0 1 0 1 0 0 1];
x2d= [1 1 0 0 0 1 0 0];
x3d= [1 1 1 0 1 1 0 1];
%x1d has a one bit error, x2d and x3d have two bit errors.
x1d=[1 -1 1 -1 1 -1 -1 1];
x2d=[1 1 -1 -1 -1 1 -1 -1];
x3d=[1 1 1 -1 1 1 -1 1];
Y will be the same. (Since Hopfield networks have no inputs, the second
argument to sim is Q = 2 when using matrix notation).
Ai = T;
[Y, Pf, Af] = sim (net, 2, [ ], Ai);
%Y
To see if the network can correct a corrupted vector, run the following
code, which simulates the Hopfield network for five time steps. (Since
Hopfield networks have no inputs, the second argument to sim is {Q TS} =
[1 5] when using cell array notation.)
Ai = {[-0.9; -0.8; 0.7]};
[Y, Pf, Af] = sim (net, {1 5}, { },Ai);
%Y{1}
If you run the above code, Y{1} will equal T(:,1) if the network has
managed to convert the corrupted vector Ai to the nearest target vector.
Description of sim function
Purpose: simulate a neural network.
Syntax:
[Y, Pf, Af, E, perf] = sim (net, P, Pi, Ai, T)
[Y, Pf, Af, E, perf] = sim (net, {Q TS}, Pi, Ai, T)
[Y,Pf,Af,E,perf] = sim(net,Q,Pi,Ai,T)
Description sim simulates neural networks.
[Y, Pf, Af, E, perf] = sim (net, P, Pi, Ai, T) takes,
net - Network.
P - Network inputs.
Pi - Initial input delay conditions, default = zeros.
Ai - Initial layer delay conditions, default = zeros.
T - Network targets, default = zeros.
and returns,
Y - Network outputs.
Pf - Final input delay conditions.
Af - Final layer delay conditions.
E - Network errors.
perf- Network performance.
Note that arguments Pi, Ai, Pf, and Af are optional and need only be used for
networks that have input or layer delays.
sim’s signal arguments can have two formats: cell array or matrix.