Форум для обсуждения курса

11.03.2023 МТ-401

11.03.2023 МТ-401

yazan Артем Маковецкий -
Yanıt sayısı: 0

clear();

# 1 класс: (1,1),(2,4),(7,18),(12,15)
# 2 класс: (-3,5),(-4,9),(-5,12),(-15,2)
# 3 класс: (7,-3),(14,-18),(3,-22),(7,-13)
# 4 класс: (-1,-1),(-4,-9),(-3,-22),(-12,-15)
points = [1,2,7,12, -3,-4,-5,-15, 7,14,3,7, -1,-4,-3,-12;
1,4,18,15, 5,9,12,2, -3,-18,-22,-13, -1,-9,-22,-15];
Y = [1,0,0,0;
1,0,0,0;
1,0,0,0;
1,0,0,0;
0,1,0,0;
0,1,0,0;
0,1,0,0;
0,1,0,0;
0,0,1,0;
0,0,1,0;
0,0,1,0;
0,0,1,0;
0,0,0,1;
0,0,0,1;
0,0,0,1;
0,0,0,1;
];

Y=Y';

[ht, wd] = size(points);
#H1 -> (s1,N) = (3,16)
for i = 1:ht + 1
for j = 1:wd
if (i == 3)
H1(i, j) = 1;
else
H1(i, j) = points(i, j);
end
end
end

#disp(H1);

s1 = 3;
s2 = 4;

#W1 -> (s2,s1) = (4,3)
for i = 1:s2
for j = 1:s1
W(i,j) = 1;
end
end

#W = [
# 1,0,1;
# 0,2,0;
# 1,0,1;
# 1,1,1
#]

#disp(W);

#Z2 -> (s2,N) = (4,16)
Z2 = W * H1;

#disp(Z2);

#Z2 -> (s2,N) = (4,16)
function HI = softmax(Z2)
[s2, N] = size(Z2);
for j = 1:N
sum = 0;
for k = 1:s2
sum = sum + exp(Z2(k, j));
end
for i = 1:s2
HI(i, j) = exp(Z2(i, j)) / sum;
end
end
endfunction;

H2 = softmax(Z2);

#disp(H2);

#N = wd;
#for i = 1:N
# sum = 0;
# for j = 1:s2
# sum = sum + H2(j, i);
# end
# #disp(sum);
#end

#disp(H2);

#H2 -> (s2,N) -> (4,16)
function sum = Error(H2,Y)
[s2, N] = size(H2);
sum = 0;
for i=1 : s2
for j=1 : N
t = (H2(i,j)-Y(i,j));
sum += t*t;
endfor
endfor
endfunction;

E = Error(H2,Y)
#disp(E);

function res = DerivSoftmax(Z2)
t = softmax(Z2);
res = t.-(t.*t);
endfunction;

function res = Gradient(H1, H2, Y, Z2)
HY = H2 - Y;
dF2 = DerivSoftmax(Z2);
Delta = HY .* dF2;
res = Delta * (H1');
endfunction;

DH2 = DerivSoftmax(Z2);
#disp(DH2);


function H2 = DirectPass(H1, Y, W)
Z2 = W * H1;
H2 = softmax(Z2);
endfunction;


lambda = 0.19;

W0 = W;
W1 = W0 - lambda * Gradient(H1, H2, Y, Z2);
H2 = DirectPass(H1, Y, W1);
E2 = Error(H2, Y)
# deltaError -> 0, deltaLambda -> 0