%The Matlab Code for pages 46-48 in our text, for the Associative Memory %example. A{1}=[1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1 -1 1 -1 -1]; A{2}=[-1 1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1 -1 1 -1]; A{3}=[1 1 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 1 -1]; A{4}=[-1 1 1 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 1]; A{5}=[1 1 1 -1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1 -1]; A{6}=[-1 1 1 1 -1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1]; T1=A{1}; T2=A{2}; G1=A{3}; G2=A{4}; F1=A{5}; F2=A{6}; gg=colormap(gray); gg=gg(end:-1:1,:); subplot(2,3,1) imagesc(T1) colormap(gg) subplot(2,3,2) imagesc(G1) subplot(2,3,3) imagesc(F1) subplot(2,3,4) imagesc(T2) subplot(2,3,5) imagesc(G2) subplot(2,3,6) imagesc(F2) fprintf('Training patterns are shown. Press any key to continue...\n'); pause %************************************** %The main code starts here! %************************************** X=[T1(:) T2(:) G1(:) G2(:) F1(:) F2(:)]; T=[60 60 0 0 -60 -60]; alpha=0.03; W=randn(1,16); b=0; TotalErr=0; NumPoints=6; for k=1:60 idx=randperm(6); for j=1:NumPoints ThisOut=W*X(:,idx(j))+b; ThisErr=T(idx(j))-ThisOut; %Update the weights and biases W=W+alpha*ThisErr*X(:,idx(j))'; b=b+alpha*ThisErr; end EpochErr(k)=norm((W*X+b*ones(1,6))-T); end %Run some samples with errors: close all for j=1:15 k=ceil(rand*6); TT=A{k}+0.7*randn(size(A{k})); h=W*TT(:)+b; imagesc(TT); colormap(gg); fprintf('Test item is shown. Your Guess is?\n'); pause fprintf('The current (rounded) output is: %d\n',round(h)); if round(h)>30 fprintf('Matlab Says: T\n'); elseif round(h)<30 & round(h)>-30 fprintf('Matlab Says: G\n'); else fprintf('Matlab Says: F\n'); end if k==1 | k==2 fprintf('The selected map was actually T\n\n'); elseif k==3 | k==4 fprintf('The selected map was actually G\n\n'); elseif k==5 | k==6 fprintf('The selected map was actually F\n\n'); else fprintf('Error\n'); end pause end