예제 #1
0
파일: NerveNetWork.cs 프로젝트: MyCreazy/BP
        /// <summary>
        /// 初始化权值矩阵
        /// </summary>
        /// <param name="inDimension">输入节点数目</param>
        /// <param name="hidedenDimension">隐藏层节点数</param>
        /// <param name="outDimension">输出层节点数</param>
        private void InitWeightMatrix(int inDimension, int hidedenDimension, int outDimension)
        {
            // 变成从数据库获取,如果没有获取到数据,则随机产生
            VerficationCodeParamOperate operate      = new VerficationCodeParamOperate();
            List <MWeightMatrix>        hiddenmatrix = operate.GetWeightMatrix("MU", 0);

            hiddenmatrix = hiddenmatrix.OrderBy(p => p.MatrixLocation).ToList();

            List <MWeightMatrix> outputmatrix = operate.GetWeightMatrix("MU", 1);

            outputmatrix = outputmatrix.OrderBy(p => p.MatrixLocation).ToList();

            // 初始化输入到隐藏层的权值矩阵
            for (int i = 0; i < inDimension; i++)
            {
                string[] dd = null;

                // 存的数据以逗号隔开
                if (hiddenmatrix != null && hiddenmatrix.Count > 0)
                {
                    dd = hiddenmatrix[i].MatrixLineData.Split(',');
                }

                for (int j = 0; j < hidedenDimension; j++)
                {
                    if (dd != null)
                    {
                        this.hideWeightMatrix[i, j] = Convert.ToDouble(dd[j]);
                    }
                    else
                    {
                        this.hideWeightMatrix[i, j] = (2.0 * this.random.NextDouble()) - 1.0;
                    }
                }
            }

            // 初始化隐藏层到输出层的权值矩阵
            for (int i = 0; i < hidedenDimension; i++)
            {
                string[] dd = null;

                // 存的数据以逗号隔开
                if (outputmatrix != null && outputmatrix.Count > 0)
                {
                    dd = outputmatrix[i].MatrixLineData.Split(',');
                }

                for (int j = 0; j < outDimension; j++)
                {
                    if (dd != null)
                    {
                        this.outPutWeightMatrix[i, j] = Convert.ToDouble(dd[j]);
                    }
                    else
                    {
                        this.outPutWeightMatrix[i, j] = (2.0 * this.random.NextDouble()) - 1.0;
                    }
                }
            }
        }
예제 #2
0
파일: NerveNetWork.cs 프로젝트: MyCreazy/BP
        /// <summary>
        /// 保存权值矩阵
        /// </summary>
        private void SaveWeightMatrix()
        {
            // 保存训练好的数据
            List <MWeightMatrix> saveWeightMatrix = new List <MWeightMatrix>();

            for (int i = 0; i < this.hideWeightMatrix.GetLength(0); i++)
            {
                string        matrixLineData    = string.Empty;
                MWeightMatrix heiddenWeightLine = new MWeightMatrix();
                for (int j = 0; j < this.hideWeightMatrix.GetLength(1); j++)
                {
                    matrixLineData += this.hideWeightMatrix[i, j].ToString();
                    if (j != this.hideWeightMatrix.GetLength(1) - 1)
                    {
                        matrixLineData += ",";
                    }
                }

                heiddenWeightLine.MatrixLineData = matrixLineData;
                heiddenWeightLine.DataSource     = this.data.DataSource;
                heiddenWeightLine.MatrixLocation = i;
                heiddenWeightLine.MatrixType     = 0;

                saveWeightMatrix.Add(heiddenWeightLine);
            }


            for (int i = 0; i < this.outPutWeightMatrix.GetLength(0); i++)
            {
                string        matrixLineData   = string.Empty;
                MWeightMatrix outputWeightLine = new MWeightMatrix();
                for (int j = 0; j < this.outPutWeightMatrix.GetLength(1); j++)
                {
                    matrixLineData += this.outPutWeightMatrix[i, j].ToString();
                    if (j != this.outPutWeightMatrix.GetLength(1) - 1)
                    {
                        matrixLineData += ",";
                    }
                }

                outputWeightLine.MatrixLineData = matrixLineData;
                outputWeightLine.DataSource     = this.data.DataSource;
                outputWeightLine.MatrixLocation = i;
                outputWeightLine.MatrixType     = 1;

                saveWeightMatrix.Add(outputWeightLine);
            }

            MWeightMatrix hideThresholdeWeight = new MWeightMatrix();
            string        matrixLineDataOne    = string.Empty;

            for (int i = 0; i < this.hideThreshold.GetLength(0); i++)
            {
                matrixLineDataOne += this.hideThreshold[i].ToString();
                if (i != this.hideThreshold.GetLength(0) - 1)
                {
                    matrixLineDataOne += ",";
                }
            }

            hideThresholdeWeight.MatrixLineData = matrixLineDataOne;
            hideThresholdeWeight.DataSource     = this.data.DataSource;
            hideThresholdeWeight.MatrixLocation = 0;
            hideThresholdeWeight.MatrixType     = 2;
            saveWeightMatrix.Add(hideThresholdeWeight);



            MWeightMatrix outPutThresholdeWeight = new MWeightMatrix();
            string        matrixLineDataTwo      = string.Empty;

            for (int i = 0; i < this.outputThreshold.GetLength(0); i++)
            {
                matrixLineDataTwo += this.outputThreshold[i].ToString();
                if (i != this.outputThreshold.GetLength(0) - 1)
                {
                    matrixLineDataTwo += ",";
                }
            }

            outPutThresholdeWeight.MatrixLineData = matrixLineDataTwo;
            outPutThresholdeWeight.DataSource     = this.data.DataSource;
            outPutThresholdeWeight.MatrixLocation = 0;
            outPutThresholdeWeight.MatrixType     = 3;
            saveWeightMatrix.Add(outPutThresholdeWeight);



            // 保存
            VerficationCodeParamOperate operate = new VerficationCodeParamOperate();

            operate.UpdateWeightMatrix(saveWeightMatrix);
        }