Last active
August 29, 2015 13:57
-
-
Save stephentu/9405802 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// computes and steps along the gradient of the SVM objective function: Sum_i HingeLoss(1.0 - normal^Tx_i y_i) + ||w||_2^2 | |
public static double[] SupportVectorStep(PINQueryable<Example> input, double[] normal, double epsilon) | |
{ | |
// select the examples that are currently mis-labeled by the normal vector. also add some negative normal for our regularizer | |
var errors = input.Where(x => x.label * x.vector.Select((v, i) => v * normal[i]).Sum() < 1.0) | |
.Concat(Enumerable.Repeat(new Example(normal, -1.0), 10).AsQueryable()); | |
// fold the average error into the normal | |
var newnormal = new double[normal.Length]; | |
foreach (var coordinate in Enumerable.Range(0, normal.Length)) | |
newnormal[coordinate] = normal[coordinate] + errors.NoisyAverage(epsilon, x => x.label * x.vector[coordinate]); | |
return newnormal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment