public void FunctionTest() { Linear dense = new Linear(1); SparseLinear target = new SparseLinear(1); double[] sx = { 1, -0.555556, 2, +0.250000, 3, -0.864407, 4, -0.916667 }; double[] sy = { 1, -0.666667, 2, -0.166667, 3, -0.864407, 4, -0.916667 }; double[] sz = { 1, -0.944444, 3, -0.898305, 4, -0.916667 }; double[] dx = { -0.555556, +0.250000, -0.864407, -0.916667 }; double[] dy = { -0.666667, -0.166667, -0.864407, -0.916667 }; double[] dz = { -0.944444, +0.000000, -0.898305, -0.916667 }; double expected, actual; expected = dense.Function(dx, dy); actual = target.Function(sx, sy); Assert.AreEqual(expected, actual, 1e-10); expected = dense.Function(dx, dz); actual = target.Function(sx, sz); Assert.AreEqual(expected, actual, 1e-10); expected = dense.Function(dy, dz); actual = target.Function(sy, sz); Assert.AreEqual(expected, actual, 1e-10); }
/// <summary> /// Polynomial kernel function. /// </summary> /// /// <param name="x">Vector <c>x</c> in input space.</param> /// <param name="y">Vector <c>y</c> in input space.</param> /// <returns>Dot product in feature (kernel) space.</returns> /// public override double Function(double[] x, double[] y) { #pragma warning disable 0618 double sum = SparseLinear.Product(x, y) + constant; return(Math.Pow(sum, Degree)); }
/// <summary> /// Sigmoid kernel function. /// </summary> /// /// <param name="x">Vector <c>x</c> in input space.</param> /// <param name="y">Vector <c>y</c> in input space.</param> /// <returns>Dot product in feature (kernel) space.</returns> /// public override double Function(double[] x, double[] y) { #pragma warning disable 0618 double sum = SparseLinear.Product(x, y); return(System.Math.Tanh(Gamma * sum + Constant)); }
/// <summary> /// Computes the distance in input space /// between two points given in feature space. /// </summary> /// /// <param name="x">Vector <c>x</c> in feature (kernel) space.</param> /// <param name="y">Vector <c>y</c> in feature (kernel) space.</param> /// /// <returns>Distance between <c>x</c> and <c>y</c> in input space.</returns> /// public override double Distance(double[] x, double[] y) { if (x == y) { return(0.0); } double norm = SparseLinear.SquaredEuclidean(x, y); return(2 - 2 * Math.Exp(-gamma * Math.Sqrt(norm))); }
/// <summary> /// Computes the squared distance in input space /// between two points given in feature space. /// </summary> /// /// <param name="x">Vector <c>x</c> in feature (kernel) space.</param> /// <param name="y">Vector <c>y</c> in feature (kernel) space.</param> /// /// <returns> /// Squared distance between <c>x</c> and <c>y</c> in input space. /// </returns> /// public double ReverseDistance(double[] x, double[] y) { if (x == y) { return(0.0); } double norm = SparseLinear.SquaredEuclidean(x, y); return(-(1.0 / gamma) * Math.Log(1.0 - 0.5 * norm)); }
/// <summary> /// Gaussian Kernel function. /// </summary> /// /// <param name="x">Vector <c>x</c> in input space.</param> /// <param name="y">Vector <c>y</c> in input space.</param> /// <returns>Dot product in feature (kernel) space.</returns> /// public override double Function(double[] x, double[] y) { // Optimization in case x and y are // exactly the same object reference. if (x == y) { return(1.0); } double norm = SparseLinear.SquaredEuclidean(x, y); return(Math.Exp(-gamma * norm)); }
/// <summary> /// Cauchy Kernel Function /// </summary> /// /// <param name="x">Vector <c>x</c> in input space.</param> /// <param name="y">Vector <c>y</c> in input space.</param> /// /// <returns>Dot product in feature (kernel) space.</returns> /// public override double Function(double[] x, double[] y) { // Optimization in case x and y are // exactly the same object reference. if (x == y) { return(1.0); } #pragma warning disable 0618 double norm = SparseLinear.SquaredEuclidean(x, y); return(1.0 / (1.0 + norm / Sigma)); }
/// <summary> /// Polynomial kernel function. /// </summary> /// /// <param name="x">Vector <c>x</c> in input space.</param> /// <param name="y">Vector <c>y</c> in input space.</param> /// <returns>Dot product in feature (kernel) space.</returns> /// public override double Function(double[] x, double[] y) { double sum = SparseLinear.Product(x, y) + constant; return(Math.Pow(sum, Degree)); }