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); }
internal static IEnumerable <KeyValuePair <IBitVector, int> > CountCorrectRecognitions( IEnumerable <Tuple <IBitVector, IBitVector> > data, IDictionary <string, IDictionary <IBitVector, IReadOnlyCollection <Tuple <IBitVector, int> > > > punchedCardsCollection, IPuncher <string, IBitVector, IBitVector> puncher) { var correctRecognitionsPerLabel = new ConcurrentDictionary <IBitVector, int>(); data .AsParallel() .ForAll(dataItem => { var matchingScoresPerLabelPerPunchedCard = CalculateMatchingScoresPerLabelPerPunchedCard(punchedCardsCollection, dataItem.Item1, puncher); var topLabel = matchingScoresPerLabelPerPunchedCard .OrderByDescending(p => p.Value.Sum(keyScore => keyScore.Value)) .First() .Key; if (topLabel.Equals(dataItem.Item2)) { correctRecognitionsPerLabel.AddOrUpdate( dataItem.Item2, key => 1, (key, value) => value + 1); } }); return(correctRecognitionsPerLabel); }
CalculateMatchingScoresPerLabelPerPunchedCard( IDictionary <string, IDictionary <IBitVector, IReadOnlyCollection <IBitVector> > > punchedCardsCollection, IBitVector input, IPuncher <string, IBitVector, IBitVector> puncher, IDictionary <int, IAdjacencyMatrix> adjacencyMatrices) { var matchingScoresPerLabelPerPunchedCard = new Dictionary <IBitVector, IDictionary <string, double> >(); foreach (var punchedCardsCollectionItem in punchedCardsCollection) { var punchedInput = puncher.Punch(punchedCardsCollectionItem.Key, input).Input; foreach (var label in punchedCardsCollectionItem.Value) { ProcessTheSpecificLabel( matchingScoresPerLabelPerPunchedCard, punchedCardsCollectionItem.Key, label.Key, label.Value.Count, adjacencyMatrices[GetAdjacencyMatrixKey(punchedCardsCollectionItem.Key, label.Key)], punchedInput); } } return(matchingScoresPerLabelPerPunchedCard); }
CalculateLossPerLabelPerPunchedCard( IReadOnlyDictionary <string, IReadOnlyDictionary <IBitVector, IReadOnlyCollection <IBitVector> > > punchedCardsCollection, IBitVector input, IPuncher <string, IBitVector, IBitVector> puncher, IReadOnlyDictionary <string, IExpert> experts) { var lossPerLabelPerPunchedCard = new Dictionary <IBitVector, IReadOnlyDictionary <string, double> >(); foreach (var punchedCardsCollectionItem in punchedCardsCollection) { var expert = experts[punchedCardsCollectionItem.Key]; var punchedInput = puncher.Punch(punchedCardsCollectionItem.Key, input).Input; var losses = expert.CalculateLosses(punchedInput); foreach (var label in punchedCardsCollectionItem.Value) { if (!lossPerLabelPerPunchedCard.TryGetValue(label.Key, out var dictionary)) { dictionary = new Dictionary <string, double>(); lossPerLabelPerPunchedCard.Add(label.Key, dictionary); } ((Dictionary <string, double>)dictionary).Add(punchedCardsCollectionItem.Key, losses[label.Key]); } } return(lossPerLabelPerPunchedCard); }
internal static IEnumerable <KeyValuePair <IBitVector, int> > CountCorrectRecognitions( IEnumerable <Tuple <IBitVector, IBitVector> > data, IReadOnlyDictionary <string, IReadOnlyDictionary <IBitVector, IReadOnlyCollection <IBitVector> > > punchedCardsCollection, IPuncher <string, IBitVector, IBitVector> puncher, IReadOnlyDictionary <string, IExpert> experts, IBitVectorFactory bitVectorFactory) { var counters = DataHelper.GetLabels(bitVectorFactory).ToDictionary(label => label, label => new int[1]); data .AsParallel() .ForAll(dataItem => { var lossPerLabelPerPunchedCard = CalculateLossPerLabelPerPunchedCard( punchedCardsCollection, dataItem.Item1, puncher, experts); var topLabel = lossPerLabelPerPunchedCard .MinBy(p => p.Value.Sum(keyScore => keyScore.Value)) .Key; if (topLabel.Equals(dataItem.Item2)) { Interlocked.Increment(ref counters[topLabel][0]); } }); return(counters.ToDictionary(p => p.Key, p => p.Value[0])); }
GetPunchedCardsPerKeyPerLabel( IReadOnlyList <Tuple <IBitVector, IBitVector> > trainingData, IPuncher <string, IBitVector, IBitVector> puncher) { var count = trainingData[0].Item1.Count; return(puncher .GetKeys(count) .AsParallel() .Select(key => Tuple.Create( key, GetPunchedCardsPerLabel(trainingData.Select(trainingDataItem => Tuple.Create(puncher.Punch(key, trainingDataItem.Item1), trainingDataItem.Item2))))) .ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2)); }
CalculateMatchingScoresPerLabelPerPunchedCard( IDictionary <string, IDictionary <IBitVector, IReadOnlyCollection <Tuple <IBitVector, int> > > > punchedCardsCollection, IBitVector input, IPuncher <string, IBitVector, IBitVector> puncher) { var matchingScoresPerLabelPerPunchedCard = new Dictionary <IBitVector, IDictionary <string, double> >(); foreach (var punchedCardsCollectionItem in punchedCardsCollection) { var punchedInput = puncher.Punch(punchedCardsCollectionItem.Key, input).Input; foreach (var label in punchedCardsCollectionItem.Value) { ProcessTheSpecificLabel(matchingScoresPerLabelPerPunchedCard, punchedCardsCollectionItem.Key, label, punchedInput); } } return(matchingScoresPerLabelPerPunchedCard); }
GetPunchedCardsPerKeyPerLabel( IList <Tuple <IBitVector, IBitVector> > trainingData, IPuncher <string, IBitVector, IBitVector> puncher) { var count = trainingData[0].Item1.Count; var punchedCardsPerKeyPerLabel = new Dictionary < string, IDictionary <IBitVector, IReadOnlyCollection <Tuple <IBitVector, int> > > >(); puncher .GetKeys(count) .AsParallel() .ForAll(key => { punchedCardsPerKeyPerLabel.Add(key, GetPunchedCardsPerLabel(trainingData.Select(trainingDataItem => new Tuple <IPunchedCard <string, IBitVector>, IBitVector>( puncher.Punch(key, trainingDataItem.Item1), trainingDataItem.Item2)))); }); return(punchedCardsPerKeyPerLabel); }