Path: news.mathworks.com!not-for-mail
From: "Adeel " <neoresearcher@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Backpropagation Neural network Code problem
Date: Sat, 14 Mar 2009 06:49:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 134
Message-ID: <gpfk0u$g8$1@fred.mathworks.com>
References: <gpfjg6$t0n$1@fred.mathworks.com>
Reply-To: "Adeel " <neoresearcher@gmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1237013342 520 172.30.248.37 (14 Mar 2009 06:49:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 14 Mar 2009 06:49:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1755386
Xref: news.mathworks.com comp.soft-sys.matlab:524800


"Adeel " <neoresearcher@gmail.com> wrote in message <gpfjg6$t0n$1@fred.mathworks.com>...
> I am working on my thesis on face recognition on features of face, using backpropagation neural network. For it I had creating my own code for it (I am not using the building function). But the weights did not converge. Can someone please look at the code and hint me what might be wrong with the code.
> From Adeel Raza

I am sorry that I forgot to add my code in the previous posting

Here is the code

function [v,v0,w,w0] = trainBPElementWise(v,v0,w,w0)
%%  ==Step_0==Initialization=================================
clc
%%% Enter the Architecture detail
    disp('Enter the Architecture detail');
    e = 0.05; % error limit
    n = 2;  %INPUT
    p = 4; 
    m = 1; % OUTPUT
    alpha = 0.2;
%  -----------------------------------------------------------
%%% Initialization of the Input / Target 
    cd('E:\thesisExtra\nnet')
    disp('Loading the input vector x and target vectors')
    x = [0 0; 0 1; 1 0; 1 1];
    trainRecords = size(x,1);
    t = [0; 1; 1; 0];
%  ----------------------------------------------------------
%%% Initialization of the weights
if (nargin  < 2)
    disp('weights v and w are getting initialised randomly');
        v = randc(n,p); 
        w  = randc(p,m); 
        w0 = randc(1,m);
        v0 = rand(1,p);        
end
%%%First hidden Layer
    zin = zeros(trainRecords,p);
    z =  zeros(trainRecords,p);
    dinj = zeros(trainRecords,p);
    dj =  zeros(trainRecords,p);
    chv = zeros(n,p);
    chv0 = zeros(1,p);
%%%Second hidden Layer
    yin = zeros(trainRecords,m);
    y = zeros(trainRecords,m);
    dk = zeros(trainRecords,m);
    chw = zeros(p,m);
    chw0 = zeros(1,m);
iteration =1;
er = 0; error = 0;

%%  ==Step_1==While_stoping_condition_is_false==do_step_2-9===
while er==0
    errorMax(iteration) = max(max(error));
    disp(sprintf('Epoch : %4g, max err : %d',iteration, errorMax(iteration)));
%%% ==Step_2==For_Each_Training_pair===========do_Steps_3-8===
    for Tp=1:trainRecords
%% Feed forward:      
%%% ==Step_3==X_get_Input=====Already_done====================
%%% ==Step_4==================================================
%%% First Layer
        for j=1:p
            for i=1:n
                zin(Tp,j) = zin(Tp,j) + x(Tp,i) * v(i,j);
            end
            zin(Tp,j) = v0(j) + zin(Tp,j);
             z(Tp,j) = (2/(1+exp(-zin(Tp,j))))-1; %activation function 
        end
%%% ==Step_5==================================================
    %%% Second Layer
        for k=1:m
            for j=1:p
                yin(Tp,k) = yin(Tp,k) + z(Tp,j) * w(j,k);
            end
            yin(Tp,k) = w0(k) + yin(Tp,k);
             y(Tp,k) = (2/(1+exp(-yin(Tp,k))))-1; %activation function 
        end
%% Backpropagation of error (Training Started)
%%% ==Step_6==================================================
        for k=1:m
    %%% Error Info = (t - y) * (Derivative of Second layer activation function)
        dk(Tp,k) = (t(Tp,k) - y(Tp,k)) * ((1/2) *(1+y(Tp,k)) *(1 - y(Tp,k)));
            for j=1:p
                chw(j,k) = alpha * dk(Tp,k) * z(Tp,j);
            end
            chw0(k) = alpha * dk(Tp,k);
        end
%%% ==Step_7==================================================
        for j=1:p
            for k=1:m
                dinj(Tp,j) = dinj(Tp,j) + dk(Tp,k) * w(j,k);
            end
%%%     Error Info = dinj * (Derivative of First layer activation function)
           dj(Tp,j) = (dinj(Tp,j) * ((1/2) * (1 + z(Tp,j))* (1 - z(Tp,j))));
            for i=1:n
                chv(i,j) = alpha * dj(Tp,j) * x(Tp,i);
            end
            chv0(j) = alpha * dj(Tp,j);
        end
%% ==Step_8==Update_weights_and_biases========================
        for k=1:m
            for j=1:p
                w(j,k)=w(j,k)+chw(j,k);
            end
           w0(k)=w0(k)+chw0(k);
        end
        for j=1:p
            for i=1:n
                v(i,j)=v(i,j)+chv(i,j);
            end
           v0(j)=v0(j)+chv0(j);
        end
    end
%%  ==Step_9==Test_stoping_condition==========================
    error = sqrt((t-y).^2);
    %%% Update the stopping condition variable
    if max(max(error)) < e % W/P = e, W =weights,P =inputs, e = errors
        er =1;
    else
        er = 0;
    end
    if (~mod(iteration,200))
        figure
        plot(1:200,errorMax(iteration-199:iteration))
        getframe;%     F(iteration) = getframe; movie(F);
    end
    iteration = iteration +1;
    if (iteration > 600)
        break
    end
end %% End of wile loop Step 1
save('weight6040.dat','v','v0','w','w0') 
clf('reset'), cla reset, plot(errorMax)
xlabel('iteration '), ylabel('error '), title('Plot of the error')
end %% End of Function / File