Tópicos en Sistemas Expertos e Inteligencia Inteli gencia Artificial clase 6b
Arturo Vilca
[email protected] posgrado uni-fiee 2017-2
Toolbox - Redes Neuronales •
Modelamiento
•
Programación
•
Toolbox de de Redes Neuronales
•
El Perceptrón Perceptrón
Modelo de Redes Neuronales
Xi (i=1 a n)
y j (j=1 a m)
Configuración •
•
•
Cálculo de activación de cada neurona a j = ∑ xi wij Valor de salida de cada neurona s j = 0 si a j ≤ Ѳ j 1 si a j > Ѳ j Regla de aprendizaje de Widdrow-Hoff wij(t+1) = wij(t) + (t j-s j)xi
Red1: perceptron.m % Programa del Perceptrón clear all, close all, home % vectores de entrada y objetivo X = [ 0 0 1 1; 0 1 0 1 ]; % objetivo (OR lógico) T = [0 1 1 1];
% recuperación de las dimensiones de las matrices [r,q] = size(X); [s,q] = size(T);
Red1… % elección de los pesos y el ajuste inicial w = randn(s,r); b = randn; Nep = 100; % número total de épocas de aprendizaje K = 10; % ganancia de aprendizaje x1min=-0.5; x1max=1.5; x2min=-0.5; x2max=1.5; ECM=1; % inicialización
Red1… for i=1:Nep % fin del aprendizaje si ECM=0 if ECM == 0 i=i-1; break end % fase de prueba % activación WX = W*X; % ajuste de bias WXmasb = WX + b*ones(size(WX));
Red1… % cálculo de la salida y del error salida = WXmasb >= 0; error = T-salida; % suma de los cuadrados de los errores ECM = sum(sum(error.^2)); tr(i) = ECM; % trazado de la recta de aprendizaje close all recta(X,T,W,b,x1min,x1max,x2min,x2max,i) disp([‘epoca: ' num2str(i) ' ECM : ' num2str(ECM)])
Red1… % fase de aprendizaje dW = K*error*X'; db = error*ones(q,1); W = W + dW; b = b + db; pause end
Red1… % trazado de la evolución de las suma de los % cuadrados de los errores figure(2) plot(tr) title('suma de cuadrados de errores') xlabel('epoca') Grid disp('prueba sobre el juego [0 1]') x=[0 1]'; s=(W*x+b>0)
Red1: recta.m function recta(X,T,W,b,x1min,x1max,x2min,x2max,i) % trazado de la recta de separación [nx,mx]=size(X); figure(1) hold on axis([x1min x1max x2min x2max])
Red1:recta.m… for k=1:mx Tcero=T(k)==0; if Tcero plot(X(1,k),X(2,k),'o') else plot(X(1,k),X(2,k),'+') end title([‘Vectores a clasificar y la recta de separación - época: ' num2str(i)]) xlabel('x1') ylabel('x2') grid end
Red1:recta.m… xx=get(gca,'xlim'); y=(-xx*W(1)-b)/W(2); plot(xx,y,'r') hold off
NN-ToolBox: Perceptrón
Funciones •
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
nnstart nftool view fitnet feedforwardnet Cascadeforwardnet train trainlm trainbr trainscg trainrp mse regression ploterrhist plotfit plotperform plotregression plottrainstate
Neural network getting started GUI Neural network fitting tool View neural network Function fitting neural network Feedforward neural network Cascade-forward neural network Train neural network Levenberg-Marquardt backpropagation Bayesian regulation backpropagation Scaled conjugate gradient backpropagation Resilient backpropagation Mean squared normalized error performance function Linear regression Plot error histogram Plot function fit Plot network performance Plot linear regression Plot training state values
Función newp •
net = newp(p,t,tf,lf)
NET = newp(P,T,TF,LF) takes these inputs, P - RxQ matrix of Q1 representative input vectors. T - SxQ matrix of Q2 representative target vectors. TF - Transfer function, default = 'hardlim'. LF - Learning function, default = 'learnp'. Returns a new perceptron. The transfer function TF can be HARDLIM or HARDLIMS. The learning function LF can be LEARNP or LEARNPN.
newp >> red = newp([0 1; -2 2],1); >> P = [0 0 1 1; 0 1 0 1]; >> T = [0 1 1 1]; >> Y = sim(red,P) Y= 1 1 1 1 >> red.trainParam.epochs = 20; >> red = train(red,P,T); >> Y = sim(red,P) Y= 0 1 1 1
… newp clear all close all x = [0 0 1 1; 0 1 0 1]; t = [1 1 1 0]; plotpv(x,t) red = newp([0 1;0 1],1); red.iw{1,1}=[1,1]; red.b{1}=0.5; pesos=red.iw; figure;
pesos=red.iw{1,1}; bias=red.b{1}; plotpv(x,t) plotpc(pesos,bias) red=train(red,x,t); prueba=[0;0]; a = sim(red,prueba) prueba=[1;1]; a = sim(red,prueba)
Función perceptron perceptron(hardlimitTF,perceptronLF) toma una función de transferencia hardlimit y una función de aprendizaje y retorna un perceptrón •
x = [0 0 1 1; 0 1 0 1]; t = [0 1 1 1]; red = perceptron; red = train(red,x,t); view(red) y = red(x);
Red de perceptrones
feedforwardnet x = [0 1 2 3 4 5 6 7 8]; t = [0 0.84 0.91 0.14 -0.77 -0.96 -0.28 0.66 0.99]; plot(x,t,'o') red = feedforwardnet(10); red = configure(red,x,t); y1 = red(x) plot(x,t,'o',x,y1,'x') red = train(red,x,t); y2 = red(x) plot(x,t,'o',x,y1,'x',x,y2,'*')
nnstart