Skip to content

Instantly share code, notes, and snippets.

@stephentu
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephentu/9405802 to your computer and use it in GitHub Desktop.
Save stephentu/9405802 to your computer and use it in GitHub Desktop.
// 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