public static float ComputeSupervisedLoss(SparseMatrix Dt, DenseMatrix y, string OutputType)
{
if (Dt.nCols != y.nCols || Dt.nRows != y.nRows)
{
throw new Exception("The numbers of samples from label and prediction do not match.");
}
DenseMatrix TmpDenseMat = new DenseMatrix(y);
SparseMatrix TmpSparseMat = new SparseMatrix(Dt);
DenseRowVector TmpDenseRowVec = new DenseRowVector(Dt.nCols);
float TrainingLoss = 0.0f;
switch (OutputType)
{
case "softmaxCE":
MatrixOperation.ScalarAddMatrix(TmpDenseMat, y, 1e-20f);
MatrixOperation.Log(TmpDenseMat);
MatrixOperation.ElementwiseMatrixMultiplyMatrix(TmpSparseMat, Dt, TmpDenseMat);
MatrixOperation.VerticalSumMatrix(TmpDenseRowVec, TmpSparseMat);
TrainingLoss = TmpDenseRowVec.Sum() * (-1.0f);
break;
case "linearQuad":
MatrixOperation.MatrixSubtractMatrix(TmpDenseMat, Dt);
MatrixOperation.ElementwiseSquare(TmpDenseMat);
MatrixOperation.VerticalSumMatrix(TmpDenseRowVec, TmpDenseMat);
TrainingLoss = TmpDenseRowVec.Sum();
break;
case "linearCE":
MatrixOperation.ScalarAddMatrix(TmpDenseMat, y, 1e-20f);
MatrixOperation.Log(TmpDenseMat);
MatrixOperation.ElementwiseMatrixMultiplyMatrix(TmpSparseMat, Dt, TmpDenseMat);
MatrixOperation.VerticalSumMatrix(TmpDenseRowVec, TmpSparseMat);
TrainingLoss = TmpDenseRowVec.Sum() * (-1.0f);
break;
default:
throw new Exception("Unknown OutputType.");
}
return TrainingLoss;
}
public static float ComputeSupervisedLoss(SparseMatrix Dt, SparseMatrix y, string OutputType)