/// <summary> /// 对特征向量进行分类 /// </summary> /// <param name="weightMatrixList">权值矩阵列表,对样本进行线性变换</param> /// <param name="vector">特征列向量</param> /// <returns>返回分类结果,如果返回-1则表示分类失败!</returns> public static int Classify(List <decimal[, ]> weightMatrixList, decimal[,] vector) { foreach (decimal[,] weightMatrix in weightMatrixList) { vector = DefaultActivateFunc.Active(MatrixHelper.MatrixMultiMatrix(weightMatrix, vector)); } int type = -1; int count = 0; for (int i = 0, len = vector.GetLength(0); i < len; i++) { if (1M - vector[i, 0] <= .1M) { type = i; count++; } } return(count > 1?-1:type); }
/// <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); }
/// <summary> /// 克罗内克积 /// </summary> /// <param name="matrix"></param> /// <returns></returns> public Matrix Kronecker(Matrix matrix) { return(new Matrix(MatrixHelper.MatrixKronecker(InnerMatrix, matrix.InnerMatrix))); }
/// <summary> /// 创建单位矩阵 /// </summary> /// <param name="width"></param> public Matrix(int width) { InnerMatrix = MatrixHelper.CreateIdentityMatrix(width); }
/// <summary> /// 逐元素乘法 /// </summary> /// <param name="matrix"></param> /// <returns></returns> public Matrix Dot(Matrix matrix) { return(new Matrix(MatrixHelper.MatrixDotMatrix(InnerMatrix, matrix.InnerMatrix))); }