Exemple #1
0
        public void OutputShouldNotBeZero()
        {
            var values = new [] {
                0.5f, 0.1f, 0.0f, 1.0f, 0.7f, 0.66f, 0.99f
            };
            var map = BitMapper.Map(values, 0f, 1f, 50);

            var atleastOneIsTrue = false;

            for (int i = 0, l = values.Length * 50; i < l; i++)
            {
                var b = map(i);
                Console.Write(b ? "1" : "0");
                if (i % 50 == 49)
                {
                    Console.WriteLine();
                }
                if (b)
                {
                    atleastOneIsTrue = true;
                }
            }

            Assert.IsTrue(atleastOneIsTrue, "At least one output bit should be true.");
        }
Exemple #2
0
        public void TestData()
        {
            var trainingData = ReadTestData("../../../../Data/train_sensor.txt", "../../../../Data/train_category.txt");

            var floatPrecision = 50;

            var cl = Create();

            foreach (var frame in trainingData)
            {
                var bits = AsBitArray(BitMapper.Map(frame.Columns, -0.6f, 2.21f, floatPrecision), frame.Columns.Length * floatPrecision);
                cl.Train(frame.Category.ToString(), bits);
            }


            var testData = ReadTestData("../../../../Data/test_sensor.txt", "../../../../Data/test_category.txt");

            var acurate = 0;
            var total   = 0;

            var accuracy = new Dictionary <string, Accuracy>();

            foreach (var frame in testData)
            {
                var bits = AsBitArray(BitMapper.Map(frame.Columns, -0.6f, 2.21f, floatPrecision), frame.Columns.Length * floatPrecision);

                var matches = cl.FindMatches(bits)
                              .OrderByDescending(c => c.Strength)
                              .Take(2)
                              .ToArray();

                var cat = frame.Category.ToString();
                if (matches[0].Identifier == cat)
                {
                    acurate++;
                }
                total++;

                Accuracy v;
                if (!accuracy.TryGetValue(cat, out v))
                {
                    accuracy.Add(cat, v = new Accuracy());
                }

                if (matches[0].Identifier == frame.Category.ToString())
                {
                    v.acurate++;
                }
                v.total++;

                Console.WriteLine("Expected: {0}  Matched: {1} ({3:p})  Runner Up: {2} ({4:p})", frame.Category, matches[0].Identifier, matches[1].Identifier, matches[0].Strength, matches[1].Strength);
            }
            Console.WriteLine("Accuracy: {0:p}", ((float)acurate) / ((float)total));

            foreach (var pair in accuracy)
            {
                Console.WriteLine("Accuracy {1}: {0:p}", ((float)pair.Value.acurate) / ((float)pair.Value.total), pair.Key);
            }
        }
Exemple #3
0
        public void TestData()
        {
            var trainingData = ReadTestData("../../../../Data/train_sensor.txt", "../../../../Data/train_category.txt");

            var columnCount    = 300;
            var cellsPerColumn = 1;

            var floatPrecision = 50;

            var node = new Node(
                inputCount: 32 * floatPrecision,
                columnCount: columnCount,
                cellsPerColumn: cellsPerColumn,
                desiredLocalActivity: 10,
                minOverlap: 2,
                maxSynapsesPerColumn: 20,
                newSynapseCount: 2
                );

            var cl = new AlsingClassifier();

            foreach (var frame in trainingData)
            {
                node.Feed(BitMapper.Map(frame.Columns, -0.6f, 2.21f, floatPrecision));
            }

            using (var log = new StreamWriter("../../../../Data/train_result.txt", false, Encoding.ASCII))
                foreach (var frame in trainingData)
                {
                    node.Feed(BitMapper.Map(frame.Columns, -0.6f, 2.21f, floatPrecision));
                    cl.Train(frame.Category.ToString(), node.ToArray());
                    log.WriteLine(node.Select(v => v ? '1' : '0').ToArray());
                }

            node.Learning = false;

            var testData = ReadTestData("../../../../Data/test_sensor.txt", "../../../../Data/test_category.txt");

            var acurate = 0;
            var total   = 0;

            var accuracy = new Dictionary <string, Accuracy>();

            using (var log = new StreamWriter("../../../../Data/test_result.txt", false, Encoding.ASCII))
                foreach (var frame in testData)
                {
                    var activeCount = 0;

                    Func <int, bool> input = BitMapper.Map(frame.Columns, -0.6f, 2.21f, floatPrecision);
                    node.Feed(input);

                    foreach (var b in node)
                    {
                        if (b)
                        {
                            activeCount++;
                        }
                    }

                    var matches = cl.FindMatches(node.ToArray())
                                  .OrderByDescending(c => c.Strength)
                                  .Take(2)
                                  .ToArray();

                    log.WriteLine(node.Select(d => d ? '1' : '0').ToArray());

                    /*for (var i = 0; i < 32 * 50; i++)
                     *      Console.Write(input(i) ? "1" : "0");
                     * Console.WriteLine();
                     *
                     * Console.WriteLine(String.Join("", outputData.Select(v => v ? "1" : "0")));*/

                    var cat = frame.Category.ToString();
                    if (matches[0].Identifier == cat)
                    {
                        acurate++;
                    }
                    total++;

                    Accuracy v;
                    if (!accuracy.TryGetValue(cat, out v))
                    {
                        accuracy.Add(cat, v = new Accuracy());
                    }

                    if (matches[0].Identifier == frame.Category.ToString())
                    {
                        v.acurate++;
                    }
                    v.total++;

                    Console.WriteLine("Expected: {0}  Matched: {1} ({3:p})  Runner Up: {2} ({4:p})  Activity Count: {5}", frame.Category, matches[0].Identifier, matches[1].Identifier, matches[0].Strength, matches[1].Strength, activeCount);
                }
            Console.WriteLine("Accuracy: {0:p}", ((float)acurate) / ((float)total));

            foreach (var pair in accuracy)
            {
                Console.WriteLine("Accuracy {1}: {0:p}", ((float)pair.Value.acurate) / ((float)pair.Value.total), pair.Key);
            }
        }
Exemple #4
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: htm TRAINING-SENSOR-FILE TRAINING-CATEGORY-FILE TEST-SENSOR-FILE TEST-CATEGORY-FILE [spatialonly] [PARAMETER VALUE [PARAMETER VALUE ...]]");
                Console.WriteLine("Parameters (Default Value):");
                Console.WriteLine("nodesPerLevel (1)");
                Console.WriteLine("columnCount (100)");
                Console.WriteLine("cellsPerColumn (4)");
                Console.WriteLine("desiredLocalActivity (10)");
                Console.WriteLine("minOverlap (2)");
                Console.WriteLine("maxSynapsesPerColumn (2)");
                Console.WriteLine("connectedPerm (0.2)");
                Console.WriteLine("permanenceInc (0.01)");
                Console.WriteLine("permanenceDec (0.01)");
                Console.WriteLine("slidingWindowLength (1000)");
                Console.WriteLine("activationThreshold (1)");
                Console.WriteLine("minThreshold (1)");
                Console.WriteLine("learningRadius (10)");
                Console.WriteLine("newSynapseCount (10)");
                Console.WriteLine("floatPrecision (50)");
                Console.WriteLine("warmUps (1)");
                Console.WriteLine();
                Console.WriteLine("trainingLog (NONE) - File name to log training output");
                Console.WriteLine("testLog (NONE) - File name to log test output");
                return;
            }

            string trainingLog = null;
            string testLog     = null;

            int floatPrecision = 50;

            int   columnCount = 100, cellsPerColumn = 4, desiredLocalActivity = 10, minOverlap = 2, maxSynapsesPerColumn = 2;
            float connectedPerm = 0.2f, permanenceInc = 0.01f, permanenceDec = 0.01f;
            int   slidingWindowLength = 1000, activationThreshold = 1;
            int   minThreshold = 1;
            int   learningRadius = 10, newSynapseCount = 10;
            int   warmUps = 1;

            bool spatialOnly = false;

            int[] nodesPerLevel = new[] { 1 };

            var ci = System.Globalization.CultureInfo.InvariantCulture;

            for (var i = 4; i < args.Length; i++)
            {
                var cmd = args[i].ToLowerInvariant();
                switch (cmd)
                {
                case "nodesperlevel":
                    var levels = new List <int>();
                    int v;
                    while (++i < args.Length)
                    {
                        if (!int.TryParse(args[i], out v))
                        {
                            i--;
                            break;
                        }
                        levels.Add(v);
                    }
                    nodesPerLevel = levels.ToArray();
                    break;

                case "warmups":
                    warmUps = int.Parse(args[++i], ci);
                    break;

                case "columncount":
                    columnCount = int.Parse(args[++i], ci);
                    break;

                case "cellspercolumn":
                    cellsPerColumn = int.Parse(args[++i], ci);
                    break;

                case "desiredlocalactivity":
                    desiredLocalActivity = int.Parse(args[++i], ci);
                    break;

                case "minoverlap":
                    minOverlap = int.Parse(args[++i], ci);
                    break;

                case "maxsynapsespercolumn":
                    maxSynapsesPerColumn = int.Parse(args[++i], ci);
                    break;

                case "connectedperm":
                    connectedPerm = float.Parse(args[++i], ci);
                    break;

                case "permanenceinc":
                    permanenceInc = float.Parse(args[++i], ci);
                    break;

                case "permanencedec":
                    permanenceDec = float.Parse(args[++i], ci);
                    break;

                case "slidingwindowlength":
                    slidingWindowLength = int.Parse(args[++i], ci);
                    break;

                case "activationthreshold":
                    activationThreshold = int.Parse(args[++i], ci);
                    break;

                case "minthreshold":
                    minThreshold = int.Parse(args[++i], ci);
                    break;

                case "learningradius":
                    learningRadius = int.Parse(args[++i], ci);
                    break;

                case "newsynapsecount":
                    newSynapseCount = int.Parse(args[++i], ci);
                    break;

                case "floatprecision":
                    floatPrecision = int.Parse(args[++i], ci);
                    break;

                case "traininglog":
                    trainingLog = args[++i];
                    break;

                case "testlog":
                    testLog = args[++i];
                    break;

                case "spatialonly":
                    spatialOnly = true;
                    break;

                default:
                    Console.WriteLine("Unknown argument: " + args[i]);
                    return;
                }
            }

            var trainingData = ReadTestData(args[0], args[1]);
            var testData     = ReadTestData(args[2], args[3]);

            var allValues = trainingData.SelectMany(r => r.Columns)
                            .Union(trainingData.SelectMany(r => r.Columns));

            float min = allValues.Min(), max = allValues.Max();

            object node;

            if (spatialOnly)
            {
                node = new SpatialPooler(
                    trainingData.First().Columns.Length *floatPrecision,
                    columnCount, desiredLocalActivity, minOverlap, maxSynapsesPerColumn,
                    connectedPerm, permanenceInc, permanenceDec,
                    slidingWindowLength
                    );
            }
            else
            {
                node = new Tree(
                    nodesPerLevel,
                    trainingData.First().Columns.Length *floatPrecision,
                    columnCount, cellsPerColumn, desiredLocalActivity, minOverlap, maxSynapsesPerColumn,
                    connectedPerm, permanenceInc, permanenceDec,
                    slidingWindowLength, activationThreshold,
                    minThreshold,
                    learningRadius, newSynapseCount
                    );
            }

            TestData(node, trainingData, testData, warmUps, trainingLog, testLog, c => BitMapper.Map(c, min, max, floatPrecision));
        }
Exemple #5
0
        public void TestData()
        {
            var trainingData = ReadTestData("../../../../Data/train_sensor.txt", "../../../../Data/train_category.txt");

            var columnCount = 300;

            var sp = new SpatialPooler(
                inputCount: 32 * 50,
                columnCount: columnCount,
                desiredLocalActivity: 10,
                minOverlap: 2,
                maxSynapsesPerColumn: 20
                );

            var cl = new AlsingClassifier();

            foreach (var frame in trainingData)
            {
                sp.Feed(BitMapper.Map(frame.Columns, -0.6f, 2.1f, 50));
            }

            using (var log = new StreamWriter("../../../../Data/train_result_spatial.txt", false, Encoding.ASCII))
                foreach (var frame in trainingData)
                {
                    sp.Feed(BitMapper.Map(frame.Columns, -0.6f, 2.1f, 50));
                    cl.Train(frame.Category.ToString(), sp.ToArray());
                    log.WriteLine(String.Join(" ", sp.Select(v => v ? '1' : '0')));
                }

            sp.Learning = false;

            var testData = ReadTestData("../../../../Data/test_sensor.txt", "../../../../Data/test_category.txt");

            using (var log = new StreamWriter("../../../../Data/test_result_spatial.txt", false, Encoding.ASCII))
                foreach (var frame in testData)
                {
                    var activeCount = 0;

                    Func <int, bool> input = BitMapper.Map(frame.Columns, -0.6f, 2.1f, 50);

                    sp.Feed(input);

                    foreach (var b in sp)
                    {
                        if (b)
                        {
                            activeCount++;
                        }
                    }

                    log.WriteLine(String.Join(" ", sp.Select(v => v ? '1' : '0')));

                    var matches = cl.FindMatches(sp.ToArray())
                                  .OrderByDescending(c => c.Strength)
                                  .Take(2)
                                  .ToArray();

                    /*for (var i = 0; i < 32 * 50; i++)
                     *      Console.Write(input(i) ? "1" : "0");
                     * Console.WriteLine();
                     *
                     * Console.WriteLine(String.Join("", outputData.Select(v => v ? "1" : "0")));*/

                    Console.WriteLine("Expected: {0}  Matched: {1} ({3:p})  Runner Up: {2} ({4:p})  Activity Count: {5}", frame.Category, matches[0].Identifier, matches[1].Identifier, matches[0].Strength, matches[1].Strength, activeCount);
                }
        }