public static double RBFKernel(Vector w, Vector x, double c) { double sum = 0; for (int i = 0; i < x.getDimension(); i++) { sum += Math.pow(w.get(i) - x.get(i), 2); } return Math.exp(-sum / c); }
/** * Trains the classifier for Multiclass SVM, by using a label for One-vs-All training. * * @param examples - The examples to train on * @param rate0 - the learning rate * @param C - The C term * @param epochT - The number of epochs * @param label - The label to train for * @return - Weight vector for the classifier. */ public static Vector SGD( ArrayList<Vector> examples, double rate0, double C, int epochT, String label) { double rate; int t = 0; Vector w = new Vector(examples.get(0).getDimension()); for (int epoch = 0; epoch < epochT; epoch++) { examples = Util.shuffle(examples); for (int i = 0; i < examples.size(); i++) { Vector x = examples.get(i); rate = rate0 / (1 + (rate0 * t) / C); Vector E = w; if (x.getLabel(label) * LinearKernel(w, x, 1) <= 1) E = w.add(x.scale(-1 * C * x.getLabel(label))); w = w.add(E.scale(-1 * rate)); t++; } } return w; }
public static double LinearKernel(Vector w, Vector x, int power) { return Math.pow(w.transpose(x), power); }