コード例 #1
0
        ////
        /// <summary>
        /// 求delta_i
        /// </summary>
        /// <param name="empirical_e">@param empirical_e fi的期望</param>
        /// <param name="fi">@param fi fi的下标</param>
        /// <returns></returns>
        //private double iis_solve_delta(double empirical_e, int fi)
        private double iis_solve_delta(double empirical_e, string fi)
        {
            double delta = 0;
            double f_newton, df_newton;

            double[,] p_yx = calc_prob_y_given_x();

            int iters = 0;

            while (iters < 50)                                  // 牛顿法
            {
                f_newton = df_newton = 0;
                for (int i = 0; i < instances.Count; i++)
                {
                    MLInstance <int, int> instance = instances[i];
                    MLFeature <int>       tfeature = instance.Feature;
                    Feature feature = new Feature(tfeature);
                    int     index   = features.IndexOf(feature);
                    if (index == -1)
                    {
                        index = feature.getIndex(instance.Feature.Count);
                        if (string.Join("", features[index]) == string.Join("", feature))
                        {
                            features[index] = feature;
                        }
                    }
                    for (int y = minY; y <= maxY; y++)
                    {
                        int    f_sharp = apply_f_sharp(feature, y);
                        double prod    = p_yx[index, y] * functions[fi].Apply(feature, y) * Math.Exp(delta * f_sharp);
                        f_newton  += prod;
                        df_newton += prod * f_sharp;
                    }
                }
                f_newton  = empirical_e - f_newton / N;     // g
                df_newton = -df_newton / N;                 // g的导数

                if (Math.Abs(f_newton) < 0.0000001)
                {
                    return(delta);
                }

                double ratio = f_newton / df_newton;

                delta -= ratio;
                if (Math.Abs(ratio) < EPSILON)
                {
                    return(delta);
                }
                iters++;
            }
            return(double.NaN);                          //如果不收敛,返回NaN

            throw new Exception("IIS did not converge"); // w_i不收敛
        }
コード例 #2
0
        /**
         * 分类
         * @param instance
         * @return
         */
        public override int Classify(MLInstance <int, int> instance)
        {
            double max   = 0;
            int    label = -1;

            //for (int y = minY; y <= maxY; y++)
            for (int y = FeatureSummary.LabelList[0]; y <= FeatureSummary.LabelList[FeatureSummary.LabelCnt - 1]; y++)
            {
                double sum = 0;
                //for (int i = 0; i < functions.Count; i++)
                int i = 0;
                foreach (string key in functions.Keys)
                {
                    sum += Math.Exp(w[i] * functions[key].Apply((MLFeature <int>)instance.Feature, y));
                    i++;
                }
                if (sum > max)
                {
                    max   = sum;
                    label = y;
                }
            }
            return(label);
        }
コード例 #3
0
        ////public double CheckInstances(MLInstances<LabelT,FeatureT> TestList)
        ////{
        ////    List<MLInstance<LabelT, FeatureT>> list = new List<MLInstance<LabelT, FeatureT>>();
        ////    list.AddRange(TestList);
        ////    return CheckInstances(list);
        ////}

        public abstract LabelT Classify(MLInstance <LabelT, FeatureT> instances);