Little Red Riding Hood
This example try to show the principle of the perceptron algorithm. The objective of this application is to determine in based of some characteristics which person you are taking about.
This is the code of the application, some code is not mention because only help us to define de GUI, but everything you can watch on GitHub.
This method is in charge to execute the perceptron algorithm, read the input and the output that we previously select con the ui, and a training base array to compare and could resolve which character we selected.
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% aqui se entrena al perceptron
global W1;
global W2;
set(handles.edit3,'String','Procesando');
coefApre = rand;
input = [ 1, 1, 1, 0, 0, 0;
1, 0, 0, 1, 0, 1;
0, 0, 1, 0, 1, 1];
output = [1, 1, 1, 0, 0, 0, 0;
0, 0, 0, 1, 1, 0, 1;
0, 0, 0, 1, 0, 1, 1];
out1 = [0, 0, 0, 0, 0, 0, 0];
out2 = [0, 0, 0, 0, 0, 0, 0];
E = [0, 0, 0, 0, 0, 0, 0];
A = [0, 0, 0, 0, 0, 0, 0];
W1 = rand(6,7);
W2 = rand(7);
error = 1;
while error > 0.08%cada ciclo es una etapa
error = 0;
for e =1:1:3% cada ciclo es el calculo de una entrada
%calcula salidas de capa oculta
for i = 1:1:7
in = input(e,:) * W1(:,i);
out1(i) = 1 / (1 + exp(-1*in));
end
%calcula salida de utlima capa
for i = 1:1:7
in = out1 * W2(:,i);
out2(i) = 1 / (1 + exp(-1*in));
end
%AJUSTE DE PESOS
%pesos de la ultima capa
for i = 1:1:7
E(i) = output(e,i) - out2(i); %error
end
errorSuma = 0;
for i = 1:1:7
errorSuma = errorSuma + abs(E(i)); %error
end
if error < errorSuma;
error = errorSuma;
end
for i = 1:1:7
A(i) = E(i) * out2(i) * (1 - out2(i)); %calculo de retropropagacion
for j = 1:1:7
W2(j,i) = W2(j,i) + (coefApre * out1(j) * A(i));
end
end
%pesos para la capa oculta
for i = 1:1:7
delta = 0;
for k = 1:1:7
delta = delta + W2(k,i)*A(k);
end
delta = out1(i) * (1 - out1(i)) * (delta); %calculo de retropropagacion
for j = 1:1:6
W1(j,i) = W1(j,i) + (coefApre * input(e,j) * delta);
end
end
end
end
set(handles.edit3,'String','Finalizo Entrenamiento');
This method help us to print base on the output the photo of the character, and also some behavior of them.
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%aqui evalua a las entradas
global W1;
global W2;
set(handles.edit1,'String','');
input = [0, 0, 0, 0, 0, 0];
if(strcmp(get(handles.pushbutton1,'String'),'SI'))
input(1) = 1;
else
input(1) = 0;
end
if(strcmp(get(handles.pushbutton2,'String'),'SI'))
input(2) = 1;
else
input(2) = 0;
end
if(strcmp(get(handles.pushbutton3,'String'),'SI'))
input(3) = 1;
else
input(3) = 0;
end
if(strcmp(get(handles.pushbutton4,'String'),'SI'))
input(4) = 1;
else
input(4) = 0;
end
if(strcmp(get(handles.pushbutton5,'String'),'SI'))
input(5) = 1;
else
input(5) = 0;
end
if(strcmp(get(handles.pushbutton6,'String'),'SI'))
input(6) = 1;
else
input(6) = 0;
end
out1 = [0, 0, 0, 0, 0, 0, 0];
out2 = [0, 0, 0, 0, 0, 0, 0];
for i = 1:1:7
in = input * W1(:,i);
out1(i) = 1 / (1 + exp(-1*in));
end
for i = 1:1:7
in = out1 * W2(:,i);
out2(i) = round(1 / (1 + exp(-1*in)));
end
if(out2(1)==1)
set(handles.text9,'Visible','on');
else
set(handles.text9,'Visible','off');
end
if(out2(2)==1)
set(handles.text10,'Visible','on');
else
set(handles.text10,'Visible','off');
end
if(out2(3)==1)
set(handles.text11,'Visible','on');
else
set(handles.text11,'Visible','off');
end
if(out2(4)==1)
set(handles.text12,'Visible','on');
else
set(handles.text12,'Visible','off');
end
if(out2(5)==1)
set(handles.text13,'Visible','on');
else
set(handles.text13,'Visible','off');
end
if(out2(6)==1)
set(handles.text14,'Visible','on');
else
set(handles.text14,'Visible','off');
end
if(out2(7)==1)
set(handles.text15,'Visible','on');
else
set(handles.text15,'Visible','off');
end
lobo = [1, 1, 1, 0, 0, 0, 0];
abuelita = [0, 0, 0, 1, 1, 0, 1];
lenador = [0, 0, 0, 1, 0, 1, 1];
axes(handles.axes1)
if out2 == lobo
set(handles.edit1,'String','Es el Lobo');
imshow('lobo.png')
end
if out2 == abuelita
set(handles.edit1,'String','Es la Abuelita');
imshow('abuelita.jpg')
end
if out2 == lenador
set(handles.edit1,'String','Es el Lenador');
imshow('lenador.jpg')
end
Last updated
Was this helpful?