Example #1
0
        private static void WriteTrainingAndTestResults(
            IDictionary <string, IDictionary <IBitVector, IReadOnlyCollection <IBitVector> > > topPunchedCardsPerLabel,
            List <Tuple <IBitVector, IBitVector> > trainingData,
            List <Tuple <IBitVector, IBitVector> > testData,
            IPuncher <string, IBitVector, IBitVector> puncher)
        {
            Console.WriteLine("Unique input combinations per punched card (descending): " +
                              GetPunchedCardsPerLabelString(topPunchedCardsPerLabel));

            var trainingCorrectRecognitionsPerLabel =
                RecognitionHelper.CountCorrectRecognitions(trainingData, topPunchedCardsPerLabel, puncher);

            Console.WriteLine("Training results: " +
                              trainingCorrectRecognitionsPerLabel
                              .Sum(correctRecognitionsPerLabel => correctRecognitionsPerLabel.Value) +
                              " correct recognitions of " + trainingData.Count);

            var testCorrectRecognitionsPerLabel =
                RecognitionHelper.CountCorrectRecognitions(testData, topPunchedCardsPerLabel, puncher);

            Console.WriteLine("Test results: " +
                              testCorrectRecognitionsPerLabel
                              .Sum(correctRecognitionsPerLabel => correctRecognitionsPerLabel.Value) +
                              " correct recognitions of " + testData.Count);
        }
Example #2
0
        GetTopPunchedCardsPerLabel(
            IDictionary <string, IDictionary <IBitVector, IReadOnlyCollection <IBitVector> > > punchedCardsPerKeyPerLabel,
            int topPunchedCardsPerKeyPerLabelCount)
        {
            var topPunchedCardsPerKeyPerLabel =
                new Dictionary <string, IDictionary <IBitVector, IReadOnlyCollection <IBitVector> > >();

            for (byte i = 0; i < DataHelper.LabelsCount; i++)
            {
                var label = DataHelper.GetLabelBitVector(i, BitVectorFactory);

                var topPunchedCardsPerSpecificLabel = punchedCardsPerKeyPerLabel
                                                      .OrderByDescending(punchedCardPerLabel =>
                                                                         RecognitionHelper.CalculateBitVectorsScore(punchedCardPerLabel.Value[label]))
                                                      .Take(topPunchedCardsPerKeyPerLabelCount);

                foreach (var topPunchedCardPerSpecificLabel in topPunchedCardsPerSpecificLabel)
                {
                    if (!topPunchedCardsPerKeyPerLabel.TryGetValue(topPunchedCardPerSpecificLabel.Key, out var dictionary))
                    {
                        dictionary = new Dictionary <IBitVector, IReadOnlyCollection <IBitVector> >();
                        topPunchedCardsPerKeyPerLabel.Add(topPunchedCardPerSpecificLabel.Key, dictionary);
                    }

                    dictionary.Add(label, topPunchedCardPerSpecificLabel.Value[label]);
                }
            }

            return(topPunchedCardsPerKeyPerLabel);
        }
Example #3
0
        private static void Main()
        {
            var trainingData = DataHelper.ReadTrainingData(BitVectorFactory).ToList();
            var testData     = DataHelper.ReadTestData(BitVectorFactory).ToList();

            var punchedCardBitLengths = new uint[] { 8, 16, 32, 64, 128, 256 };

            foreach (var punchedCardBitLength in punchedCardBitLengths)
            {
                Console.WriteLine("Punched card bit length: " + punchedCardBitLength);

                IPuncher <string, IBitVector, IBitVector> puncher = new RandomPuncher(punchedCardBitLength, BitVectorFactory);
                var punchedCardsPerKeyPerLabel = GetPunchedCardsPerKeyPerLabel(trainingData, puncher);
                var experts = RecognitionHelper.CreateExperts(punchedCardsPerKeyPerLabel);

                Console.WriteLine();
                Console.WriteLine("Global top punched card:");
                WriteTrainingAndTestResults(GetGlobalTopPunchedCard(punchedCardsPerKeyPerLabel, experts), trainingData, testData, puncher, experts);

                Console.WriteLine();
                Console.WriteLine("Top punched cards per label:");
                WriteTrainingAndTestResults(GetTopPunchedCardsPerLabel(punchedCardsPerKeyPerLabel, experts, 64), trainingData, testData, puncher, experts);

                Console.WriteLine();
            }

            Console.WriteLine("Press \"Enter\" to exit the program...");
            Console.ReadLine();
        }
Example #4
0
        private static IDictionary <string, IDictionary <IBitVector, IReadOnlyCollection <IBitVector> > > GetGlobalTopPunchedCard(
            IDictionary <string, IDictionary <IBitVector, IReadOnlyCollection <IBitVector> > > punchedCardsPerKeyPerLabel)
        {
            var globalTopPunchedCard = punchedCardsPerKeyPerLabel
                                       .OrderByDescending(punchedCardPerKeyPerLabel =>
                                                          punchedCardPerKeyPerLabel.Value
                                                          .AsParallel()
                                                          .Sum(labelAndInputs => RecognitionHelper.CalculateBitVectorsScore(labelAndInputs.Value)))
                                       .First();

            return(new Dictionary <string, IDictionary <IBitVector, IReadOnlyCollection <IBitVector> > >
            {
                { globalTopPunchedCard.Key, globalTopPunchedCard.Value }
            });
        }
Example #5
0
        private static IReadOnlyDictionary <string, IReadOnlyDictionary <IBitVector, IReadOnlyCollection <IBitVector> > > GetGlobalTopPunchedCard(
            IReadOnlyDictionary <string, IReadOnlyDictionary <IBitVector, IReadOnlyCollection <IBitVector> > > punchedCardsPerKeyPerLabel,
            IReadOnlyDictionary <string, IExpert> experts)
        {
            var globalTopPunchedCard = punchedCardsPerKeyPerLabel
                                       .AsParallel()
                                       .Select(punchedCardsPerKeyPerLabel =>
                                               Tuple.Create(punchedCardsPerKeyPerLabel, RecognitionHelper.CalculateMaxLossSum(punchedCardsPerKeyPerLabel, experts)))
                                       .MaxBy(tuple => tuple.Item2).Item1;

            return(new Dictionary <string, IReadOnlyDictionary <IBitVector, IReadOnlyCollection <IBitVector> > >
            {
                { globalTopPunchedCard.Key, globalTopPunchedCard.Value }
            });
        }
Example #6
0
        GetTopPunchedCardsPerLabel(
            IReadOnlyDictionary <string, IReadOnlyDictionary <IBitVector, IReadOnlyCollection <IBitVector> > > punchedCardsPerKeyPerLabel,
            IReadOnlyDictionary <string, IExpert> experts,
            int topPunchedCardsPerKeyPerLabelCount)
        {
            var topPunchedCardsPerKeyPerLabel =
                new Dictionary <string, IReadOnlyDictionary <IBitVector, IReadOnlyCollection <IBitVector> > >();

            foreach (var label in DataHelper.GetLabels(BitVectorFactory))
            {
                var topPunchedCardsPerSpecificLabel = punchedCardsPerKeyPerLabel
                                                      .AsParallel()
                                                      .Select(punchedCardsPerKeyPerLabel =>
                                                              Tuple.Create(punchedCardsPerKeyPerLabel, RecognitionHelper.CalculateMaxLoss(punchedCardsPerKeyPerLabel, experts, label)))
                                                      .OrderByDescending(tuple => tuple.Item2)
                                                      .Take(topPunchedCardsPerKeyPerLabelCount)
                                                      .Select(tuple => tuple.Item1);

                foreach (var topPunchedCardPerSpecificLabel in topPunchedCardsPerSpecificLabel)
                {
                    if (!topPunchedCardsPerKeyPerLabel.TryGetValue(topPunchedCardPerSpecificLabel.Key, out var dictionary))
                    {
                        dictionary = new Dictionary <IBitVector, IReadOnlyCollection <IBitVector> >();
                        topPunchedCardsPerKeyPerLabel.Add(topPunchedCardPerSpecificLabel.Key, dictionary);
                    }

                    ((Dictionary <IBitVector, IReadOnlyCollection <IBitVector> >)dictionary).Add(label, topPunchedCardPerSpecificLabel.Value[label]);
                }
            }

            return(topPunchedCardsPerKeyPerLabel);
        }