private double Compute(int k, WeightVector weightVector)
        {
            double output = 0;
            //double secondTerm = 0;
            int lineIndex = 0;
            //var weightedFeaturesum = new WeightedFeatureSum(weightVector, null, true);

            if (_inputSentence.Count != _outputTagsList.Count)
            {
                throw new Exception("counts dont match "+ _inputSentence.Count + "with "+ _outputTagsList.Count);
            }
            var ngramTags = new Tags(_tagList);

            // first term.
            foreach (var sentence in _inputSentence)
            {
                var outputTags = _outputTagsList[lineIndex];

                if (sentence.Count != outputTags.Count)
                {
                    throw new Exception("compute counts dont match " + sentence.Count + "with " + outputTags.Count);
                }

                output += CalculateGradient(outputTags, k,
                    ngramTags, lineIndex);

                //output += weightedFeaturesum.GetAllFeatureK(outputTags, k, sentence);

                //// second term.
                //for (var j = 0; j < outputTags.Count; j++)
                //{
                //    double sum = 0;
                //    foreach (var ngramTag in ngramTags.GetNGramTags(2))
                //    {
                //        string[] split = ngramTag.Split(new[] {':'});
                //        sum += (forwardBackwordAlgos[i].GetQ(j, split[0], split[1]) *
                //            weightedFeaturesum.GetFeatureK(split[0], split[1], j, k, sentence));
                //    }
                //    secondTerm += sum;
                //}
                lineIndex++;
            }

            output = output - (_lambda*weightVector.Get(k));
            return output;
        }
 public WeightVector RunIterations(WeightVector weightVector, int iterationCount)
 {
     for (int iter = 0; iter < iterationCount; iter++)
     {
         Console.WriteLine(DateTime.Now + " running iteration " + iter);
         var newWeightVector = new WeightVector(weightVector.FeatureKDictionary);
         SetForwardBackwordAlgo(weightVector);
         //for (var k = 0; k < weightVector.FeatureKDictionary.Count; k++)
         for (var k = weightVector.FeatureKDictionary.Count-1; k >= 0; k--)
         {
             if (k%100 == 0)
             {
                 Console.WriteLine(DateTime.Now + " running iteration for k " + k);
             }
             var wk = Compute(k, weightVector);
             wk = weightVector.Get(k) + _lambda*wk;
             newWeightVector.SetKey(k, wk);
         }
         weightVector = newWeightVector;
     }
     _weightVector = weightVector;
     return weightVector;
 }
        public void ComputeGradientValues(WeightVector weightVector, double[] gradient,
            int startIndex, int endIndex )
        {
            for (var k = startIndex; k < endIndex; k++)
            {
                double outputDouble = 0;

                var kstring = "@#" + k.ToString(CultureInfo.InvariantCulture);
                for (var lineIndex = 0; lineIndex < _inputSentence.Count; lineIndex++)
                {
                    var outputTags = _outputTagsList[lineIndex];

                    double initOutputDouble = 0;
                    initOutputDouble += GetAllFeatureKFromCache(outputTags, k, lineIndex);
                    initOutputDouble -= CalculateGradient(outputTags, k,
                        lineIndex, kstring);

                    outputDouble += initOutputDouble;
                }

                gradient[k] = outputDouble - (_lambda * weightVector.Get(k));
            }
        }