10. prolog %MEDICAL DIAGNOSTIC SYSTEM %CHILDHOOD DISEASES
%EXAMPLE ONLY NOT FOR MEDICAL USE domains disease=symbol
symptom=symbol query=symbol replay=symbol database
xpositive(symptom) xnegative(symptom) predicates nondeterm
hypothesis(disease) nondeterm symptom(disease) go
positive(query,symptom) clear_facts remember(symptom,replay)
ask(query,symptom,replay) clauses go:%clearwindow,
hypothesis(Disease),!, write("the patient probably has ",Disease),
clear_facts. positive(_,Symptom):xpositive(Symptom),!.
positive(Query,Symptom):not(xnegative(Symptom)),
ask(Query,Symptom,Replay), Replay="y". 10
11. ask(Query,Symptom,Replay):write(Query), readln(Replay),
remember(Symptom,Replay).
remember(Symptom,"y"):asserta(xpositive(Symptom)).
remember(Symptom,"n"):asserta(xnegative(Symptom)).
clear_facts:retract(xpositive(_)),fail.
clear_facts:retract(xnegative(_)),fail. clear_facts.
symptom(fever):positive("Dos the patient have the fever
(y/n)",fever). symptom(headache):positive("Dos the patient have the
headache (y/n)",headache). symptom(body_ache):positive("Dos the
patient have the body_ache (y/n)",body_ache).
symptom(conjuctvitis):positive("Dos the patient have the
conjuctvitis (y/n)",conjuctvitis). symptom(chills):positive("Dos
the patient have the chills (y/n)",chills).
symptom(sore_throat):positive("Dos the patient have the throat
(y/n)",throat). symptom(couph):positive("Dos the patient have the
couph (y/n)",couph). symptom(runny_nose):positive("Dos the patient
have the runny_nose (y/n)",runny_nose). symptom(rash):positive("Dos
the patient have the rash (y/n)",rash). 11
17. : . : the neuron as simple computing element . . . : sign
function step function Hard limit function sigmoid function linear
function soft limit function . ]5.0,5.0-[ 71
18. . . ) Yd(p : ,3,2,1=e(p) = Yd(p) Y(p) where p p p ) e(p )
Y(p ) .Y(p ) xi(p)* wi(p ) x(p ) xi(p ) wi(p ) . Y(p )xi(p ) wi(p .
:Preceptron learning rule )Wi(p+1)= wi(p) +xi(p) e(p , learning
rate : : w1,w2,,wn ]5.0,5.0-[ : ) x1(p),x2(p),xn(p ) . Yd(p 1=p n y
sign xi wi 1i n step 81
19. : : )Wi(p+1)= wi(p) +Wi(p )Wi(p)= xi(p) e(p ) Wi(p p : p .
91
24. . . : ,[-2.4/Fi ]2.4/Fi Fi i . . : ) x1(p),x2(p),xn(p
)Yd,1(p) , Yd,2(p) ,.,Yd,n(p : n y ( p ) sigmoid xi ( p) wij ( p )
j j 1i n j sigmoid : m y ( p ) sigmoid x jk ( p) w jk ( p) k k 1i
42
25. m k sigmoid : : )k(p)= yk(p)[1-yk(p)] ek(p : ,3,2,1=ek(p) =
Ydk(p) Yk(p) where p : )Wjk (p+1)= wjk(p) +Wjk (p )Wjk (p)= yj(p)
k(p : )Wjk (p+1)= wjk(p) + yj(p) k(p : I ) j ( p ) y j ( p) [1 y j
( p)] k ( p ) w jk ( p 1k )Wij (p+1)= Wij (p) + Wij (p ) Wij (p) =
Xi(p) j(p : p . 100.0 . 52
26. Code : % ================== % Filename: XOR_bp.m %
================== echo on; % Hit any key to define four 2-element
input vectors denoted by "p". pause p=[1 0 1 0;1 1 0 0] % Hit any
key to define ten 1-element target vectors denoted by "t". pause
t=[0 1 1 0] % Hit any key to define the network architecture. pause
s1=3; %Three neurons in the hidden layer s2=1; %One neuron in the
output layer % Hit any key to create the network and initialise its
weights and biases. pause net=newff([0 1;0
1],[s1,s2],{'tansig','purelin'},'traingd'); % Hit any key to set up
the frequency of the training progress to be displayed, % maximum
number of epochs, acceptable error, and learning rate. pause
net.trainParam.show=1; progress net.trainParam.epochs=1000;
net.trainParam.goal=0.001; net.trainParam.lr=0.1; % Number of
epochs between showing the % Maximum number of epochs % Performance
goal % Learning rate % Hit any key to train the back-propagation
network. pause net=train(net,p,t); % Hit any key to see whether the
network has learned the XOR operation. pause p=[1;1] a=sim(net,p)
26
27. % Hit any key to continue. pause p=[0;1] a=sim(net,p) % Hit
any key to continue. pause p=[1;0] a=sim(net,p) % Hit any key to
continue. pause p=[0;0] a=sim(net,p) echo off disp('end of XOR_bp')
Output : 27
28. 28
29. 29
30. : GUI 03
31. . . - sm . . . . recurrent . neural network the hopfiled
network . Output Signals 2y 2 2x yi i xi yn n xn Input Signals 1y 1
1x . 13
32. : n M Y1,Y2,,YM i j : M m1 y mi y mj , i j 0 ,i j w ij ymi
ymj i,j : M T m m w y y MI 1m : . ym . : xm , j y m , j , i
1,2,...n; m 1,2,.., M n ) y mi sign ( wij xmj i 1j ..... ..........
.......... .......... .......... .......... xm y m , m 1,2,..., M y
m sign ( wx m ), m 1,2,..., M ymi i ym xmj j xm . 23
33. . : n ) x (prob : xym m=1,2,,M )( : j=1,2,,n xj(0) = xj n
yi (0) sign( wij x j (0) i ).., i 1,2,..., n 1j )0( xj j x 0= yi(0)
p i p 0= p : ) y (0) sign( wx(0) )( )y(p : n ) yi ( p 1) sign( wij
x j ( p ) i 1j . : n yi ( p 1) sign( wij y j ( p ) i), i 1,2,..., n
1j : ) y ( p 1) sign( wy ( p ) 33
34. . . 43
35. Code (1): % ==================== % The Hopfield network %
==================== %
===========================================================================
% Reference: Negnevitsky, M., "Artificial Intelligence: A Guide to
Intelligent % Systems", Addison Wesley, Harlow, England, 2002. %
Sec. 6.6 The Hopeld network %
===========================================================================
% Hit any key to define two target fundamental memories to be
stored % in the network as the two columns of the matrix T. pause
T=[1 1 1;-1 -1 -1]' T= 1 -1 1 -1 1 -1 % Hit any key to plot the
Hopfield state space with the two fundamental memories % identified
by red markers. pause
plot3(T(1,:),T(2,:),T(3,:),'r.','markersize',20) axis([-1 1 -1 1 -1
1]); axis manual; hold on; title('Representation of the possible
states for the three-neuron Hopfield network') set(gca,'box','on');
view([37.5 30]); % Hit any key to obtain weights and biases of the
Hopfield network. pause 35
36. net=newhop(T); % Hit any key to test the network with six
unstable states represented as the % six-column matrix P. Unstable
states are identified by blue markers. pause P=[-1 1 1;1 -1 1;1 1
-1;-1 -1 1;-1 1 -1;1 -1 -1]' P= -1 1 1 -1 1 1 -1 -1 1 -1 1 -1 1 1
-1 1 -1 -1 for i=1:6 a = {P(:,i)}; [y,Pf,Af]=sim(net,{1 10},{},a);
record=[cell2mat(a) cell2mat(y)]; start=cell2mat(a);
plot3(start(1,1),start(2,1),start(3,1),'b*',record(1,:),record(2,:),record(3,:))
drawnow; % Hit any key to continue. pause a = {P(:,i)};
[y,Pf,Af]=sim(net,{1 10},{},a); record=[cell2mat(a) cell2mat(y)];
start=cell2mat(a);
plot3(start(1,1),start(2,1),start(3,1),'b*',record(1,:),record(2,:),record(3,:))
drawnow; % Hit any key to continue. 36
37. pause a = {P(:,i)}; [y,Pf,Af]=sim(net,{1 10},{},a);
record=[cell2mat(a) cell2mat(y)]; start=cell2mat(a);
plot3(start(1,1),start(2,1),start(3,1),'b*',record(1,:),record(2,:),record(3,:))
drawnow; % Hit any key to continue. pause a = {P(:,i)};
[y,Pf,Af]=sim(net,{1 10},{},a); record=[cell2mat(a) cell2mat(y)];
start=cell2mat(a);
plot3(start(1,1),start(2,1),start(3,1),'b*',record(1,:),record(2,:),record(3,:))
drawnow; % Hit any key to continue. pause a = {P(:,i)};
[y,Pf,Af]=sim(net,{1 10},{},a); record=[cell2mat(a) cell2mat(y)];
start=cell2mat(a);
plot3(start(1,1),start(2,1),start(3,1),'b*',record(1,:),record(2,:),record(3,:))
drawnow; % Hit any key to continue. pause 37
38. a = {P(:,i)}; [y,Pf,Af]=sim(net,{1 10},{},a);
record=[cell2mat(a) cell2mat(y)]; start=cell2mat(a);
plot3(start(1,1),start(2,1),start(3,1),'b*',record(1,:),record(2,:),record(3,:))
drawnow; % Hit any key to continue. pause Output(1) : 38
39. 39
40. 40
41. 41
42. Code(2) : function varargout = hopfieldNetwork(varargin)
gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn',
@hopfieldNetwork_OpeningFcn, ... 'gui_OutputFcn',
@hopfieldNetwork_OutputFcn, ... 'gui_LayoutFcn', [] , ...
'gui_Callback', []); if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1}); end if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else
gui_mainfcn(gui_State, varargin{:}); end % End initialization code
- DO NOT EDIT % --- Executes just before hopfieldNetwork is made
visible. function hopfieldNetwork_OpeningFcn(hObject, eventdata,
handles, varargin) % Choose default command line output for
hopfieldNetwork handles.output = hObject; N =
str2num(get(handles.imageSize,'string')); handles.W = [];
handles.hPatternsDisplay = []; % Update handles structure
guidata(hObject, handles); % --- Outputs from this function are
returned to the command line. function varargout =
hopfieldNetwork_OutputFcn(hObject, eventdata, handles) % varargout
cell array for returning output args (see VARARGOUT); % hObject
handle to figure % eventdata reserved - to be defined in a future
version of MATLAB % handles structure with handles and user data
(see GUIDATA) % Get default command line output from handles
structure varargout{1} = handles.output; % --- Executes on button
press in reset. function reset_Callback(hObject, eventdata,
handles) % cleans all data and enables the change of the number of
neurons used for n=1 : length(handles.hPatternsDisplay)
delete(handles.hPatternsDisplay(n)); 42
43. end handles.hPatternsDisplay = [];
set(handles.imageSize,'enable','on'); handles.W = [];
guidata(hObject, handles); function imageSize_Callback(hObject,
eventdata, handles) % hObject handle to imageSize (see GCBO) %
eventdata reserved - to be defined in a future version of MATLAB %
handles structure with handles and user data (see GUIDATA) num =
get(hObject,'string'); n = str2num(num); if isempty(n) num = '32';
set(hObject,'string',num); end if n > 32 warndlg('It is strongly
recomended NOT to work with networks with more then 32^2
neurons!','!! Warning !!') end % --- Executes during object
creation, after setting all properties. function
imageSize_CreateFcn(hObject, eventdata, handles) . if ispc
set(hObject,'BackgroundColor','white'); else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')
); end % --- Executes on button press in loadIm. function
loadIm_Callback(hObject, eventdata, handles) [fName dirName] =
uigetfile('*.bmp;*.tif;*.jpg;*.tiff'); if fName
set(handles.imageSize,'enable','off'); cd(dirName); im =
imread(fName); N = str2num(get(handles.imageSize,'string')); im =
fixImage(im,N); imagesc(im,'Parent',handles.neurons);
colormap('gray'); end % --- Executes on button press in train.
function train_Callback(hObject, eventdata, handles) 43
44. Npattern = length(handles.hPatternsDisplay); if Npattern
> 9 msgbox('more then 10 paterns isn''t supported!','error');
return end im = getimage(handles.neurons); N =
get(handles.imageSize,'string'); N = str2num(N); W = handles.W;
%weights vector avg = mean(im(:)); %removing the cross talk part if
~isempty(W) %W = W +( kron(im,im))/(N^2); W = W + (
kron(im-avg,im-avg))/(N^2)/avg/(1-avg); else % W =
kron(im,im)/(N^2); W = ( kron(im-avg,im-avg))/(N^2)/avg/(1-avg);
end % Erasing self weight ind = 1:N^2; f = find(mod(ind,N+1)==1);
W(ind(f),ind(f)) = 0; handles.W = W; % Placing the new pattern in
the figure... xStart = 0.01; xEnd = 0.99; height = 0.65; width =
0.09; xLength = xEnd-xStart; xStep = xLength/10; offset =
4-ceil(Npattern/2); offset = max(offset,0); y = 0.1; if Npattern
> 0 for n=1 : Npattern x = xStart+(n+offset-1)*xStep; h =
handles.hPatternsDisplay(n); set(h,'units','normalized');
set(h,'position',[x y width height]); end x =
xStart+(n+offset)*xStep; h =
axes('units','normalized','position',[x y width height]);
handles.hPatternsDisplay(n+1) = h; imagesc(im,'Parent',h); else x =
xStart+(offset)*xStep; h = axes('units','normalized','position',[x
y width height]); handles.hPatternsDisplay = h; end 44
45. imagesc(im,'Parent',h); set(h,
'YTick',[],'XTick',[],'XTickMode','manual','Parent',handles.learnedPat
erns); guidata(hObject, handles); % --- Executes on button press in
addNoise. function addNoise_Callback(hObject, eventdata, handles)
im = getimage(handles.neurons); % N =
get(handles.imageSize,'string'); % N = floor(str2num(N)/2)+1;
noisePercent = get( handles.noiseAmount, 'value' ); N = round(
length(im(:))* noisePercent ); N = max(N,1); %minimum change one
neuron ind = ceil(rand(N,1)*length(im(:))); % im(ind) = -1*im(ind);
%!!!! im(ind) = ~im(ind); imagesc(im,'Parent',handles.neurons);
colormap('gray'); % --- Executes on button press in run. function
run_Callback(hObject, eventdata, handles) im =
getimage(handles.neurons); [rows cols] = size(im); if rows ~= cols
msgbox('I don''t support non square images','error'); return; end N
= rows; W = handles.W; if isempty(W) msgbox('No train data - doing
nothing!','error'); return; end %figure; imagesc(W) mat =
repmat(im,N,N); mat = mat.*W; mat = im2col(mat,[N,N],'distinct');
networkResult = sum(mat); networkResult =
reshape(networkResult,N,N); im = fixImage(networkResult,N);
imagesc(im,'Parent',handles.neurons); function im = fixImage(im,N)
% if isrgb(im) if length( size(im) ) == 3 im = rgb2gray(im);
45
46. end im = double(im); m = min(im(:)); M = max(im(:)); im =
(im-m)/(M-m); %normelizing the image im = imresize(im,[N
N],'bilinear'); %im = (im > 0.5)*2-1; %changing image values to
-1 & 1 im = (im > 0.5); %changing image values to 0 & 1
% --- Executes on slider movement. function
noiseAmount_Callback(hObject, eventdata, handles) % hObject handle
to noiseAmount (see GCBO) % eventdata reserved - to be defined in a
future version of MATLAB % handles structure with handles and user
data (see GUIDATA) percent = get(hObject,'value'); percent =
round(percent*100);
set(handles.noisePercent,'string',num2str(percent)); % --- Executes
during object creation, after setting all properties. function
noiseAmount_CreateFcn(hObject, eventdata, handles)
set(hObject,'BackgroundColor',[.9 .9 .9]); else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')
); end Output(2) : 46
47. 47
48. 48
49. 49
50. : 05
51. self-organizing neural network . habbian learning i j j i :
- . - . . . 15
52. : ]1,0[ . . : p n y j ( p ) xi ( p) wij ( p ) j 1i n j j :
: )1+Wij(p)+= Wij(p) Wij(p ) Wij(p p : ])Wij(p)= yj(p)[xj(p) -
Wij(p =/ : p 25
54. Code : function digit_recognition disp('
=====================================') disp(' Character
recognition neural networks') disp('
=====================================') disp('
======================================================================
======') disp(' Reference: Negnevitsky, M., "Artificial
Intelligence: A Guide to Intelligent') disp(' Systems", Addison
Wesley, Harlow, England, 2002. ') disp(' Sec. 9.4 Will a neural
network work for my problem? ') disp('
======================================================================
======') disp('
======================================================================
=========') disp(' Problem: A multilayer feedforward network is
used for the recognition of digits') disp(' from 0 to 9. Each digit
is represented by a 5 x 9 bit map. ') disp('
======================================================================
=========') disp(' Hit any key to visualise bit maps of the
digits.') disp(' ') pause
[digit1,digit2,digit3,digit4,digit5,digit6,digit7,digit8,digit9,digit0
] = bit_maps; disp(' Hit any key to obtain ten 45-element input
vectors denoted by "p".') pause
p=[digit1(:),digit2(:),digit3(:),digit4(:),digit5(:),digit6(:),digit7(
:),digit8(:),digit9(:),digit0(:)] disp(' Hit any key to define ten
10-element target vectors denoted by "t". ') pause t = eye(10)
54
55. disp(' Hit any key to define the network architecture.')
pause s1=12; % Number of neurons in the hidden layer s2=10; %
Number of neurons in the output layer disp(' ') fprintf(1,'
s1=%.0f; fprintf(1,' s2=%.0f; disp(' ') Number of neurons in the
hidden layern',s1); Number of neurons in the output layern',s2);
disp(' Hit any key to create the network, initialise its weights
and biases, ') disp(' and set up training parameters.') pause net =
newff(minmax(p),[s1 s2],{'logsig' 'purelin'},'traingdx');
net.trainParam.show = 20; progress net.trainParam.epochs = 1000;
net.trainParam.goal = 0.001; net.trainParam.lr=0.1; % Number of
epochs between showing the % Maximum number of epochs % Performance
goal % Learning rate disp(' ') fprintf(1,'
net.trainParam.show=%.0f; Number of epochs between showing the
progressn',net.trainParam.show); fprintf(1,'
net.trainParam.epochs=%.0f; Maximum number of
epochsn',net.trainParam.epochs); fprintf(1,'
net.trainParam.goal=%.3f; Performance goaln',net.trainParam.goal);
fprintf(1,' net.trainParam.lr=%.2f; Learning
raten',net.trainParam.lr); disp(' ') disp(' Hit any key to train
the back-propagation network.') pause disp(' ') disp(' net =
train(net,p,t)') disp(' ') net = train(net,p,t); disp(' Hit any key
to see how the network recognises a digit, for example digit 3.')
pause digit3 probe=digit3(:); a=sim(net,probe); disp('
a=sim(net,probe)') 55
56. a=round(a) disp(' Hit any key to see how "noise" distorts
the bit map of a digit, for example digit 5.') disp(' ') pause
probe=digit5; figure('name','"Noisy" bit maps') subplot(1,2,1)
probe_plot(probe) title('Noise level: 0%') disp(' Hit any key to
continue.') disp(' ') pause probe=digit5+randn(size(probe))*0.1;
subplot(1,2,2) probe_plot(probe) title('Noise level: 10%') disp('
Hit any key to continue.') disp(' ') pause
probe=digit5+randn(size(probe))*0.2; figure('name','"Noisy" bit
maps') subplot(1,2,1) probe_plot(probe) title('Noise level: 20%')
disp(' Hit any key to continue.') disp(' ') pause
probe=digit5+randn(size(probe))*0.5; subplot(1,2,2)
probe_plot(probe) title('Noise level: 50%') disp(' Hit any key to
evaluate the digit recognition neural network.') disp(' ') pause %
Set parameters for the test. noise_range = 0:.05:.50; % Range of
noise max_test = 100; % Number of test examples for each digit to
be recognised average_error = []; % Average recognition error for a
particular noise level 56
57. % Evaluate the digit recognition network. for
noise_level=noise_range error=0; for i=1:max_test
probe=p+randn(size(p))*noise_level; a=compet(sim(net,probe));
error=error+sum(sum(abs(a-t)))/2; end average_error =
[average_error error/10/max_test]; fprintf('Noise level: %.0f
percent; Average error: %.0f
percentn',noise_level*100,error/10/max_test*100); end disp(' ')
disp(' Hit any key to plot the test results.') disp(' ') pause h =
figure; plot(noise_range*100,average_error*100,'b-');
title('Performance of the digit recognition network') xlabel('Noise
level, %'); ylabel('Recognition error, %'); disp(' ') disp(' Hit
any key to train the digit recognition network with "noisy"
examples.') disp(' ') pause figure net.trainParam.epochs = 1000; %
Maximum number of epochs to train. t_noise = [t t t t]; for pass =
1:10 fprintf('Pass = %.0fn',pass); p_noise=[p p
(p+randn(size(p))*0.1) (p+randn(size(p))*0.2)]; net=
train(net,p_noise,t_noise); end disp(' Hit any key to evaluate the
digit recognition network trained with "noisy" examples.') disp('
') pause average_error = []; for noise_level = noise_range error =
0; 57
58. for i=1:max_test probe=p+randn(size(p))*noise_level;
a=compet(sim(net,probe)); error=error+sum(sum(abs(a-t)))/2; end
average_error = [average_error error/10/max_test]; fprintf('Noise
level: %.0f percent; Average error: %.0f
percentn',noise_level*100,error/10/max_test*100); end disp(' ')
disp(' Hit any key to plot the test results.') disp(' ') pause
figure(h) hold on plot(noise_range*100,average_error*100,'r-');
legend('Network trained with "perfect" examples','Network trained
with "noisy" examples',2); hold off disp('end of
digit_recognition') function probe_plot(probe); [m n]=size(probe);
probe_plot=[probe probe(:,[n])]'; probe_plot=[probe_plot
probe_plot(:,[m])]'; pcolor(probe_plot) colormap(gray) axis('ij')
axis image Output : 58
71. . ) ( ) (an inhibitory ) ( . . . . yj j ) ( . standard
competitive learning rule : ( xi wij ), if neuron j wins the
competition wij ,0 if neuron j loses the competition xi wj j . x
Euclidean distance . 17
72. x 1) wi (n 1 n 2 d x wj ( xi wij ) 2 1 i xi wij x wj wj x d
x wj wj x jx x j x min x wj , j 1,2,..., m m . 27
73. Code : echo on; pause rand('seed',1234); p=rands(2,1000);
plot(p(1,:),p(2,:),'r.') title('Input vectors'); xlabel('p(1)');
ylabel('p(2)'); pause s1=6; s2=6; net=newsom([-1 1; -1 1],[s1 s2]);
plotsom(net.iw{1,1},net.layers{1}.distances) pause
net.trainParam.show=100; progress % Number of epochs between
showing the for i=1:10 hold on;
net.trainParam.epochs=i*net.trainParam.show; net=train(net,p);
delete(findobj(gcf,'color',[0 0 1])); delete(findobj(gcf,'color',[1
0 0])); plotsom(net.IW{1,1},net.layers{1}.distances); hold off;
pause(0.001) end echo off; for i=1:(s1*s2);
text(net.iw{1,1}(i,1)+0.02,net.iw{1,1}(i,2),sprintf('%g',i)); end
echo on; pause for i=1:3 probe=rands(2,1); hold on;
plot(probe(1,1),probe(2,1),'.g','markersize',25); a=sim(net,probe);
a=find(a) text(probe(1,1)+0.03,probe(2,1),sprintf('%g',(a))); hold
off 73