listing bab 13

LAMSTAR CODE (MATLAB)

Main.m
clear all
close all
X = train_pattern;
%pause(1)
%close all
n = 12 % Number of subwords
flag = zeros(1,n);
% To make 12 subwords from 1 input
for i = 1:min(size(X)),
X_r{i} = reshape(X(:,i),6,6);
for j = 1:n,
if (j<=6),
X_in{i}(j,:) = X_r{i}(:,j)’;
else
X_in{i}(j,:) = X_r{i}(j-6,:);
end
end
% To check if a subword is all ’0’s and makes it normalized value equal to zero
% and to normalize all other input subwords
p(1,:) = zeros(1,6);
for k = 1:n,
for t = 1:6,
if (X_in{i}(k,t)~= p(1,t)),
X_norm{i}(k,:) = X_in{i}(k,:)/sqrt(sum(X_in{i}(k,:).^2));
else
X_norm{i}(k,:) = zeros(1,6);
end
end
end
end%%%End of for
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dynamic Building of neurons
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Building of the first neuron is done as Kohonen Layer neuron
%(this is for all the subwords in the first input pattern for all SOM modules
i = 1;
ct = 1;
while (i<=n),
i
cl = 0;
for t = 1:6,
if (X_norm{ct}(i,t)==0),
cl = cl+1;
end
end
if (cl == 6),
Z{ct}(i) = 0;
elseif (flag(i) == 0),
W{i}(:,ct) = rand(6,1);
flag(i) = ct;
W_norm{i}(:,ct) = W{i}(:,ct)/sqrt(sum(W{i}(:,ct).^2));
Z{ct}(i)= X_norm{ct}(i,:)*W_norm{i};
alpha =0.8;
tol = 1e-5;
while(Z{ct}(i) <= (1-tol)),
W_norm{i}(:,ct) = W_norm{i}(:,ct) + alpha*(X_norm{ct}(i,:)’ -
W_norm{i}(:,ct));
Z{ct}(i) = X_norm{ct}(i,:)*W_norm{i}(:,ct);
end%%%%%End of while
end%%%%End of if
r(ct,i) = 1;
i = i+1;
end%%%%End of while
r(ct,:) = 1;
ct = ct+1;
while (ct <= min(size(X))),
for i = 1:n,
cl = 0;
for t = 1:6,
if (X_norm{ct}(i,t)==0),
cl = cl+1;
end
end
if (cl == 6),
Z{ct}(i) = 0;
else
i
r(ct,i) = flag(i);
r_new=0;
for k = 1:max(r(ct,i)),
Z{ct}(i) = X_norm{ct}(i,:)*W_norm{i}(:,k);
if Z{ct}(i)>=0.95,
r_new = k;
flag(i) = r_new;
r(ct,i) = flag(i);
break;
end%%%End of if
end%%%%%%%End of for
if (r_new==0),
flag(i) = flag(i)+1;
r(ct,i) = flag(i);
W{i}(:,r(ct,i)) = rand(6,1);
%flag(i) = r
W_norm{i}(:,r(ct,i)) = W{i}(:,r(ct,i))/sqrt(sum(W{i}(:,r(ct,i)).^2));
Z{ct}(i) = X_norm{ct}(i,:)*W_norm{i}(:,r(ct,i));
alpha =0.8;
tol = 1e-5;
while(Z{ct}(i) <= (1-tol)),
W_norm{i}(:,r(ct,i)) = W_norm{i}(:,r(ct,i)) + alpha*(X_norm{ct}(i,:)’ -
W_norm{i}(:,r(ct,i)));
Z{ct}(i) = X_norm{ct}(i,:)*W_norm{i}(:,r(ct,i));
end%%%End of while
end%%%End of if
%r_new
%disp(’Flag’)
%flag(i)
end%%%%End of if
end
ct = ct+1;
end
save W_norm W_norm
for i = 1:5,
d(i,:) = [0 0];
d(i+5,:) = [0 1];
d(i+10,:) = [1 0];
end
d(16,:) = [1 1];
%%%%%%%%%%%%%%%
% Link Weights
%%%%%%%%%%%%%%%
ct = 1;
m_r = max(r);
for i = 1:n,
L_w{i} = zeros(m_r(i),2);
end
ct = 1;
%%% Link weights and output calculations
Z_out = zeros(16,2);
while (ct <= 16),
ct
%for mn = 1:2
L = zeros(12,2);
% for count = 1:20,
for i = 1:n,
if (r(ct,i)~=0),
for j = 1:2,
if (d(ct,j)==0),
L_w{i}(r(ct,i),j) = L_w{i}(r(ct,i),j)-0.05*20;
else
L_w{i}(r(ct,i),j) = L_w{i}(r(ct,i),j)+0.05*20;
end %%End if loop
end %%% End for loop
L(i,:) = L_w{i}(r(ct,i),:);
end %%%End for loop
end
% end %%% End for loop
Z_out(ct,:) = sum(L);
ct = ct+1;
end
save L_w L_w

Test.m
clear all
X = test_pattern;
load W_norm
load L_w
% To make 12 subwords
for i = 1:min(size(X)),
i
X_r{i} = reshape(X(:,i),6,6);
for j = 1:12,
if (j<=6),
X_in{i}(j,:) = X_r{i}(:,j)’;
else
X_in{i}(j,:) = X_r{i}(j-6,:);
end
end
p(1,:) = zeros(1,6);
for k = 1:12,
for t = 1:6,
if (X_in{i}(k,t)~= p(1,t)),
X_norm{i}(k,:) = X_in{i}(k,:)/sqrt(sum(X_in{i}(k,:).^2));
else
X_norm{i}(k,:) = zeros(1,6);
end
end
end
for k = 1:12,
Z = X_norm{i}(k,:)*W_norm{k};
if (max(Z) == 0),
Z_out(k,:) = [0 0];
else
index(k) = find(Z == max(Z));
L(k,:) = L_w{k}(index(k),:);
Z_out(k,:) = L(k,:)*Z(index(k));
end
end
final_Z = sum(Z_out)
end

Training Pattern.m
function train = train_pattern
x1 = [1 1 1 1 1 1; 1 0 0 0 0 0; 1 1 1 1 1 1; 1 0 0 0 0 1; 1 0 0 0 0 1;
1 1 1 1 1 1];
x2 = [1 1 1 1 1 1; 1 0 0 0 0 1; 1 0 0 0 0 0; 1 1 1 1 1 1; 1 0 0 0 0 1;
1 1 1 1 1 1];
x3 = [1 1 1 1 1 1; 1 0 0 0 0 0; 1 1 1 1 1 0; 1 0 0 0 0 1; 1 0 0 0 0 1;
1 1 1 1 1 1];
x4 = [1 1 1 1 1 1; 1 0 0 0 0 0; 1 0 1 1 1 0; 1 1 0 0 0 1; 1 0 0 0 0 1;
1 1 1 1 1 1];
x5 = [1 1 1 1 1 0; 1 0 0 0 0 1; 1 0 0 0 0 0; 1 1 1 1 1 1; 1 0 0 0 0 1;
1 1 1 1 1 1];
x6 = zeros(6,6);
x6(1,:) = 1;
x6(:,6) = 1;
x7 = zeros(6,6);
x7(1,3:6) = 1;
x7(:,6) = 1;
x8 = zeros(6,6);
x8(1,2:6) = 1;
x8(:,6) = 1;
x9 = zeros(6,6);
x9(1,:) = 1;
x9(1:5,6) = 1;
x10 = zeros(6,6);
x10(1,2:5) = 1;

x10(2:5,6) = 1;
x11 = zeros(6,6);
for i = 1:6,
x11(i,i) = 1;
end
x11(1,6) = 1;
x11(2,5) = 1;
x11(3,4) = 1;
x11(4,3) = 1;
x11(5,2) = 1;
x11(6,1) = 1;
x12 = x11;
x12(1,1) = 0;
x12(6,6) = 0;
x12(1,6) = 0;
x12(6,1) = 0;
x13 = x11;
x13(1,1) = 0;
x13(6,6) = 0;
x14 = x11;
x14(1,6) = 0;
x14(6,1) = 1;
x15 = x11;
x15(3:4,3:4) = 0;
x16 = zeros(6,6);
x16(:,3:4) = 1;
x17 = zeros(6,6);
x17(1,:) = 1;
x17(6,:) = 1;
x17(:,1) = 1;
x18 = zeros(6,6);
x18(:,2) = 1;
x18(:,4) = 1;
x19 = (x16+x17+x18)/3;
xr1 = reshape(x1’,1,36);
xr2 = reshape(x2’,1,36);
xr3 = reshape(x3’,1,36);
xr4 = reshape(x4’,1,36);
xr5 = reshape(x5’,1,36);
xr6 = reshape(x6’,1,36);
xr7 = reshape(x7’,1,36);
xr8 = reshape(x8’,1,36);
xr9 = reshape(x9’,1,36);
xr10 = reshape(x10’,1,36);
xr11 = reshape(x11’,1,36);
xr12 = reshape(x12’,1,36);
xr13 = reshape(x13’,1,36);
xr14 = reshape(x14’,1,36);
xr15 = reshape(x15’,1,36);
xr19 = reshape(x19’,1,36);
xr16 = reshape(x16’,1,36);
xr17 = reshape(x17’,1,36);
xr18 = reshape(x18’,1,36);
train = [xr1’ xr2’ xr3’ xr4’ xr5’ xr6’ xr7’ xr8’ xr9’ xr10’ xr11’ xr12’
xr13’ xr14’ xr15’ xr19’];
rest = [xr16’ xr17’ xr18’];

Test Pattern.m
function t_pat = test_pattern
x1 = [0 0 0 0 0 0; 1 0 0 0 0 0; 1 1 1 1 1 0; 1 0 0 0 0 1; 1 0 0 0 0 1;
1 1 1 1 1 1];
x2 = zeros(6,6);
x2(:,1) = 1;
x2(3:6,6) = 1;
x2(6,:) = 1;
x2(3,5) = 1;
x2(4,4) = 1;
x2(5,3) = 1;
3 = zeros(6,6);
x3(1,:) = 1;
x3(:,6) = 1;
x3(1:2,1) = 1;
x4 = zeros(6,6);
x4(1,3:6) = 1;
x4(1:5,6) = 1;
x5 = zeros(6,6);
for i = 1:6,
x5(i,i) = 1;
end
x5(1,6) = 1;
x5(6,1) = 1;
x6 = x5;
x6(3,4) = 1;
x6(4,3) = 1;
x7 = zeros(6,6);
x7(:,4) = 1;
x8 = zeros(6,6);
x8(:,3:4) = 1;
xr1 = reshape(x1’,1,36);
xr2 = reshape(x2’,1,36);
xr3 = reshape(x3’,1,36);
xr4 = reshape(x4’,1,36);
xr5 = reshape(x5’,1,36);
xr6 = reshape(x6’,1,36);
xr7 = reshape(x7’,1,36);
xr8 = reshape(x8’,1,36);
t_pat = [xr1’ xr2’ xr3’ xr4’ xr5’ xr6’ xr7’ xr8’];


Komentar

Postingan populer dari blog ini

MUTS

5.1. Madaline Training

11.1. Fundamental Philosophy