📄 generalization.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copy Right: the Computational Intelligence Laboratory
%% System name: PCA
%% File name: Generalization.m
%% Description: This is a simulation of the generalization capability of neural
%% network. in the simulation, the tiger image and fruit image will
%% is used.
%%
%% Author:
%% Date: 4/14/2004
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
tigerImage = imread('tigertest.bmp');
[rr,cc] = size(tigerImage);
Img = tigerImage;
mc=256;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the image is digited into the 16x16384 matrix.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r = 4; c = 4 ; p = r*c ;
tigerImage = blkM2vc(Img, [r c]) ;
[p N] = size(tigerImage) ;
Xm = mean(tigerImage')' ;
Xmax=max(max(abs(tigerImage)));
tigerImage = tigerImage - Xm(:, ones(1, N)) ;
tigerImage = tigerImage/max(max(abs(tigerImage))) ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the original tiger image is shows.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tempXX=tigerImage*Xmax;
tempXX=tempXX+Xm(:, ones(1, N)) ;
Imr = vc2blkM(tempXX, r, rr) ;
Imr = round(mc*Imr/max(max(Imr))) ;
figure(1), set(1,'Name','the original tiger image'),image(Imr), colormap(gray(mc))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the neural network is trained by the input data of the tiger image.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m = 2 ; % number of neurons
time(m)=rem(Now,1);
W = 0.6*(rand(m, p)-0.5); % weight initialisation
lda=rand(m,1);
er = .0001; % the length convergence error
e0=tigerImage;
for j=1:m
for s=1:16
[rs rn] = sort(rand(1, N)) ;
for n=1:N
if n==1
eta(j)=sum(sum(e0.^2))/N;
end
x = e0(:, rn(n)) ; % randomised selection of patterns
y = W(j,:)*x ;
eta(j)=y^2+eta(j);
dW(j,:)=(y/eta(j))*(x' - W(j,:)*y);
W(j,:)=W(j,:)+dW(j,:);
if sqrt(sum(dW(j,:).^2))<er
break;
end
end
if sqrt(sum(dW(j,:).^2))<er
break;
end
end
y=W(j,:)*e0;
e0=e0-W(j,:)'*y;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% reading the fruit image.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fruitImage = imread('fruit.bmp');
[rr,cc] = size(fruitImage);
Img = fruitImage;
mc=256;
r = 4; c = 4 ; p = r*c ;
fruitImage = blkM2vc(Img, [r c]) ;
[p N] = size(fruitImage) ;
Xm = mean(fruitImage')' ;
Xmax=max(max(abs(fruitImage)));
fruitImage = fruitImage - Xm(:, ones(1, N)) ;
fruitImage = fruitImage/max(max(abs(fruitImage))) ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% showing the original fruit image.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
XX=fruitImage*Xmax;
XX=XX+Xm(:, ones(1, N)) ;
Imr = vc2blkM(XX, r, rr) ;
Imr = round(mc*Imr/max(max(Imr))) ;
figure(2),set(2,'Name','the original fruit image'), image(Imr), colormap(gray(mc))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% extrating the principal component of the fruit data set.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yy = W*fruitImage ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Reconstructing the fruit image.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xr = W'*yy;
Xr=Xr*Xmax;
Xr = Xr + Xm(:, ones(1, N)) ;
Imr = vc2blkM(Xr, r, rr) ;
Imr = round(mc*Imr/max(max(Imr))) ;
Imr = round(Imr) ;
figure(3),set(3,'Name','the generalization result'), image(Imr), colormap(gray(mc))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% this is end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -