public void Hinge() { double actual; actual = new HingeLoss(new[] { 0, 1, -0.5, 0.8 }).Loss(new[] { 0, 1, -0.5, 0.8 }); Assert.AreEqual(1.7, actual); actual = new HingeLoss(new[] { 0, 1, -0.5, 0.8 }).Loss(new[] { 0, 1, 0.5, 0.8 }); Assert.AreEqual(2.7, actual); actual = new HingeLoss(new[] { 0, 1, -0.5, 0.8 }).Loss(new[] { -5, 1, 0.5, 0.8 }); Assert.AreEqual(1.7, actual); actual = new HingeLoss(new[] { 5.4, 1, -0.5, 0.8 }).Loss(new[] { -5.2, 1, 0.5, 0.8 }); Assert.AreEqual(7.9, actual, 1e-10); actual = new HingeLoss(new int[] { 0, 1, 0, 0 }).Loss(new double[] { 1, 1, 1, 1 }); Assert.AreEqual(2, actual); actual = new HingeLoss(new int[] { 0, 1, 0, 0 }).Loss(new double[] { 0, 0, 0, 0 }); Assert.AreEqual(4, actual); actual = new HingeLoss(new int[] { -1, 1, -1, -1 }).Loss(new double[] { -1, -1, -1, -1 }); Assert.AreEqual(6, actual); actual = new HingeLoss(new int[] { 0, 0, 0, 1 }).Loss(new double[] { -1, 1, 1, 1 }); Assert.AreEqual(4, actual); actual = new HingeLoss(new int[] { 0, 0, 0, 1 }).Loss(new double[] { -1, -1, -1, 1 }); Assert.AreEqual(8, actual); actual = new HingeLoss(new int[] { -1, -1, -1, 1 }).Loss(new double[] { 0, 0, 0, 1 }); Assert.AreEqual(5, actual); actual = new HingeLoss(new double[] { 0, 1, 0, 0 }).Loss(new double[] { 0, 0, 0, 0 }); Assert.AreEqual(4, actual); Assert.AreEqual(0, new HingeLoss().Loss(1, 1)); Assert.AreEqual(2, new HingeLoss().Loss(-1, 1)); Assert.AreEqual(2, new HingeLoss().Loss(1, -1)); Assert.AreEqual(0, new HingeLoss().Loss(-1, -1)); Assert.AreEqual(0, new HingeLoss().Loss(1, 5)); Assert.AreEqual(7, new HingeLoss().Loss(-1, 6)); Assert.AreEqual(8, new HingeLoss().Loss(1, -7)); Assert.AreEqual(0, new HingeLoss().Loss(-1, -8)); Assert.AreEqual(2, new HingeLoss().Loss(-1, 1)); Assert.AreEqual(1, new HingeLoss().Loss(1, 0)); Assert.AreEqual(1, new HingeLoss().Loss(-1, 0)); Assert.AreEqual(0, new HingeLoss().Loss(true, 5)); Assert.AreEqual(7, new HingeLoss().Loss(false, 6)); Assert.AreEqual(8, new HingeLoss().Loss(true, -7)); Assert.AreEqual(0, new HingeLoss().Loss(false, -8)); Assert.AreEqual(2, new HingeLoss().Loss(false, 1)); Assert.AreEqual(1, new HingeLoss().Loss(true, 0)); Assert.AreEqual(1, new HingeLoss().Loss(false, 0)); }
public void SdcaMulticlass() { var env = new ConsoleEnvironment(seed: 0); var dataPath = GetDataPath(TestDatasets.iris.trainFilename); var dataSource = new MultiFileSource(dataPath); var ctx = new MulticlassClassificationContext(env); var reader = TextLoader.CreateReader(env, c => (label: c.LoadText(0), features: c.LoadFloat(1, 4))); MulticlassLogisticRegressionPredictor pred = null; var loss = new HingeLoss(new HingeLoss.Arguments() { Margin = 1 }); // With a custom loss function we no longer get calibrated predictions. var est = reader.MakeNewEstimator() .Append(r => (label: r.label.ToKey(), r.features)) .Append(r => (r.label, preds: ctx.Trainers.Sdca( r.label, r.features, maxIterations: 2, loss: loss, onFit: p => pred = p))); var pipe = reader.Append(est); Assert.Null(pred); var model = pipe.Fit(dataSource); Assert.NotNull(pred); VBuffer <float>[] weights = default; pred.GetWeights(ref weights, out int n); Assert.True(n == 3 && n == weights.Length); foreach (var w in weights) { Assert.True(w.Length == 4); } var biases = pred.GetBiases(); Assert.True(biases.Count() == 3); var data = model.Read(dataSource); // Just output some data on the schema for fun. var schema = data.AsDynamic.Schema; for (int c = 0; c < schema.ColumnCount; ++c) { Console.WriteLine($"{schema.GetColumnName(c)}, {schema.GetColumnType(c)}"); } var metrics = ctx.Evaluate(data, r => r.label, r => r.preds, 2); Assert.True(metrics.LogLoss > 0); Assert.True(metrics.TopKAccuracy > 0); }
public void HingeLoss_Loss_2() { var sut = new HingeLoss(); var targets = Matrix <float> .Build.Dense(3, 2, new float[] { 1f, 1f, 0f, 0f, 0f, 1, }); var predictions = Matrix <float> .Build.Dense(3, 2, new float[] { 0.9f, 0.9f, 0.1f, .1f, .1f, .9f }); var actual = sut.Loss(targets, predictions); Assert.AreEqual(0.200000018, actual, 0.001); }
public void HingeLoss_Loss() { // example from http://cs231n.github.io/linear-classify/#svmvssoftmax var sut = new HingeLoss(); var targets = Matrix <float> .Build.Dense(1, 3, new float[] { 0, 0, 1 }); var predictions = Matrix <float> .Build.Dense(1, 3, new float[] { -2.85f, 0.86f, 0.28f }); var actual = sut.Loss(targets, predictions); Assert.AreEqual(1.58, actual, 0.001); }
public void SdcaBinaryClassificationNoCalibration() { var env = new ConsoleEnvironment(seed: 0); var dataPath = GetDataPath(TestDatasets.breastCancer.trainFilename); var dataSource = new MultiFileSource(dataPath); var ctx = new BinaryClassificationContext(env); var reader = TextLoader.CreateReader(env, c => (label: c.LoadBool(0), features: c.LoadFloat(1, 9))); LinearBinaryPredictor pred = null; var loss = new HingeLoss(new HingeLoss.Arguments() { Margin = 1 }); // With a custom loss function we no longer get calibrated predictions. var est = reader.MakeNewEstimator() .Append(r => (r.label, preds: ctx.Trainers.Sdca(r.label, r.features, maxIterations: 2, loss: loss, onFit: p => pred = p))); var pipe = reader.Append(est); Assert.Null(pred); var model = pipe.Fit(dataSource); Assert.NotNull(pred); // 9 input features, so we ought to have 9 weights. Assert.Equal(9, pred.Weights2.Count); var data = model.Read(dataSource); var metrics = ctx.Evaluate(data, r => r.label, r => r.preds); // Run a sanity check against a few of the metrics. Assert.InRange(metrics.Accuracy, 0, 1); Assert.InRange(metrics.Auc, 0, 1); Assert.InRange(metrics.Auprc, 0, 1); // Just output some data on the schema for fun. var schema = data.AsDynamic.Schema; for (int c = 0; c < schema.ColumnCount; ++c) { Console.WriteLine($"{schema.GetColumnName(c)}, {schema.GetColumnType(c)}"); } }
public void LossHinge() { var loss = new HingeLoss(); // Positive examples. TestHelper(loss, 1, 2, 0, 0); TestHelper(loss, 1, 1, 0, 0, false); TestHelper(loss, 1, 0.99, 0.01, 1, false); TestHelper(loss, 1, 0.5, 0.5, 1); // Negative examples. TestHelper(loss, 0, 0.5, 1.5, -1); TestHelper(loss, 0, -0.5, 0.5, -1); TestHelper(loss, 0, -1, 0, 0, false); TestHelper(loss, 0, -2, 0, 0); }
public void LossHinge() { HingeLoss.Arguments args = new HingeLoss.Arguments(); HingeLoss loss = new HingeLoss(args); // Positive examples. TestHelper(loss, 1, 2, 0, 0); TestHelper(loss, 1, 1, 0, 0, false); TestHelper(loss, 1, 0.99, 0.01, 1, false); TestHelper(loss, 1, 0.5, 0.5, 1); // Negative examples. TestHelper(loss, 0, 0.5, 1.5, -1); TestHelper(loss, 0, -0.5, 0.5, -1); TestHelper(loss, 0, -1, 0, 0, false); TestHelper(loss, 0, -2, 0, 0); }
public void SdcaBinaryClassificationNoClaibration() { var env = new TlcEnvironment(seed: 0); var dataPath = GetDataPath("breast-cancer.txt"); var dataSource = new MultiFileSource(dataPath); var reader = TextLoader.CreateReader(env, c => (label: c.LoadBool(0), features: c.LoadFloat(1, 9))); LinearBinaryPredictor pred = null; var loss = new HingeLoss(new HingeLoss.Arguments() { Margin = 1 }); // With a custom loss function we no longer get calibrated predictions. var est = reader.MakeNewEstimator() .Append(r => (r.label, preds: r.label.PredictSdcaBinaryClassification(r.features, maxIterations: 2, loss: loss, onFit: p => pred = p))); var pipe = reader.Append(est); Assert.Null(pred); var model = pipe.Fit(dataSource); Assert.NotNull(pred); // 9 input features, so we ought to have 9 weights. Assert.Equal(9, pred.Weights2.Count); var data = model.Read(dataSource); // Just output some data on the schema for fun. var rows = DataViewUtils.ComputeRowCount(data.AsDynamic); var schema = data.AsDynamic.Schema; for (int c = 0; c < schema.ColumnCount; ++c) { Console.WriteLine($"{schema.GetColumnName(c)}, {schema.GetColumnType(c)}"); } }
public void AveragePerceptronNoCalibration() { var env = new ConsoleEnvironment(seed: 0); var dataPath = GetDataPath(TestDatasets.breastCancer.trainFilename); var dataSource = new MultiFileSource(dataPath); var ctx = new BinaryClassificationContext(env); var reader = TextLoader.CreateReader(env, c => (label: c.LoadBool(0), features: c.LoadFloat(1, 9))); LinearBinaryPredictor pred = null; var loss = new HingeLoss(new HingeLoss.Arguments() { Margin = 1 }); var est = reader.MakeNewEstimator() .Append(r => (r.label, preds: ctx.Trainers.AveragedPerceptron(r.label, r.features, lossFunction: loss, numIterations: 2, onFit: p => pred = p))); var pipe = reader.Append(est); Assert.Null(pred); var model = pipe.Fit(dataSource); Assert.NotNull(pred); // 9 input features, so we ought to have 9 weights. Assert.Equal(9, pred.Weights2.Count); var data = model.Read(dataSource); var metrics = ctx.Evaluate(data, r => r.label, r => r.preds); // Run a sanity check against a few of the metrics. Assert.InRange(metrics.Accuracy, 0, 1); Assert.InRange(metrics.Auc, 0, 1); Assert.InRange(metrics.Auprc, 0, 1); }
public static NS.Tuple <Machine, Solver> train_one(Problem prob, Parameters param, out double[] w, double Cp, double Cn) { Sparse <double>[] inputs = prob.Inputs; bool[] labels = prob.Outputs.Apply(x => x >= 0); // Create the learning algorithm from the parameters var teacher = create_solver(param, Cp, Cn, inputs, labels); Trace.WriteLine("Training " + param.Solver); // Run the learning algorithm var sw = Stopwatch.StartNew(); var svm = teacher.Learn(inputs, labels); sw.Stop(); double error = new HingeLoss(labels).Loss(svm.Score(inputs)); // Save the solution w = svm.ToWeights(); Trace.WriteLine(String.Format("Finished {0}: {1} in {2}", param.Solver, error, sw.Elapsed)); return(NS.Tuple.Create(svm, teacher)); }
public static void train_one(Problem prob, Parameters param, out double[] w, double Cp, double Cn) { double[][] inputs = prob.Inputs; int[] labels = prob.Outputs.Apply(x => x >= 0 ? 1 : -1); // Create the learning algorithm from the parameters var teacher = create(param, Cp, Cn, inputs, labels); Trace.WriteLine("Training " + param.Solver); // Run the learning algorithm var sw = Stopwatch.StartNew(); SupportVectorMachine svm = teacher.Learn(inputs, labels); sw.Stop(); double error = new HingeLoss(labels).Loss(svm.Score(inputs)); // Save the solution w = svm.ToWeights(); Trace.WriteLine(String.Format("Finished {0}: {1} in {2}", param.Solver, error, sw.Elapsed)); }