public void SoftmaxTest() { var a = new Matrix(2, 2); a.InRandomize(); var b = a.Duplicate(); var res = b.Duplicate(); var sumExp = 0.0; for (var i = 0; i < res.Rows; i++) { for (var j = 0; j < res.Columns; j++) { sumExp += Math.Exp(b[i, j]); } } for (var i = 0; i < res.Rows; i++) { for (var j = 0; j < res.Columns; j++) { res[i, j] = Math.Exp(b[i, j]) / sumExp; } } b = res; a = new SoftmaxKernel().Forward(a); Assert.IsTrue(a == b, "Softmax Activation successful"); }
public void SoftmaxPrimeTest() { var a = new Matrix(2, 2); a.InRandomize(); var b = a.Duplicate(); var res = b.Duplicate(); var sumExp = 0.0; for (var i = 0; i < res.Rows; i++) { for (var j = 0; j < res.Columns; j++) { sumExp += Math.Exp(b[i, j]); } } for (var i = 0; i < res.Rows; i++) { for (var j = 0; j < res.Columns; j++) { res[i, j] = Math.Exp(b[i, j]) / sumExp; } } b = res; var s = new SoftmaxKernel(); a = s.Forward(a); a = s.Backward(a); b.InMap((x) => Math.Exp(x) / sumExp * (1 - Math.Exp(x) / sumExp)); Assert.IsTrue(a == b, "Softmax Derivative successful"); }