Exemplo n.º 1
0
        /// <summary>Samples a single position in the sequence.</summary>
        /// <remarks>
        /// Samples a single position in the sequence.
        /// Does not modify the sequence passed in.
        /// returns the score of the new label for the position to sample
        /// </remarks>
        /// <param name="sequence">the sequence to start with</param>
        /// <param name="pos">the position to sample.</param>
        /// <param name="temperature">the temperature to control annealing</param>
        private Pair <int, double> SamplePositionHelper(ISequenceModel model, int[] sequence, int pos, double temperature)
        {
            double[] distribution = model.ScoresOf(sequence, pos);
            if (temperature != 1.0)
            {
                if (temperature == 0.0)
                {
                    // set the max to 1.0
                    int argmax = ArrayMath.Argmax(distribution);
                    Arrays.Fill(distribution, double.NegativeInfinity);
                    distribution[argmax] = 0.0;
                }
                else
                {
                    // take all to a power
                    // use the temperature to increase/decrease the entropy of the sampling distribution
                    ArrayMath.MultiplyInPlace(distribution, 1.0 / temperature);
                }
            }
            ArrayMath.LogNormalize(distribution);
            ArrayMath.ExpInPlace(distribution);
            int    newTag  = ArrayMath.SampleFromDistribution(distribution, random);
            double newProb = distribution[newTag];

            return(new Pair <int, double>(newTag, newProb));
        }
        public virtual void TestExpLogInplace()
        {
            ArrayMath.ExpInPlace(d1);
            ArrayMath.LogInPlace(d1);
            ArrayMath.PairwiseSubtractInPlace(d1, d2);
            double norm2 = ArrayMath.Norm(d1);

            NUnit.Framework.Assert.AreEqual(norm2, 1e-5, 0.0);
        }