Пример #1
0
        /// <summary>
        /// 训练方法
        /// </summary>
        /// <param name="weightMatrixList">权值矩阵列表,对样本进行线性变换</param>
        /// <param name="matrix">输入矩阵,每一列就是一个样本的特征向量</param>
        /// <param name="desiredMatrix">期望矩阵</param>
        /// <returns>返回训练后的权值矩阵</returns>
        public static List <decimal[, ]> Train(List <decimal[, ]> weightMatrixList, decimal[,] matrix, decimal[,] desiredMatrix)
        {
            decimal            learnRate  = new decimal(.11);
            List <decimal[, ]> prevInput  = new List <decimal[, ]>();
            List <decimal[, ]> prevWeight = new List <decimal[, ]>();
            List <decimal[, ]> prevOutput = new List <decimal[, ]>();
            List <decimal[, ]> prevActive = new List <decimal[, ]>();

            decimal[,] output;
            foreach (decimal[,] weightMatrix in weightMatrixList)
            {
                prevWeight.Insert(0, weightMatrix);
                prevInput.Insert(0, matrix);
                matrix = DefaultActivateFunc.Active(output = weightMatrix.MatrixMultiMatrix(matrix));
                prevOutput.Insert(0, output);
                prevActive.Insert(0, matrix);
            }
            decimal[,] backPro = desiredMatrix.MatrixReduceMatrix(prevActive[0]).MatrixVec().VecDiag().MatrixMultiMatrix(
                DefaultActivateFunc.Dactive(prevOutput[0]).MatrixVec().VecDiag().MatrixMultiScalar(-1));
            List <decimal[, ]> partialWeight = new List <decimal[, ]>();

            partialWeight.Insert(0, backPro.MatrixMultiMatrix(prevInput[0].Turn().MatrixKronecker(MatrixHelper.CreateIdentityMatrix(prevWeight[0].GetLength(0)))).Turn());
            for (int i = 1, len = weightMatrixList.Count; i < len; i++)
            {
                backPro = backPro.MatrixMultiMatrix(MatrixHelper.CreateIdentityMatrix(prevActive[i].GetLength(1)).MatrixKronecker(weightMatrixList[i - 1]))
                          .MatrixMultiMatrix(DefaultActivateFunc.Dactive(prevActive[i]).MatrixVec().VecDiag());
                partialWeight.Insert(0, backPro.MatrixMultiMatrix(prevInput[i].Turn().MatrixKronecker(MatrixHelper.CreateIdentityMatrix(weightMatrixList[i].GetLength(0)))));
            }
            for (int i = 0, len = weightMatrixList.Count; i < len; i++)
            {
                weightMatrixList[i] = weightMatrixList[i].MatrixAndMatrix(partialWeight[i].MatrixMultiScalar(learnRate));
            }
            return(weightMatrixList);
        }
Пример #2
0
 /// <summary>
 /// 创建单位矩阵
 /// </summary>
 /// <param name="width"></param>
 public Matrix(int width)
 {
     InnerMatrix = MatrixHelper.CreateIdentityMatrix(width);
 }