/// <summary>
 /// <inheritDoc/>
 ///
 /// </summary>
 public virtual double ScoreOf(int[] sequence)
 {
     if (models != null)
     {
         double score = 0;
         for (int i = 0; i < models.Length; i++)
         {
             score += wts[i] * models[i].ScoreOf(sequence);
         }
         return(score);
     }
     //return model1.scoreOf(sequence);
     return(model1Wt * model1.ScoreOf(sequence) + model2Wt * model2.ScoreOf(sequence));
 }
Exemple #2
0
        /// <summary>
        /// Finds the best sequence by collecting numSamples samples, scoring them, and then choosing
        /// the highest scoring sample.
        /// </summary>
        /// <returns>the array of type int representing the highest scoring sequence</returns>
        public virtual int[] FindBestUsingSampling(ISequenceModel model, int numSamples, int sampleInterval, int[] initialSequence)
        {
            IList samples = CollectSamples(model, numSamples, sampleInterval, initialSequence);

            int[]  best      = null;
            double bestScore = double.NegativeInfinity;

            foreach (object sample in samples)
            {
                int[]  sequence = (int[])sample;
                double score    = model.ScoreOf(sequence);
                if (score > bestScore)
                {
                    best      = sequence;
                    bestScore = score;
                    log.Info("found new best (" + bestScore + ")");
                    log.Info(ArrayMath.ToString(best));
                }
            }
            return(best);
        }
 public virtual void ExtendWith(int tag, ISequenceModel ts, int s)
 {
     ExtendWith(tag);
     int[] tags = TmpTags(ts.LeftWindow() + 1 + ts.RightWindow(), s);
     score += ts.ScoreOf(tags, Size() - ts.RightWindow() - 1);
 }
        private static int[] BestSequenceOld(ISequenceModel ts)
        {
            int length = ts.Length();
            int leftWindow = ts.LeftWindow();
            int rightWindow = ts.RightWindow();
            int padLength = length + leftWindow + rightWindow;
            int[][] tags = new int[padLength][];
            int[] tagNum = new int[padLength];
            for (int pos = 0; pos < padLength; pos++)
            {
                tags[pos] = ts.GetPossibleValues(pos);
                tagNum[pos] = tags[pos].Length;
            }

            int[] tempTags = new int[padLength];
            int[] productSizes = new int[padLength];
            int curProduct = 1;
            for (int i = 0; i < leftWindow + rightWindow; i++)
                curProduct *= tagNum[i];
            for (int pos = leftWindow + rightWindow; pos < padLength; pos++)
            {
                if (pos > leftWindow + rightWindow)
                    curProduct /= tagNum[pos - leftWindow - rightWindow - 1];
                curProduct *= tagNum[pos];
                productSizes[pos - rightWindow] = curProduct;
            }

            double[][] windowScore = new double[padLength][];
            for (int pos = leftWindow; pos < leftWindow + length; pos++)
            {
                windowScore[pos] = new double[productSizes[pos]];
                Arrays.Fill(tempTags, tags[0][0]);
                for (int product = 0; product < productSizes[pos]; product++)
                {
                    int p = product;
                    for (int curPos = pos + rightWindow; curPos >= pos - leftWindow; curPos--)
                    {
                        tempTags[curPos] = tags[curPos][p % tagNum[curPos]];
                        p /= tagNum[curPos];
                    }

                    windowScore[pos][product] = ts.ScoreOf(tempTags, pos);
                }
            }

            double[][] score = new double[padLength][];
            int[][] trace = new int[padLength][];
            for (int pos = 0; pos < padLength; pos++)
            {
                score[pos] = new double[productSizes[pos]];
                trace[pos] = new int[productSizes[pos]];
            }

            for (int pos = leftWindow; pos < length + leftWindow; pos++)
            {
                for (int product = 0; product < productSizes[pos]; product++)
                {
                    if (pos == leftWindow)
                    {
                        score[pos][product] = windowScore[pos][product];
                        trace[pos][product] = -1;
                    }
                    else
                    {
                        score[pos][product] = Double.NegativeInfinity;
                        trace[pos][product] = -1;
                        int sharedProduct = product / tagNum[pos + rightWindow];
                        int factor = productSizes[pos] / tagNum[pos + rightWindow];
                        for (int newTagNum = 0; newTagNum < tagNum[pos - leftWindow - 1]; newTagNum++)
                        {
                            int predProduct = newTagNum * factor + sharedProduct;
                            double predScore = score[pos - 1][predProduct] + windowScore[pos][product];
                            if (predScore > score[pos][product])
                            {
                                score[pos][product] = predScore;
                                trace[pos][product] = predProduct;
                            }
                        }
                    }
                }
            }

            double bestFinalScore = Double.NegativeInfinity;
            int bestCurrentProduct = -1;
            for (int product = 0; product < productSizes[leftWindow + length - 1]; product++)
            {
                if (score[leftWindow + length - 1][product] > bestFinalScore)
                {
                    bestCurrentProduct = product;
                    bestFinalScore = score[leftWindow + length - 1][product];
                }
            }

            int lastProduct = bestCurrentProduct;
            for (int last = padLength - 1; last >= length - 1; last--)
            {
                tempTags[last] = tags[last][lastProduct % tagNum[last]];
                lastProduct /= tagNum[last];
            }

            for (int pos = leftWindow + length - 2; pos >= leftWindow; pos--)
            {
                int bestNextProduct = bestCurrentProduct;
                bestCurrentProduct = trace[pos + 1][bestNextProduct];
                tempTags[pos - leftWindow] = tags[pos - leftWindow][bestCurrentProduct / (productSizes[pos] / tagNum[pos - leftWindow])];
            }

            return tempTags;
        }
 public virtual void ExtendWith(int tag, ISequenceModel ts, int s)
 {
     ExtendWith(tag);
     int[] tags = TmpTags(ts.LeftWindow() + 1 + ts.RightWindow(), s);
     score += ts.ScoreOf(tags, Size() - ts.RightWindow() - 1);
 }
Exemple #6
0
        /// <summary>
        /// Samples the complete sequence once in the forward direction
        /// Destructively modifies the sequence in place.
        /// </summary>
        /// <param name="sequence">the sequence to start with.</param>
        public virtual double SampleSequenceForward(ISequenceModel model, int[] sequence, double temperature, ICollection <int> onlySampleThesePositions)
        {
            double returnScore = double.NegativeInfinity;

            // log.info("Sampling forward");
            if (onlySampleThesePositions != null)
            {
                foreach (int pos in onlySampleThesePositions)
                {
                    returnScore = SamplePosition(model, sequence, pos, temperature);
                }
            }
            else
            {
                if (samplingStyle == SequentialSampling)
                {
                    for (int pos = 0; pos < sequence.Length; pos++)
                    {
                        returnScore = SamplePosition(model, sequence, pos, temperature);
                    }
                }
                else
                {
                    if (samplingStyle == RandomSampling)
                    {
                        foreach (int aSequence in sequence)
                        {
                            int pos = random.NextInt(sequence.Length);
                            returnScore = SamplePosition(model, sequence, pos, temperature);
                        }
                    }
                    else
                    {
                        if (samplingStyle == ChromaticSampling)
                        {
                            // make copies of the sequences and merge at the end
                            IList <Pair <int, int> > results = new List <Pair <int, int> >();
                            foreach (IList <int> indieList in partition)
                            {
                                if (indieList.Count <= chromaticSize)
                                {
                                    foreach (int pos in indieList)
                                    {
                                        Pair <int, double> newPosProb = SamplePositionHelper(model, sequence, pos, temperature);
                                        sequence[pos] = newPosProb.First();
                                    }
                                }
                                else
                                {
                                    MulticoreWrapper <IList <int>, IList <Pair <int, int> > > wrapper = new MulticoreWrapper <IList <int>, IList <Pair <int, int> > >(chromaticSize, new _IThreadsafeProcessor_269(this, model, sequence, temperature));
                                    // returns the position to sample in first place and new label in second place
                                    results.Clear();
                                    int interval = System.Math.Max(1, indieList.Count / chromaticSize);
                                    for (int begin = 0; end < indieListSize; begin += interval)
                                    {
                                        end = System.Math.Min(begin + interval, indieListSize);
                                        wrapper.Put(indieList.SubList(begin, end));
                                        while (wrapper.Peek())
                                        {
                                            Sharpen.Collections.AddAll(results, wrapper.Poll());
                                        }
                                    }
                                    wrapper.Join();
                                    while (wrapper.Peek())
                                    {
                                        Sharpen.Collections.AddAll(results, wrapper.Poll());
                                    }
                                    foreach (Pair <int, int> posVal in results)
                                    {
                                        sequence[posVal.First()] = posVal.Second();
                                    }
                                }
                            }
                            returnScore = model.ScoreOf(sequence);
                        }
                    }
                }
            }
            return(returnScore);
        }