Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
2015_for_stud_Практика_С.doc
Скачиваний:
16
Добавлен:
17.03.2016
Размер:
1.69 Mб
Скачать

3. Зміст звіту по практичній роботі

1. Назва і мета роботи.

2. Постановка задачі (завдання).

3. Приклад розв’язання завдання.

Контрольні запитання

  1. Чим відрізняється шар Кохонена від мапи Кохонена?

  2. Визначте правила навчання шару та мапи Кохонена.

  3. Які Ви знаєте топології розташування нейронів для мапи Кохонена?

  4. Визначте поняття “окіл нейрона”.

  5. Наведіть приклади обчислення відстаней між нейронами, які застосовують при навчанні мап Кохонена.

Задачі для самостійного розв’язання з теми 2

Варіанти завдань: кластеризація даних

Використати дані з бази даних діабет

1. 1-50 записи,

2. 51-100 записи,

3. 101-150 записи,

4. 151-200 записи,

5. 201-250 записи,

6. 251- 300 записи,

7. 301-350 записи,

8. 351- 400 записи,

9. 401 - 450 записи,

10. 451-500 записи,

11. 501- 550 записи,

12. 551- 600 записи,

13. 651 - 700 записи,

14. 701-750 записи.

http://archive.ics.uci.edu/ml/datasets.html

Аудиторна робота

1 % Self Organizing Feature Maps sofm (Kohonen networks)

The MATLAB program to cluster 4 vectors is given as follows.

%Kohonen self organizing maps

clc; clear;

x=[1 1 0 0;0 0 0 1;1 0 0 0;0 0 1 1]

alpha=0.4;

%initial weight matrix

w=rand(4,3); % 3 class, R4

disp('Initial weight matrix');

disp(w);

con=1;epoch=0;

while con

for i=1:4

for j=1:2

D(j)=0;

for k=1:4

D(j)=D(j)+(w(k,j)-x(i,k))^2;

end

end

for j=1:2

if D(j)==min(D)

J=j;

end

end

w(:,J)=w(:,J)+alpha*(x(i,:)'-w(:,J));

end

alpha=0.5*alpha; epoch=epoch+1;

if epoch==500

con=0;

end

end

disp('Weight Matrix after 500 epoch');

disp(w);

Output

Weight Matrix after 500 epoch

0.0422 0.9535 0.4387

0.0070 0.4195 0.3816

0.4913 0.1447 0.7655

0.9731 0.0052 0.7952

The MATLAB program for clustering the input vectors inside a square is given as follows.

%Kohonen self organizing maps

clc;clear; alpha=0.5;

%Input vectors are chosen randomly from within a square of side 1.0(centered at the orgin)

x1=rand(1,20)-0.5; x2=rand(1,20)-0.5;

x=[x1;x2]';

%The initial weights are chosen randomly within -1.0 to 1.0;

w1=rand(1,5)-rand(1,5);

w2=rand(1,5)-rand(1,5);

w=[w1;w2];

%Plot for training patterns

figure(1);

plot([-0.5 0.5 0.5 -.5 -0.5],[-0.5 -0.5 0.5 0.5 -0.5]);

xlabel('X1');ylabel('X2'); title('Kohonen net input');

hold on;

plot(x1,x2,'b.');

axis([-1.0 1.0 -1.0 1.0]);

%Plot for Initial weights

figure(2);

plot([-0.5 0.5 0.5 -0.5 -0.5],[-0.5 -0.5 0.5 0.5 -0.5]);

xlabel('W1'); ylabel('W2');

title('Kohonen self-organizing map Epoch=0');

hold on;

plot(w(1,:),w(2,:),'b.',w(1,:),w(2,:),'k');

axis([-1.0 1.0 -1.0 1.0]);

con=1;epoch=0;

while con

for i=1:20

for j=1:5

D(j)=0;

for k=1:2

D(j)=D(j)+(w(k,j)-x(i,k))^2;

end

end

for j=1:5

if D(j)==min(D)

J=j;

end

end

I=J-1;

K=J+1;

if I<1

I=5;

end

if K>5

K=1;

end

w(:,J)=w(:,J)+alpha*(x(i,:)'-w(:,J));

w(:,I)=w(:,I)+alpha*(x(i,:)'-w(:,I));

w(:,K)=w(:,K)+alpha*(x(i,:)'-w(:,K));

end

alpha=alpha-0.005; epoch=epoch+1;

if epoch==75

con=0;

end

end

disp('Epoch Number'); disp(epoch);

disp('Learning rate after 75 epoch');

disp(alpha);

%Plot for Final weights

figure(3);

plot([-0.5 0.5 0.5 -0.5 -0.5],[-0.5 -0.5 0.5 0.5 -0.5]);

xlabel('W1'); ylabel('W2');

title('Kohonen self-organizing map Epoch=100');

hold on;

plot(w(1,:),w(2,:),'b.',w(1,:),w(2,:),'k');

axis([-1.0 1.0 -1.0 1.0]);

Learning rate after 50 epoch

0.1250

Один із прикладів конкурентних мереж

%competetive learning demonstration

function competetiveDemo

clc;

close all;clear all;

[P(:,1) P(:,2)]=textread('met.txt','%f %f');

%%Parametrs

%Kohonen learning rate

KLR=0.01;

%Conscience learning rate

CLR=0.001;

numOfNeurons=7;epochs=1000;

%%Initialization

min_max=mean([min(P,[],1)-1; max(P,[],1)+1],2);

W=[ones(numOfNeurons,1).*min_max(1),ones(numOfNeurons,1).*min_max(2)];

c=ones(numOfNeurons,1)/numOfNeurons;

b=exp(1-log(c));

%%Train

Q=size(P,1);

for k=1:epochs

for n=1:Q

i=fix(rand*Q)+1;

p=P(i,:)';

z=ndist(W,p)+b;

a=compet(z);

db=learnb(b,a,CLR);

b=b+db;

dw=learnW(W,p,a,KLR);

W=W+dw;

end;

end

%%Results

drawPlot(P,W,'r+');

for k=1:size(P,1)

simulate(P(k,:),W)

end;

function dw=learnW(w,p,a,lr)

[S,R]=size(w);

Q=size(p,2);

pt=p';

dw=zeros(S,R);

for q=1:Q

i=find(a(:,q));

dw(i,:)=dw(i,:)+lr*(pt(q+zeros(length(i),1),:)-w(i,:));

end

function db=learnb(b,a,lr)

[s,q]=size(a);

if q~=1, a=(1/q)*sum(a,2);end

%b->conscience

c=exp(1-log(b));

%update conscience

c=(1-lr)*c+lr*a;

%conscience->db

db=exp(1-log(c))-b;

function a=compet(n)

[S,Q]=size(n);

[maxn,indn]=max(n,[],1);

a=sparse(indn,1:Q,ones(1,Q),S,Q);

function z=ndist(w,p)

[S,R]=size(w);

[R2,Q]=size(p);

z=zeros(S,Q);

if (Q<S)

p=p';

copies=zeros(1,S);

for q=1:Q

z(:,q)=sum((w-p(q+copies,:)).^2,2);

end

else

w=w';

copies=zeros(1,Q);

for i=1:S

z(i,:)=sum((w(:,i+copies)-p).^2,1);

end

end

z=-z.^0.5;

function drawPlot(P,W,color)

hold on;

minV=min(P,[],1)-0.2

maxV=max(P,[],1)+0.2

axis([minV(1) maxV(1) minV(2) maxV(2)])

for k=1:size(P,1)

plot(P(k,1),P(k,2),'b ');

end

for k=1:size(W,1)

plot(W(k,1),W(k,2),color);

plot(W(k,1),W(k,2),'yo', 'markersize',25)

xos1=[W(k,1)-0.05, W(k,1)];

yos1=[W(k,2)+0.05, W(k,2)];

[x1,y1]=ds2nfu(xos1,yos1);

class=sprintf('Class #%d',k);

text(xos1(1)+0.05,yos1(1)+0.1,class,'Fontsize',8)

end

function simulate(p,W)

a=ndist(W,p');

class=find(compet(a));

s=sprintf('This is class #%d', class);

disp(s)

function varargout=ds2nfu(varargin)

if length(varargin{1})==1 && ishandle(varargin{1})...

&& strcmp(get(varargin{1},'type'),'axes')

hAx=varargin{1};

varargin=varargin(2:end);

else

hAx=gca;

end;

if length(varargin)==1

pos=varargin{1};

else

[x,y]=deal(varargin{:});

end

axun=get(hAx,'Units');

set(hAx,'Units','normalized');

axpos=get(hAx,'Position');

axlim=axis(hAx);

axwidth=diff(axlim(1:2));

axheight=diff(axlim(3:4));

if exist('x','var')

varargout{1}=(x-axlim(1))*axpos(3)/axwidth+axpos(1);

varargout{2}=(y-axlim(3))*axpos(4)/axheight+axpos(2);

else

pos(1)=(pos(1)-axlim(1))/axwidth*axpos(3)+axpos(1);

pos(2)=(pos(2)-axlim(3))/axheight*axpos(4)+axpos(2);

pos(3)=pos(3)*axpos(3)/axwidth;

pos(4)=pos(4)*axpos(4)/axheight;

varargout{1}=pos;

end

set(hAx,'Units',axun)

Мережa Хеммінга

clc;

close all; clear all;

q=2;r=3;

W=zeros(q,r)

P(1,:)=[1.000 -1.000 -1.000]; %ORANGE

P(2,:)=[1.000 1.000 1.000]; %APPlE

rab1=size(P(1,:)); b=rab1(2);

alfa=0.5;

y1=[0;0];

s1=abs(sum(W(1,:)-P(1,:)));

s2=abs(sum(W(2,:)-P(2,:)));

while (s1 > 0.0005 && s2 > 0.0005)

y1(1)= W(1,:)*P(1,:)'+b;

y1(2)= W(2,:)*P(2,:)'+b;

W(1,:)=(W(1,:)'+alfa*y1(1)*(P(1,:)'-W(1,:)'))';

W(2,:)=(W(2,:)'+alfa*y1(2)*(P(2,:)'-W(2,:)'))';

s1=abs(sum(W(1,:)-P(1,:)));

s2=abs(sum(W(2,:)-P(2,:)));

end;

W1=W; % W2=[1 -0.5; -0.5 1]

for i=1:q

for j=1:q

if i==j

W2(i,j) =1;

else

W2(i,j)=-0.5

end; end;end

y1=[0;0]; Pt=[-1.000; -1.000; -1.000];

y1=purelin(W1*Pt+b);

y2=[0;0]

while (y2~=y1)

y2=poslin(W2*y1); y1=y2;

end;

for i=1:q

if y2(i)>0

s=sprintf('This is class #%d', i); disp (s);break;

else

s=sprintf('This is class #%d', i); disp (s);break;

end; end;

y2

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]