Sunteți pe pagina 1din 13

Industrial Computing

Artificial Neural Network


Eng. Byron Lima MSc.

Student: __RANDY DAVID ROMERO CORONADO


________________________________________________________________

1. Single Layer Perceptron


a. In the perceptron below, what will the output be when the input is (0, 0)? What about inputs (0,
1), (1, 1) and (1, 0)? What if we change the bias weight to -0.5?

Figure 1: Single Layer Perceptron. b = 1


Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.

b. Implement a perceptron. Implement the perceptron, and train it to perform the logical
functions NOT (use only one of the inputs), NAND, and NOR. Use a customized script to solve
this problem.

%% Inicialización del espacio de trabajo


close all; clear all; clc;
x= [0 1 0 1; %[x1 x2]
0 0 1 1]'; % Patrones de entrada
yd=[1 0 0 0]; % Salida esperada
winit= [1.5 0.5 1.5]; % Pesos iniciales [w0 w1 w2];
limit = 10; %Máximo número de épocas
disp('------------- Función Lógica NOR -------------')
%% Proceso de entrenamiento a través del método de corrección de error
w= winit; e=0; End=false;
while (End == false) && (e<limit)
End=true;
for i=1:4 %número de patrones para entrenamiento
y(i)= x(i,1)*w(2)+x(i,2)*w(3)+w(1); %suma ponderada
y=x1*w1+x2*w2+w0
if y(i)>=0 %Función de activación
y(i)=1;
else
y(i)=0;
end
error(i)=yd(i)-y(i); %cálculo del error
if (error(i)==0)
wfin=w;
Zerr=true;
else %Ajuste de los pesos de la red
wfin(2)=x(i,1)*error(i)+w(2);
wfin(3)=x(i,2)*error(i)+w(3);
wfin(1)=error(i)+w(1);
Zerr=false;
end
w=wfin;
End=(Zerr && End);
pause(0.1);
end
e = e+1; %Conteo del número de épocas
if End==1
%disp("Entrenamiento exitoso.....")
fprintf('Entrenamiento Exitoso... Epocas %d \n',e)
end
end
if e>=limit
disp("Entrenamiento incompleto, se ha excedido el número máximo
de épocas....")
end

%Ejecución de la red
xr = x;
for i=1:4
yr (i)= xr(i,1)*w(2)+x(i,2)*w(3)+w(1);
if yr (i) >= 0 %Funcion de activacion
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.

yr (i) = 1;
else
yr (i)=0;
end
end
disp('Entradas: [x1 x2]' ); disp(xr)
disp('Salidas:[y]'); disp(yr')
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.

2. Classification of linearly separable data with a perceptron


Two clusters of data, belonging to two classes, are defined in a 2-dimensional input space.
Classes are linearly separable. The task is to construct a Perceptron for the classification of data.

c. Solve this problem using the neural networks toolbox.

close all, clear all, clc, format compact


% number of samples of each class
N = 20;
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.

% define inputs and outputs


offset = 5; % offset for second class
x = [randn(2,N) randn(2,N)+offset]; % inputs
y = [zeros(1,N) ones(1,N)]; % outputs
% Plot input samples with PLOTPV (Plot perceptron input/target vectors)
figure(1)
plotpv(x,y);

net = perceptron;
net = train(net,x,y);
view(net);

figure(1)
plotpc(net.IW{1},net.b{1});
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.

d.-Solve this problem using a customized script.

close all, clear all, clc, format compact


N =20;
% define inputs and outputs
offset = 5; % offset for second class
x = [randn(2,N) randn(2,N)+offset]; % inputs
y = [zeros(1,N) ones(1,N)]; % outputs

winit=[1.5 0.5 1.5];%Pesos iniciales [w0 w1 w2]


limit =10;%Maximo numero de epocas
%%Proceso de entrenamiento a traves del metodo de correccion de error
w=winit;e=0;End=false;
x=x';
yd=y;
while(End==false)&&(e<limit)
End=true;
for i=1:(2*N) %numero de patrones de entrenamiento
y(i)=x(i,1)*w(2)+x(i,2)*w(3)+w(1);%Suma ponderada
if y(i) >=0%Funcion de activacion
y(i)=1;
else
y(i)=0;
end
error(i)=yd(i)-y(i);%Calculo del error
if (error(i)==0)
wfin=w;
Zerr=true;
else %Ajuste de los pesos de la red
wfin(2)=x(i,1)*error(i)+w(2);
wfin(3)=x(i,2)*error(i)+w(3);
wfin(1)=error(i)+w(1);
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.

Zerr=false;
end
w=wfin;
End=(Zerr && End);
pause(0.01);
end
e=e+1;
if End==1
disp("Entrenamiento exitoso...N# epocas:")
disp(e);
end
end
if e>=limit
disp("Entrenamiento incompleto, se ha excedido el numero maximo de
epocas")
end
xr=x;
for i=1:(2*N)
yr(i)=xr(i,1)*w(2)+xr(i,2)*w(3)+w(1);
if yr(i)>=0
yr(i)=1;
else
yr(i)=0;
end
end
disp('Entradas: [X1 X2]');disp(xr)
disp('Salidas: [y]');disp(yr')
% Plot input samples with PLOTPV (Plot perceptron input/target vectors)
figure(1)
plotpv(x',y);
x1=xr(:,1);
m=-wfin(2)/wfin(3);
b=-wfin(1)/wfin(3);
x2=x1*m+b;
hold on;grid on;
plot(x1,x2);
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.

3. Multi-Layer Perceptron (MLP)


e. Develop a perceptron network with two input variables x1 and x2 and at most three perceptron
that exactly classifies the marked area (and the boundary) plotted in Figure 3 as positive.
Illustrate the topology (structure) of the network and the weights of its neurons.
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.

Figure 3: Input space of the perceptron network. The gray area is classified as positive.

X1 y X2

4. Classification of a 4-class problem with a perceptron

Perceptron network with 2-inputs and 2-outputs is trained to classify input vectors into 4
categories.
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.
Industrial Computing
Artificial Neural Network
Eng. Byron Lima MSc.

f. Solve this problem using the neural networks toolbox.


g. Solve this problem using a customized script.