Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
I am calculating values by using weights and bias from MATLAB trained ANN. trying to code a sigmoid simulation equation, but for some reason C# calculations vary too much than that of MATLAB. i.e. error is too high. I tried to check each step of the equation and found out the specific part that is creating the problem (Emphasized part), but I don't know how to solve this issue, if someone could help, would be a huge favour.

1+(purelin(net.LW{2}×(tansig(net.IW{1}×(1-(abs(2×([inputs]-1)))))+net.b{1}))+net.b{2}))/2

What I have tried:

//Normalization of Data
        public double Normalization(double x, double xMAx, double xMin)
        {
                double xNorm = 0.0;
                xNorm = (x - xMin) / (xMAx - xMin);
            if (xNorm < 0)
                xNorm = 0;

            if (xNorm > 1)
                xNorm = 1;
        xNorm = Math.Round(xNorm, 4);
        return xNorm;
    }

        // Equation to calculate ANN based Output Values
        public double MetrixCalc(double[] Pn, double[,] W1, double[] W2, double[] b1, double b2, double maxValue, double minValue)
        {

            double FinalValue = 0;

            double[] PnCalc1 = new double[Pn.Length];
            double[] PnCalc2 = new double[W1.Length / Pn.Length];

            for (int i = 0; i < Pn.Length; i++)
            {
                PnCalc1[i] = 1 - Math.Abs(2 * (Pn[i] - 1));
            }

        for (int i = 0; i < (W1.Length / Pn.Length); i++)
        {
            double PnCalc = 0.0;
            for (int j = 0; j < Pn.Length; j++)
            {
                PnCalc = PnCalc + (W1[i, j] * PnCalc1[j]);
            }
            PnCalc2[i] = PnCalc;
        }

        for (int i = 0; i < PnCalc2.Length; i++)
        {
            //PnCalc2[i] = Math.Tanh(PnCalc2[i] + b1[i]);
            PnCalc2[i] = PnCalc2[i] + b1[i];
            PnCalc2[i] = 2.0 / (1 + Math.Exp(-2 * (PnCalc2[i]))) - 1;
            PnCalc2[i] = Math.Round(PnCalc2[i], 4);
        }

        double FinalCalc = 0.0;

        for (int i = 0; i < PnCalc2.Length; i++)
        {
            *FinalCalc = FinalCalc + (W2[i] * (PnCalc2[i]));*
            //FinalValue = FinalCalc;
        }

        FinalValue = FinalCalc + b2;
        FinalValue = 1 + FinalValue;
        FinalValue = (1 + FinalValue) / 2.0;
        FinalValue = (FinalValue * (maxValue - minValue)) + minValue;
        FinalValue = Math.Round(FinalValue, 4);
        FinalValue = Math.Abs(FinalValue);

        return FinalValue;
        }
Posted
Updated 13-Jun-18 14:23pm
Comments
Member1x 13-Jun-18 3:38am    
Do you normalize your result?
AizazRizvi 13-Jun-18 22:29pm    
I am denormalzing the results because input values are already normalized.
Peter_in_2780 13-Jun-18 4:32am    
Why are you rounding PnCalc2[.] before calculating FinalCalc from them? That's throwing away accuracy.
AizazRizvi 13-Jun-18 22:29pm    
it is not a big deal but your suggestion is valuable. Thanks.
Patrice T 13-Jun-18 21:29pm    
To discuss and reply to comment, use the 'reply' button on right of member name.
Advantage, the member is notified.

1 solution

Problem is solved.
Problem was with the weights matrix copied from MATLAB. debugging mode saved my life. :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900