Exemplo n.º 1
0
        /// <summary>
        /// <para>Returns an indication of similarity between vector <c>v1</c> and <c>v2</c>.</para>
        /// <para>In this case, we use a sigmoid function.</para>
        /// </summary>
        protected override double KernelFunc(FeatureVector v1, FeatureVector v2)
        {
            double result = 0;

            foreach (int f_i in v2.FeatureUnionWith(v2))
            {
                result += v1.Features[f_i] * v2.Features[f_i];
            }
            return(Math.Tanh((Gamma * result) + Coef));
        }
Exemplo n.º 2
0
        /// <summary>
        /// <para>Returns an indication of similarity between vector <c>v1</c> and <c>v2</c>.</para>
        /// <para>In this case, we use a radial basis function.</para>
        /// </summary>
        protected override double KernelFunc(FeatureVector v1, FeatureVector v2)
        {
            double result = 0;

            foreach (int f_i in v1.FeatureUnionWith(v2))
            {
                double diff = Math.Abs(v1.Features[f_i] - v2.Features[f_i]);
                result += (diff * diff);
            }
            // Optimization:
            // Here, you would usually square root the result to get the euclidean distance,
            // but this kernel would ordinarily square the distance anyway.
            // So, here we have just skipped these steps.
            return(Math.Pow(Math.E, -1 * Gamma * result));
        }
Exemplo n.º 3
0
        /// <summary>Calculates the Squared Euclidean Distance between two vectors.</summary>
        private double CalculateAbsSquaredEuclideanDistance(FeatureVector v1, FeatureVector v2)
        {
            double distance = 0;

            foreach (int f_i in v1.FeatureUnionWith(v2))
            {
                // Ignore OOV.
                if (f_i >= NoOfFeatures)
                {
                    continue;
                }
                double diff = (v2.Features[f_i] - v1.Features[f_i]);
                distance += (diff * diff);
            }
            return(distance);
        }