矩阵乘法返回错误的值

时间:2018-06-12 06:27:18

标签: c# matlab neural-network matrix-multiplication

我正在使用来自MATLAB训练的ANN的权重和偏差来计算值。试图对S形模拟方程进行编码,但由于某种原因,C#计算的变化比MATLAB的变化太大。即错误太高。我试图检查等式的每一步,找出造成问题的具体部分(强调部分),但我不知道如何解决这个问题,如果有人可以提供帮助,那将是一个巨大的帮助。

1+(purelin(net.LW {2}×(正切S型(net.IW {1}×(1-(ABS(2×([输入] -1)))))+ net.b {1 }))+ net.b {2}))/ 2

//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;
        }

1 个答案:

答案 0 :(得分:0)

问题解决了。 问题在于从MATLAB复制的权重矩阵。调试模式救了我的命。 :)