示例#1
0
        /// <summary>
        /// The Forward computation.
        /// </summary>
        /// <param name="colBottom">input blob vector (length 1)</param>
        /// <param name="colTop">output blob vector (length 1)</param>
        protected override void forward(BlobCollection <T> colBottom, BlobCollection <T> colTop)
        {
            float[] rgBottom = convertF(colBottom[0].mutable_cpu_data);
            int     nCount   = colBottom[0].count(0, m_nAxis);

            for (int i = 0; i < nCount; i++)
            {
                int nIdx = m_colBuckets.Add(rgBottom[i]);

                for (int j = 0; j < m_rgOneHotVector.Length; j++)
                {
                    if (j == nIdx)
                    {
                        m_rgOneHotVector[j] = 1.0f;
                    }
                    else
                    {
                        m_rgOneHotVector[j] = 0;
                    }
                }

                Array.Copy(m_rgOneHotVector, 0, m_rgTop, i * m_rgOneHotVector.Length, m_rgOneHotVector.Length);
            }

            colTop[0].mutable_cpu_data = convert(m_rgTop);
        }
        private void testRandom(Log log, CryptoRandom.METHOD method, int nBuckets)
        {
            PreTest.Init();

            BucketCollection col  = new BucketCollection(0, 1, nBuckets);
            CryptoRandom     rand = new CryptoRandom(method, Guid.NewGuid().GetHashCode());
            int nTotal            = 1000000;

            log.WriteLine("Testing (" + nBuckets.ToString() + ") " + method.ToString());

            for (int i = 0; i < nTotal; i++)
            {
                double df = rand.NextDouble();
                col.Add(df);
            }

            string        str  = "";
            List <double> rgdf = new List <double>();

            for (int i = 0; i < nBuckets; i++)
            {
                double dfPct = col[i].Count / (double)nTotal;
                str += dfPct.ToString("P");
                str += ", ";
                rgdf.Add(dfPct);
            }

            str = str.TrimEnd(',', ' ');

            log.WriteLine(method.ToString() + " =>> " + str);

            double dfStdev = stdDev(rgdf, false);

            log.WriteLine(method.ToString() + " stdev = " + dfStdev.ToString());
        }
        private void testRandomIdx(Log log, CryptoRandom.METHOD method, int nBuckets)
        {
            PreTest.Init();

            BucketCollection col  = new BucketCollection(0.0, 1.0, nBuckets);
            CryptoRandom     rand = new CryptoRandom(method, Guid.NewGuid().GetHashCode());
            int nTotal            = 100000;

            log.WriteLine("Testing (" + nBuckets.ToString() + ") " + method.ToString());

            List <int>         rgIdx1           = new List <int>();
            List <List <int> > rgrgPermutations = new List <List <int> >();

            for (int i = 0; i < nTotal / nBuckets; i++)
            {
                List <int> rgPermutation = new List <int>();

                for (int j = 0; j < nBuckets; j++)
                {
                    int    nIdx  = rand.Next(nBuckets);
                    double dfPct = (double)nIdx / (double)nBuckets;

                    rgPermutation.Add(nIdx);

                    col.Add(dfPct);
                }

                rgrgPermutations.Add(rgPermutation);
            }

            string        str  = "";
            List <double> rgdf = new List <double>();

            for (int i = 0; i < nBuckets; i++)
            {
                double dfPct = col[i].Count / (double)nTotal;
                str += dfPct.ToString("P");
                str += ", ";
                rgdf.Add(dfPct);
            }

            str = str.TrimEnd(',', ' ');

            log.WriteLine(method.ToString() + " =>> " + str);

            double dfStdev = stdDev(rgdf, false);

            log.WriteLine(method.ToString() + " stdev = " + dfStdev.ToString());


            // Verify permuation uniqueness
            int       nDuplicateCount   = 0;
            int       nPermutationCount = rgrgPermutations.Count;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            int nProgressIdx = 0;

            while (rgrgPermutations.Count > 1)
            {
                List <int> rgPermutation1 = rgrgPermutations[0];
                rgrgPermutations.RemoveAt(0);

                List <int> rgRemove = new List <int>();

                for (int j = 0; j < rgrgPermutations.Count; j++)
                {
                    if (compareLists(rgPermutation1, rgrgPermutations[j]))
                    {
                        nDuplicateCount++;
                        rgRemove.Add(j);
                    }
                }

                for (int j = rgRemove.Count - 1; j >= 0; j--)
                {
                    rgrgPermutations.RemoveAt(rgRemove[j]);
                }

                if (sw.Elapsed.TotalMilliseconds > 2000)
                {
                    log.Progress = (double)nProgressIdx / (double)nPermutationCount;
                    log.WriteLine("Permutation checking at " + log.Progress.ToString("P") + "...");
                    sw.Restart();
                }

                nProgressIdx++;
            }

            log.WriteLine("Out of " + nPermutationCount.ToString("N0") + " permutations, " + nDuplicateCount.ToString("N0") + " duplicates were found (" + ((double)nDuplicateCount / nPermutationCount).ToString("P") + ").");
        }