%assoc_mem_demo.m
%John Wetters
%4/8/09
%autoassociative memory p7-10 Neural Network Design
delete diary.txt
diary diary.txt
close all
clear all
display('Do you want to caluculate weight matrix W with pseudoinverse?')
reply=input('y=yes n=no : ','s')
if isempty(reply)
reply = 'y';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1) generating the alphabetical target vectors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load alpha.mat %matrix of 35x26,7x5 alphabetical characters
[m,n] = size(alpha) %check matrix loaded correctly m=35 n=26
%A=alpha(:,1); %A is the first column of alpha
%A to Z are the columns of alpha
%plot out A
A=alpha(:,1); %A is the first column vector
plot_vector7_5(A)
title('7x5 plot of "A"')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2) calculating the weight matrix W with the pseudoinverse
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pinv_alpha=pinv(alpha);%take the pseudoinverse of alpha to make orthogonal matrix to reduce errors
[m,n] = size(pinv_alpha)
if (strcmp(reply,'y'))
W=alpha*pinv_alpha;%calculate weight vectors W using alpha*pseudoinverse
PlotRealMatrix(W)
title('weight matrix with psuedoinverse')
else
%%%%%%%%%%%%%%%%%%%%%%%
%5) comparing to the results without using the pseudoinverse
%this is a test to see what happens without pseudoinverse
%%%%%%%%%%%%%%%%%%%%%%%
W=alpha*alpha';
PlotRealMatrix(W)
title('weight matrix without psuedoinverse')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%3) testing the autoassociative memory without noise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
error_save=[]
%plot without noise
for i=1:26
a=hardlims(W*alpha(:,i));
t=alpha(:,i);
error=sum(abs(t-a)) %calculate number of errors
error_save=[error error_save];
% plot_vector7_5(t) %plot target
% title('target')
%
% plot_vector7_5(a) %compare to simulated output
% title('simulated')
end
figure
plot(error_save)
title('error to target without noise')
xlabel('aphabetic character number')
ylabel('errors in character')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%4) testing the autoassociative memory with noise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
error_save=[]
noise_save=[]
noise_range = .3:.01:1;
for noiselevel = noise_range
%plot noise
P = alpha + randn(35,26)*noiselevel;
for i=1:26
in=P(:,i); %input has noise
% test=W*t;
% plot_vector7_5(test)
% title(['with noise without limiting ' num2str(noiselevel)])
a=hardlims(W*in);
% plot_vector7_5(a)
%title(['with noise with limiting' num2str(noiselevel)])
t=alpha(:,i);
error=sum(abs(t-a)) %calculate number of errors
error_save=[error error_save];
noise_save=[noiselevel noise_save];
end
end
figure
plot(noise_save,error_save,'rx')
title('error to target vs noise level')
xlabel('noise level')
ylabel('number of errors in target')
diary
edit diary.txt