Exemplo n.º 1
0
        public void TestDim1()
        {
            var m   = new Multinomial(2018);
            var cdf = new[] { 1f };

            Assert.AreEqual(0, m.Sample(cdf));
            Assert.AreEqual(0, m.Sample(cdf));
            Assert.AreEqual(0, m.Sample(cdf));
        }
Exemplo n.º 2
0
        public void TestDim3Unscaled()
        {
            var m   = new Multinomial(2018);
            var cdf = new[] { 0.05f, 0.15f, 0.5f };

            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(1, m.Sample(cdf));
        }
Exemplo n.º 3
0
        public void TestDim3()
        {
            var m   = new Multinomial(2018);
            var cdf = new[] { 0.1f, 0.3f, 1.0f };

            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(1, m.Sample(cdf));
        }
Exemplo n.º 4
0
        public void Apply(TensorProxy tensorProxy, IList <int> actionIds, Dictionary <int, ActionBuffers> lastActions)
        {
            var agentIndex = 0;

            for (var i = 0; i < actionIds.Count; i++)
            {
                var agentId = actionIds[i];
                if (lastActions.ContainsKey(agentId))
                {
                    var actionBuffer = lastActions[agentId];
                    if (actionBuffer.IsEmpty())
                    {
                        actionBuffer         = new ActionBuffers(m_ActionSpec);
                        lastActions[agentId] = actionBuffer;
                    }
                    var discreteBuffer = actionBuffer.DiscreteActions;
                    for (var j = 0; j < m_ActionSize.Length; j++)
                    {
                        ComputeCdf(tensorProxy, agentIndex, m_StartActionIndices[j], m_ActionSize[j]);
                        discreteBuffer[j] = m_Multinomial.Sample(m_CdfBuffer, m_ActionSize[j]);
                    }
                }
                agentIndex++;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Draw samples from a multinomial distribution based on log-probabilities specified
        /// in tensor src. The samples will be saved in the dst tensor.
        /// </summary>
        /// <param name="src">2-D tensor with shape batch_size x num_classes</param>
        /// <param name="dst">Allocated tensor with size batch_size x num_samples</param>
        /// <param name="multinomial">Multinomial object used to sample values</param>
        /// <exception cref="NotImplementedException">
        /// Multinomial doesn't support integer tensors
        /// </exception>
        /// <exception cref="ArgumentException">Issue with tensor shape or type</exception>
        /// <exception cref="ArgumentNullException">
        /// At least one of the tensors is not allocated
        /// </exception>
        public static void Eval(TensorProxy src, TensorProxy dst, Multinomial multinomial)
        {
            if (src.DataType != typeof(float))
            {
                throw new NotImplementedException("Only float tensors are currently supported");
            }

            if (src.valueType != dst.valueType)
            {
                throw new ArgumentException(
                          "Source and destination tensors have different types!");
            }

            if (src.data == null || dst.data == null)
            {
                throw new ArgumentNullException();
            }

            if (src.data.batch != dst.data.batch)
            {
                throw new ArgumentException("Batch size for input and output data is different!");
            }

            var cdf = new float[src.data.channels];

            for (var batch = 0; batch < src.data.batch; ++batch)
            {
                // Find the class maximum
                var maxProb = float.NegativeInfinity;
                for (var cls = 0; cls < src.data.channels; ++cls)
                {
                    maxProb = Mathf.Max(src.data[batch, cls], maxProb);
                }

                // Sum the log probabilities and compute CDF
                var sumProb = 0.0f;
                for (var cls = 0; cls < src.data.channels; ++cls)
                {
                    sumProb += Mathf.Exp(src.data[batch, cls] - maxProb);
                    cdf[cls] = sumProb;
                }

                // Generate the samples
                for (var sample = 0; sample < dst.data.channels; ++sample)
                {
                    dst.data[batch, sample] = multinomial.Sample(cdf);
                }
            }
        }
Exemplo n.º 6
0
        public static Func <float> CreateMultiRangeUniformSampler(IList <float> intervals, int seed)
        {
            //RNG
            Random distr = new Random(seed);
            // Will be used to normalize intervalFuncs
            float sumIntervalSizes = 0;
            //The number of intervals
            int numIntervals = (intervals.Count / 2);

            // List that will store interval lengths
            float[] intervalSizes = new float[numIntervals];
            // List that will store uniform distributions
            IList <Func <float> > intervalFuncs = new Func <float> [numIntervals];

            // Collect all intervals and store as uniform distrus
            // Collect all interval sizes
            for (int i = 0; i < numIntervals; i++)
            {
                var min          = intervals[2 * i];
                var max          = intervals[2 * i + 1];
                var intervalSize = max - min;
                sumIntervalSizes += intervalSize;
                intervalSizes[i]  = intervalSize;
                intervalFuncs[i]  = () => min + (float)distr.NextDouble() * intervalSize;
            }
            // Normalize interval lengths
            for (int i = 0; i < numIntervals; i++)
            {
                intervalSizes[i] = intervalSizes[i] / sumIntervalSizes;
            }
            // Build cmf for intervals
            for (int i = 1; i < numIntervals; i++)
            {
                intervalSizes[i] += intervalSizes[i - 1];
            }
            Multinomial intervalDistr = new Multinomial(seed + 1);

            float MultiRange()
            {
                int sampledInterval = intervalDistr.Sample(intervalSizes);

                return(intervalFuncs[sampledInterval].Invoke());
            }

            return(MultiRange);
        }
Exemplo n.º 7
0
 public void CanSample()
 {
     var n = new Multinomial(largeP, 4);
     var d = n.Sample();
 }
        public void CanSample()
        {
            var n = new Multinomial(_largeP, 4);

            n.Sample();
        }
 public void FailSampleStatic()
 {
     Assert.That(() => Multinomial.Sample(new Random(0), _badP, 4), Throws.ArgumentException);
 }
 public void CanSampleStatic()
 {
     Multinomial.Sample(new Random(0), _largeP, 4);
 }
Exemplo n.º 11
0
 public void FailSampleStatic()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Multinomial.Sample(new Random(0), _badP, 4));
 }
 public void FailSampleStatic()
 {
     var d = Multinomial.Sample(new Random(), badP, 4);
 }
 public void CanSampleStatic()
 {
     var d = Multinomial.Sample(new Random(), largeP, 4);
 }