コード例 #1
0
        public void TestEncodeAdjacentPositions()
        {
            int    repetitions  = 100;
            int    n            = 999;
            int    w            = 25;
            int    radius       = 10;
            double minThreshold = 0.75;
            double avgThreshold = 0.90;

            double[] allOverlaps = new double[repetitions];

            for (int i = 0; i < repetitions; i++)
            {
                double[] overlaps = OverlapsForRelativeAreas(
                    n, w, new[] { i * 10, i * 10 }, radius, new[] { 0, 1 }, 0, 1, false);

                allOverlaps[i] = overlaps[0];
            }

            Assert.IsTrue(ArrayUtils.Min(allOverlaps) > minThreshold);
            Assert.IsTrue(ArrayUtils.Average(allOverlaps) > avgThreshold);

            if (verbose)
            {
                Console.WriteLine(String.Format("===== Adjacent positions overlap " +
                                                "(n = {0}, w = {1}, radius = {2} ===", n, w, radius));
                Console.WriteLine(String.Format("Max: {0}", ArrayUtils.Max(allOverlaps)));
                Console.WriteLine(String.Format("Min: {0}", ArrayUtils.Min(allOverlaps)));
                Console.WriteLine(String.Format("Average: {0}", ArrayUtils.Average(allOverlaps)));
            }
        }
コード例 #2
0
ファイル: HtmCompute.cs プロジェクト: chetan2510/neocortexapi
        /// <summary>
        /// It traverses all connected synapses of the column and calculates the span, which synapses
        /// spans between all input bits. Then it calculates average of spans accross all dimensions.
        /// </summary>
        /// <param name="column"></param>
        /// <param name="htmConfig">Topology</param>
        /// <returns></returns>
        public static double CalcAvgSpanOfConnectedSynapses(Column column, HtmConfig htmConfig)
        {
            // Gets synapses connected to input bits.(from pool of the column)
            int[] connected = column.ProximalDendrite.GetConnectedSynapsesSparse();

            if (connected == null || connected.Length == 0)
            {
                return(0);
            }

            int[] maxCoord = new int[htmConfig.InputModuleTopology.Dimensions.Length];
            int[] minCoord = new int[maxCoord.Length];
            ArrayUtils.FillArray(maxCoord, -1);
            ArrayUtils.FillArray(minCoord, ArrayUtils.Max(htmConfig.InputModuleTopology.Dimensions));

            //
            // It takes all connected synapses
            for (int i = 0; i < connected.Length; i++)
            {
                maxCoord = ArrayUtils.MaxBetween(maxCoord, AbstractFlatMatrix.ComputeCoordinates(htmConfig.InputModuleTopology.Dimensions.Length,
                                                                                                 htmConfig.InputModuleTopology.DimensionMultiplies, htmConfig.InputModuleTopology.IsMajorOrdering, connected[i]));

                minCoord = ArrayUtils.MinBetween(minCoord, AbstractFlatMatrix.ComputeCoordinates(htmConfig.InputModuleTopology.Dimensions.Length,
                                                                                                 htmConfig.InputModuleTopology.DimensionMultiplies, htmConfig.InputModuleTopology.IsMajorOrdering, connected[i]));
            }

            return(ArrayUtils.Average(ArrayUtils.Add(ArrayUtils.Subtract(maxCoord, minCoord), 1)));
        }
コード例 #3
0
        public void TestEncodeUnrelatedAreas()
        {
            double avgThreshold = 0.3;
            double maxThreshold = 0.14;

            CoordinateEncoder.ResetRandomGenerator();

            double[] overlaps    = OverlapsForUnrelatedAreas(1499, 37, 5, 100, false);
            double   maxOverlaps = ArrayUtils.Max(overlaps);

            Debug.WriteLine("Max overlaps = " + maxOverlaps);
            Assert.IsTrue(maxOverlaps < maxThreshold);
            Assert.IsTrue(ArrayUtils.Average(overlaps) < avgThreshold);

            maxThreshold = 0.12;
            overlaps     = OverlapsForUnrelatedAreas(1499, 37, 10, 100, false);
            maxOverlaps  = ArrayUtils.Max(overlaps);
            Debug.WriteLine("Max overlaps = " + maxOverlaps);
            Assert.IsTrue(maxOverlaps < maxThreshold);
            Assert.IsTrue(ArrayUtils.Average(overlaps) < avgThreshold);

            maxThreshold = 0.13;
            overlaps     = OverlapsForUnrelatedAreas(999, 25, 10, 100, false);
            maxOverlaps  = ArrayUtils.Max(overlaps);
            Debug.WriteLine("Max overlaps = " + maxOverlaps);
            Assert.IsTrue(maxOverlaps < maxThreshold);
            Assert.IsTrue(ArrayUtils.Average(overlaps) < avgThreshold);

            maxThreshold = 0.16;
            overlaps     = OverlapsForUnrelatedAreas(499, 13, 10, 100, false);
            maxOverlaps  = ArrayUtils.Max(overlaps);
            Debug.WriteLine("Max overlaps = " + maxOverlaps);
            Assert.IsTrue(maxOverlaps < maxThreshold);
            Assert.IsTrue(ArrayUtils.Average(overlaps) < avgThreshold);
        }
コード例 #4
0
        /**
         * Populates the inner fields of this {@code Metric} with the computed stats.
         * @param l
         */
        public void computeStats(List <TNumber> l)
        {
            if (l.Count < 1)
            {
                return;
            }

            double[] doubs = null;
            if (typeof(int).IsAssignableFrom(l[0].GetType()))
            {
                doubs = ArrayUtils.ToDoubleArray(l.Cast <int>().ToArray());
            }
            else if (typeof(double).IsAssignableFrom(l[0].GetType()))
            {
                doubs = l.Cast <double>().ToArray();
            }

            min = ArrayUtils.Min(doubs);
            max = ArrayUtils.Max(doubs);
            sum = ArrayUtils.Sum(doubs);

            double d = ArrayUtils.Average(doubs);

            mean = d;
            double v = ArrayUtils.Variance(doubs, d);

            variance = v;
            double s = v > 0 ? Math.Sqrt(v) : 0.0;

            standardDeviation = s;
        }
コード例 #5
0
        /// <summary>
        /// Initialize all columns inside of partition and connect them to sensory input.
        /// It returns the average connected span of the partition.
        /// </summary>
        /// <param name="msg"></param>
        private void CreateAndConnectColumns(ConnectAndConfigureColumnsMsg msg)
        {
            DateTime startTime = DateTime.Now;

            Log(msg, Self, "Entered.");

            List <double> avgConnections = new List <double>();

            Random rnd;

            if (this.m_Config.RandomGenSeed > 0)
            {
                rnd = new Random(this.m_Config.RandomGenSeed);
            }
            else
            {
                rnd = new Random();
            }

            if (this.m_Dict.Count == 0)
            {
            }

            foreach (var element in this.m_Dict)
            {
                if (this.m_Config == null)
                {
                    throw new ArgumentException($"HtmConfig must be set in the message.");
                }

                int colIndx = (int)element.Key;

                // Gets RF
                var potential = HtmCompute.MapPotential(this.m_Config, colIndx, rnd);
                var column    = (Column)this.m_Dict[colIndx];

                // This line initializes all synases in the potential pool of synapses.
                // It creates the pool on proximal dendrite segment of the column.
                // After initialization permancences are set to zero.
                //connectColumnToInputRF(c.HtmConfig, data.Potential, data.Column);
                column.CreatePotentialPool(this.m_Config, potential, -1);

                var perms = HtmCompute.InitSynapsePermanences(this.m_Config, potential, rnd);

                avgConnections.Add(HtmCompute.CalcAvgSpanOfConnectedSynapses(column, this.m_Config));

                //Log(msg, Self, $".:). {dict.Count}/{dict.Keys.Min()}/{dict.Keys.Min()} - duration: {(DateTime.Now - startTime).TotalSeconds}");

                HtmCompute.UpdatePermanencesForColumn(this.m_Config, perms, column, potential, true);
            }

            double avgConnectedSpan = ArrayUtils.Average(avgConnections.ToArray());

            Log(msg, Self, "Completed.");
            Sender.Tell(avgConnectedSpan, Self);
            Log(msg, Self, $"Response sent. {(DateTime.Now - startTime).TotalSeconds}");
        }
コード例 #6
0
        static void Main(string[] args)
        {
            var scores  = new int[] { 55, 60, 45, 70, 85, 93, 68 };
            var total   = ArrayUtils.Total(scores);
            var average = ArrayUtils.Average(scores);
            var max     = ArrayUtils.Max(scores);
            var min     = ArrayUtils.Min(scores);

            Console.WriteLine($"合計: {total}, 平均: {average}, 最大: {max}, 最小: {min}");
        }
コード例 #7
0
ファイル: AnomalyLikelihood.cs プロジェクト: ArtiDi/HTM.Net
        /**
         * A Map containing the parameters of a normal distribution based on
         * the sampleData.
         *
         * @param sampleData
         * @param performLowerBoundCheck
         * @return
         */
        public Statistic EstimateNormal(double[] sampleData, bool performLowerBoundCheck)
        {
            double d = ArrayUtils.Average(sampleData);
            double v = ArrayUtils.Variance(sampleData, d);

            if (performLowerBoundCheck)
            {
                if (d < 0.03)
                {
                    d = 0.03;
                }
                if (v < 0.0003)
                {
                    v = 0.0003;
                }
            }

            // Compute standard deviation
            double s = v > 0 ? Math.Sqrt(v) : 0.0;

            return(new Statistic(d, v, s));
        }
コード例 #8
0
ファイル: HtmActor.cs プロジェクト: rabiul64/neocortexapi
        /// <summary>
        /// Initialize all columns inside of partition and connect them to sensory input.
        /// It returns the average connected span of the partition.
        /// </summary>
        /// <param name="msg"></param>
        //TODO remove unnecessary argument
        private double CreateAndConnectColumns(ConnectAndConfigureColumnsMsg msg)
        {
            Debug.Write(".");
            List <double> avgConnections = new List <double>();

            Random rnd;

            if (this.HtmConfig.RandomGenSeed > 0)
            {
                rnd = new Random(this.HtmConfig.RandomGenSeed);
            }
            else
            {
                rnd = new Random();
            }

            foreach (var element in this.Dict)
            {
                if (this.HtmConfig == null)
                {
                    throw new ArgumentException($"HtmConfig must be set in the message.");
                }

                int colIndx = -1;

                Column column;

                if (element.Key is string)
                {
                    if (!int.TryParse(element.Key as string, out colIndx))
                    {
                        throw new ArgumentException($"The key must be of type 'int' or string convertable to 'int");
                    }

                    column = (Column)this.Dict[element.Key];
                }
                else
                {
                    colIndx = (int)element.Key;
                    column  = (Column)this.Dict[colIndx];
                }

                // Gets RF
                var potential = HtmCompute.MapPotential(this.HtmConfig, colIndx, rnd);


                // This line initializes all synases in the potential pool of synapses.
                // It creates the pool on proximal dendrite segment of the column.
                // After initialization permancences are set to zero.
                //connectColumnToInputRF(c.HtmConfig, data.Potential, data.Column);
                column.CreatePotentialPool(this.HtmConfig, potential, -1);

                var perms = HtmCompute.InitSynapsePermanences(this.HtmConfig, potential, rnd);

                avgConnections.Add(HtmCompute.CalcAvgSpanOfConnectedSynapses(column, this.HtmConfig));

                HtmCompute.UpdatePermanencesForColumn(this.HtmConfig, perms, column, potential, true);
            }

            Debug.Write(".");

            double avgConnectedSpan = ArrayUtils.Average(avgConnections.ToArray());

            Debug.Write(".");
            return(avgConnectedSpan);
        }