本文介绍了矩阵乘法返回错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,

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


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



我尝试了什么:



 //数据规范化
public double规范化(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);
返回xNorm;
}

//计算基于ANN的输出值的公式
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);

返回FinalValue;
}
解决方案

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;
        }
解决方案


这篇关于矩阵乘法返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:41