public static bool VerifyLossFuncDerivative(LossFunc func, Tensor targetOutput, bool onlyPositiveOutput = false, int batchSize = 1, float tolerance = 0.01f) { Tensor.SetOpMode(Tensor.OpMode.CPU); var output = new Tensor(new Shape(3, 3, 3, batchSize)); output.FillWithRand(10, onlyPositiveOutput ? 0 : -1); // for derivation purposes activation functions expect already processed input var error = new Tensor(output.Shape); func.Compute(targetOutput, output, error); var derivative = new Tensor(output.Shape); func.Derivative(targetOutput, output, derivative); var error1 = new Tensor(output.Shape); func.Compute(targetOutput, output.Sub(LOSS_DERIVATIVE_EPSILON), error1); var error2 = new Tensor(output.Shape); func.Compute(targetOutput, output.Add(LOSS_DERIVATIVE_EPSILON), error2); var result = new Tensor(output.Shape); error2.Sub(error1, result); var approxDerivative = result.Div(2 * LOSS_DERIVATIVE_EPSILON); return(approxDerivative.Equals(derivative, tolerance)); }
public void Optimize(Optimizers.OptimizerBase optimizer, LossFunc loss) { Optimizer = optimizer; Model.Optimize(); LossFuncs = new LossFunc[Model.GetOutputLayersCount()]; for (int i = 0; i < LossFuncs.Length; ++i) { LossFuncs[i] = loss; } }
public void Optimize(Optimizers.OptimizerBase optimizer, Dictionary <string, LossFunc> lossDict) { Optimizer = optimizer; Model.Optimize(); #if VALIDATION_ENABLED if (lossDict.Count != Model.GetOutputLayersCount()) { throw new Exception($"Mismatched number of loss functions ({lossDict.Count}) and output layers ({Model.GetOutputLayersCount()})!"); } #endif LossFuncs = new LossFunc[Model.GetOutputLayersCount()]; int i = 0; foreach (var outLayer in Model.GetOutputLayers()) { LossFuncs[i++] = lossDict[outLayer.Name]; } }
public static bool VerifyLossFunc(LossFunc func, Tensor targetOutput, Func <float, float, float> testFunc, bool onlyPositiveOutput = false, int batchSize = 1) { Tensor.SetOpMode(Tensor.OpMode.CPU); var output = new Tensor(new Shape(3, 3, 3, batchSize)); output.FillWithRand(10, onlyPositiveOutput ? 0 : -1); var error = new Tensor(output.Shape); func.Compute(targetOutput, output, error); for (int i = 0; i < output.Shape.Length; ++i) { if (Math.Abs(error.GetFlat(i) - testFunc(targetOutput.GetFlat(i), output.GetFlat(i))) > 1e-4) { return(false); } } return(true); }