示例#1
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);
            }
        }
示例#2
0
        static void TestData(dynamic node, IEnumerable <TestFrame> trainingData, IEnumerable <TestFrame> testData, int warmUps, string trainingLog, string testLog, Func <float[], Func <int, bool> > floatMapper)
        {
            var enode = node as IEnumerable <bool>;

            var cl = new AlsingClassifier();

            var warmupStart = DateTime.Now;

            for (var w = 0; w < warmUps; w++)
            {
                foreach (var frame in trainingData)
                {
                    node.Feed(floatMapper(frame.Columns));
                }
            }

            var warmupEnd = DateTime.Now;

            var trainingStart = DateTime.Now;

            using (var log = trainingLog == null ? null : new StreamWriter(trainingLog, false, Encoding.ASCII))
                foreach (var frame in trainingData)
                {
                    node.Feed(floatMapper(frame.Columns));
                    cl.Train(frame.Category.ToString(), enode.ToArray());

                    if (log != null)
                    {
                        log.WriteLine(String.Join(" ", enode.Select(d => d ? '1' : '0')));
                    }
                }

            var trainingEnd = DateTime.Now;

            node.Learning = false;

            var acurate = 0;
            var total   = 0;

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

            var testStart = DateTime.Now;

            using (var log = testLog == null ? null : new StreamWriter(testLog, false, Encoding.ASCII))
                foreach (var frame in testData)
                {
                    var activeCount = 0;

                    Func <int, bool> input = floatMapper(frame.Columns);
                    node.Feed(input);

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

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

                    if (log != null)
                    {
                        log.WriteLine(String.Join(" ", enode.Select(d => d ? '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);
                }

            var testEnd = DateTime.Now;

            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);
            }

            Console.WriteLine();
            Console.WriteLine("Warm up:\t{0:g}", warmupEnd - warmupStart);
            Console.WriteLine("Training:\t{0:g}", trainingEnd - trainingStart);
            Console.WriteLine("Test:\t{0:g}", testEnd - testStart);
        }
示例#3
0
        static void TestData(dynamic node, IEnumerable<TestFrame> trainingData, IEnumerable<TestFrame> testData, int warmUps, string trainingLog, string testLog, Func<float[], Func<int, bool>> floatMapper)
        {
            var enode = node as IEnumerable<bool>;

            var cl = new AlsingClassifier();

            var warmupStart = DateTime.Now;

            for (var w = 0; w < warmUps; w++)
                foreach (var frame in trainingData)
                    node.Feed(floatMapper(frame.Columns));

            var warmupEnd = DateTime.Now;

            var trainingStart = DateTime.Now;

            using (var log = trainingLog == null ? null : new StreamWriter(trainingLog, false, Encoding.ASCII))
                foreach (var frame in trainingData)
                {
                    node.Feed(floatMapper(frame.Columns));
                    cl.Train(frame.Category.ToString(), enode.ToArray());

                    if (log != null) log.WriteLine(String.Join(" ", enode.Select(d => d ? '1' : '0')));
                }

            var trainingEnd = DateTime.Now;

            node.Learning = false;

            var acurate = 0;
            var total = 0;

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

            var testStart = DateTime.Now;

            using (var log = testLog == null ? null : new StreamWriter(testLog, false, Encoding.ASCII))
                foreach (var frame in testData)
                {
                    var activeCount = 0;

                    Func<int, bool> input = floatMapper(frame.Columns);
                    node.Feed(input);

                    foreach (var b in enode)
                        if (b)
                            activeCount++;

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

                    if (log != null) log.WriteLine(String.Join(" ", enode.Select(d => d ? '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);
                }

            var testEnd = DateTime.Now;

            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);

            Console.WriteLine();
            Console.WriteLine("Warm up:\t{0:g}", warmupEnd - warmupStart);
            Console.WriteLine("Training:\t{0:g}", trainingEnd - trainingStart);
            Console.WriteLine("Test:\t{0:g}", testEnd - testStart);
        }
示例#4
0
文件: Node.cs 项目: sebmarkbage/htm
        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);
        }
示例#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);
                }
        }
示例#6
0
        public void CountActiveColumnsPerStepWithMovingInput()
        {
            var sp = new SpatialPooler(
                inputCount: 100,
                columnCount: 100,
                desiredLocalActivity: 5,
                minOverlap: 2,
                maxSynapsesPerColumn: 20
                );

            var cl = new AlsingClassifier();

            var values = new bool[100];

            for (var i = 0; i < 100; i++)
            {
                values[i] = i < 5;
            }


            for (var t = 0; t < 10000; t++)
            {
                Func <int, bool> input = (i) => values[i];

                sp.Feed(input);

                values.Shift();
            }

            sp.Learning = false;

            for (var t = 0; t < 100; t++)
            {
                Func <int, bool> input = (i) => values[i];

                sp.Feed(input);

                cl.Train((t / 10).ToString(), sp.ToArray());
                values.Shift();
            }

            for (var i = 0; i < 100; i++)
            {
                values[i] = i < 6;
            }

            var rnd = new Random();

            for (var t = 0; t < 1000; t++)
            {
                var activeCount = 0;

                var noiseInput = (bool[])values.Clone();
                for (var i = 0; i < 100; i++)
                {
                    if (rnd.NextDouble() < 0.05)
                    {
                        noiseInput[i] = !noiseInput[i];
                    }
                }

                Func <int, bool> input = (i) => noiseInput[i];

                sp.Feed(input);

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

                var matches = cl.FindMatches(sp.ToArray())
                              .OrderByDescending(c => c.Strength)
                              .Select(m => m.Identifier + ": " + Math.Round(m.Strength * 100, 2) + "%");

                Console.Write(String.Join("", sp.Select(v => v ? "1" : "0")) + "\t");

                Console.WriteLine(String.Join("\t", matches));

                values.Shift();
            }
        }
        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);
            }
        }
        public void CountActiveColumnsPerStepWithMovingInput()
        {
            var sp = new SpatialPooler(
                inputCount: 100,
                columnCount: 100,
                desiredLocalActivity: 5,
                minOverlap: 2,
                maxSynapsesPerColumn: 20
            );

            var cl = new AlsingClassifier();

            var values = new bool[100];
            for (var i = 0; i < 100; i++)
                values[i] = i < 5;

            for (var t = 0; t < 10000; t++)
            {
                Func<int, bool> input = (i) => values[i];

                sp.Feed(input);

                values.Shift();
            }

            sp.Learning = false;

            for (var t = 0; t < 100; t++)
            {
                Func<int, bool> input = (i) => values[i];

                sp.Feed(input);

                cl.Train((t / 10).ToString(), sp.ToArray());
                values.Shift();
            }

            for (var i = 0; i < 100; i++)
                values[i] = i < 6;

            var rnd = new Random();

            for (var t = 0; t < 1000; t++)
            {
                var activeCount = 0;

                var noiseInput = (bool[])values.Clone();
                for (var i = 0; i < 100; i++)
                    if (rnd.NextDouble() < 0.05)
                        noiseInput[i] = !noiseInput[i];

                Func<int, bool> input = (i) => noiseInput[i];

                sp.Feed(input);

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

                var matches = cl.FindMatches(sp.ToArray())
                    .OrderByDescending(c => c.Strength)
                    .Select(m => m.Identifier + ": " + Math.Round(m.Strength * 100, 2) + "%");

                Console.Write(String.Join("", sp.Select(v => v ? "1" : "0")) + "\t");

                Console.WriteLine(String.Join("\t", matches));

                values.Shift();
            }
        }