%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%Rede Neural com uma camada escondidada%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Exemplo - Problema do Ou-Exclusivo % X =[0 0;0 1;1 0;1 1]; % Y = [0;1;1;0] % [Aval,Bval]=rede_neural(X,Y,X,Y,2,2000) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [Aval,Bval]=rede_neural(X,Yd,Xval,Ydval,h,nepocasmax) [N,ne]= size(X); Nval = size(Xval,1); X = [X,ones(N,1)]; Xval=[Xval,ones(Nval,1)]; ns= size(Yd,2); A = rands(h,ne+1); B = rands(ns,h+1); Yr = calc_saida(X,A,B,h,N); [dJdA,dJdB]=calc_grad(X,Yd,A,B,N); grad = [dJdA(:);dJdB(:)]; erro = Yr-Yd; EQM = 1/N*sum(sum(erro.*erro)); Yrval = calc_saida(Xval,A,B,h,Nval); erroval = Yrval-Ydval; EQMval = 1/N*sum(sum(erroval.*erroval)); vetEQM =[]; vetEQM = [vetEQM,EQM]; nep = 0; %alfa = 0.1; while norm(grad)>1e-5 & nep1e-4 if hl>0 alfa_u = alfa_m; elseif hl<0 alfa_l = alfa_m; else break; end alfa_m = (alfa_l+alfa_u)/2; Aaux = A - alfa_m*dJdA; Baux = B - alfa_m*dJdB; [dJdAaux,dJdBaux]=calc_grad(X,Yd,Aaux,Baux,N); grad = [dJdAaux(:);dJdBaux(:)]; hl = grad'*d; end end