public ComputeGradient(List<List<string>> inputSentence, List<List<string>> tagsList,
     List<string> tagList, double lambda, double learningParam, FeatureCache cache, WriteModel logger)
 {
     Logger = logger;
     _inputSentence = inputSentence;
     _outputTagsList = tagsList;
     _tagList = tagList;
     _lambda = lambda;
     _learningParam = learningParam;
     _cache = cache;
     forwardBackwordAlgos = new List<ForwardBackwordAlgo>();
     _weightVector = null;
     _twoGramsList = new string[4];
     _twoGramPair = new KeyValuePair<string, string>[4];
     var ngramTags = new Tags(_tagList);
     int index = 0;
     foreach (var ngram in ngramTags.GetNGramTags(2))
     {
         if (index >= _twoGramsList.Length)
         {
             Array.Resize(ref _twoGramsList, index+1);
             Array.Resize(ref _twoGramPair, index + 1);
         }
         string[] split = ngram.Split(new[] { ':' });
         _twoGramsList[index] = split[0] +"@#"+ split[1];
         _twoGramPair[index] = new KeyValuePair<string, string>(split[0], split[1]);
         index++;
     }
 }
Example #2
0
 public ComputeGradient(List<List<string>> inputSentence, List<List<string>> tagsList,
     List<string> tagList, double lambda, FeatureCache cache)
 {
     _inputSentence = inputSentence;
     _outputTagsList = tagsList;
     _tagList = tagList;
     _lambda = lambda;
     _cache = cache;
     forwardBackwordAlgos = new List<ForwardBackwordAlgo>();
     _weightVector = null;
 }
        static void TrainingTest(List<string> tags)
        {
            //const string modelFile = "../../data/gene.key.model";
            //const string input = "../../data/gene.key";

            const string modelFile = "../../data/training/tag.model.trial1";
            const string input = "../../data/training/NYT_19980403_parsed.key";
            string LoggerFile = "../../Logs/Log_"+DateTime.Now.ToFileTime()+".txt";
            const int threadCount = 1;
            var perceptron = new Perceptron(input, modelFile, tags);
            perceptron.Train();
            perceptron.ReMapFeatureToK();
            //perceptron.Dump();
            perceptron.MapFeatures.Dump();
            perceptron.ReadInputs();
            var featureCache = new FeatureCache(perceptron.InputSentences, tags,
                perceptron.MapFeatures.DictFeaturesToK);
            featureCache.CreateCache();
            var logger = new WriteModel(LoggerFile);
            var gradient = new ComputeGradient(perceptron.InputSentences, perceptron.TagsList,
                tags, .1, featureCache, logger);
            //perceptron.WeightVector.ResetAllToZero();
            gradient.RunIterations(perceptron.WeightVector, 10, threadCount);
            gradient.Dump(modelFile, perceptron.MapFeatures.DictKToFeatures);
        }
Example #4
0
        static void TrainingTest(List<string> tags)
        {
            //const string modelFile = "../../data/gene.key.model";
            //const string input = "../../data/gene.key";

            const string modelFile = "../../data/training/tag.model.trial1";
            const string input = "../../data/training/NYT_19980403_parsed.key";
            var perceptron = new Perceptron(input, modelFile, tags);
            perceptron.Train();
            perceptron.ReMapFeatureToK();
            perceptron.Dump();
            perceptron.MapFeatures.Dump();
            perceptron.ReadInputs();
            var featureCache = new FeatureCache(perceptron.InputSentences, tags,
                perceptron.MapFeatures.DictFeaturesToK);
            featureCache.CreateCache();
            var gradient = new ComputeGradient(perceptron.InputSentences, perceptron.TagsList,
                tags, .1, featureCache);
            gradient.RunIterations(perceptron.WeightVector, 10);
            gradient.Dump(modelFile, perceptron.MapFeatures.DictKToFeatures);
        }