public void CrossEntropyTest() { var error = 0.0; var actual = new Matrix(4, 1); actual.InRandomize(); var expected = new Matrix(4, 1); expected.InRandomize(); for (var i = 0; i < actual.Rows; i++) { for (var j = 0; j < actual.Columns; j++) { error += -expected[i, j] * Math.Log(actual[i, j]) + (1.0 - expected[i, j]) * Math.Log(1 - actual[i, j]); } } var cost = new CrossEntropyCostKernel(); var calculatedError = cost.Forward(actual, expected); Assert.IsTrue(Math.Abs(error - calculatedError) < 0.01f, "Cross Entropy Cost successful"); }
public void CrossEntropyPrimeTest() { var actual = new Matrix(4, 1); actual.InRandomize(); var expected = new Matrix(4, 1); expected.InRandomize(); var gradMatrix = actual.Duplicate(); for (var i = 0; i < actual.Rows; i++) { for (var j = 0; j < actual.Columns; j++) { gradMatrix[i, j] = (actual[i, j] - expected[i, j]) / ((1 - actual[i, j]) * actual[i, j]); } } var cost = new CrossEntropyCostKernel(); var calculatedMatrix = cost.Backward(actual, expected); Assert.IsTrue(gradMatrix == calculatedMatrix, "Cross Entropy Cost Derivative successful"); }