Ejemplo n.º 1
0
        /// <summary>
        /// Train Bayes Model with some optimization method
        /// </summary>
        public void ReTrain()
        {
            var model = BayesTest.LoadModel("bayesStatisticModelFile");
            var w     = new int[GetLabels().ToArray().Length];

            for (int i = 0; i < w.Length; i++)  // Initial weight vector
            {
                w[i] = 1;
            }
        }
Ejemplo n.º 2
0
        public void Train()
        {
            this.statisticModel  = BayesTest.LoadModel(@"D:\Codes\C#\EntityTyping\Fine-ner\unit test\output\model.txt");
            this.developFeatures = LoadBayesData(@"D:\Codes\C#\EntityTyping\Fine-ner\unit test\output\developFeature.txt");
            this.labels          = GetLabels().ToList();
            this.w = new Dictionary <string, double>(this.labels.Count);
            foreach (var label in labels)  // Initial weight vector
            {
                w[label] = 1;
            }
            var learnSpeed   = 0.005;
            var lastPositive = -1;

            this.positive = -1;
            int steps      = 0;
            var lastWeight = new Dictionary <string, double>(w);

            while (lastPositive == -1 || ((this.positive - lastPositive) > 0))
            {
                steps++;
                //learnSpeed /= steps;
                lastPositive = this.positive;
                var diff = GetDiff();
                lastWeight = new Dictionary <string, double>(w);
                foreach (var label in labels)
                {
                    w[label] -= diff[label] * learnSpeed;
                }
                Console.WriteLine("positive: " + positive);
                foreach (var label in labels)
                {
                    Console.Write(label + "\t" + w[label] + "\t");
                }
                Console.WriteLine("");
            }
            FileWriter writer = new LargeFileWriter(@"D:\Codes\C#\EntityTyping\Fine-ner\unit test\output\weight.txt", FileMode.Create);

            foreach (var label in labels)
            {
                writer.WriteLine(label + "\t" + lastWeight[label]);
            }
            writer.Close();
        }
Ejemplo n.º 3
0
 private void Test(HashSet <string> options)
 {
     if (options == null)
     {
         options = new HashSet <string>(new string[] { "b" });
     }
     // test with bayes model
     if (options.Contains("b"))
     {
         var tester = new BayesTest((string)GlobalParameter.Get(DefaultParameter.Field.model_file),
                                    (string)GlobalParameter.Get(DefaultParameter.Field.develop_feature_file),
                                    (string)GlobalParameter.Get(DefaultParameter.Field.test_result_file));
         try
         {
             tester.Test();
         }
         catch (Exception e)
         {
             Console.WriteLine("Error occurs during test for " + e.Message);
             throw new Exception();
         }
     }
 }