public static NMatrix Softmax(NMatrix matrix) { var maxElem = matrix.Max(); var expMatrix = matrix.Map( (elem) => { return(Math.Exp(elem - maxElem)); }); double expSum = 0; for (uint i = 0; i < expMatrix.Row * expMatrix.Col; i++) { expSum += expMatrix[i]; } var resultMatrix = new NMatrix(expMatrix); for (uint i = 0; i < resultMatrix.Row * resultMatrix.Col; i++) { resultMatrix[i] = expMatrix[i] / expSum; } return(resultMatrix); }