static DataGroup LoadTrainingData() { var directoryPath = Directory.GetCurrentDirectory(); var file = new FileInfo($"{directoryPath}\\Resources\\number-image-train.csv"); if (!file.Exists) { throw new Exception($"File Doesn't Exist: {file.FullName}"); } var rows = new List <double[]>(); var labels = new List <double>(); using (var reader = new StreamReader(file.FullName)) { var count = 0; while (!reader.EndOfStream) { count++; var line = reader.ReadLine(); if (count == 1) { continue; // Skip first line that has column names } var data = line.Split(","); var rowData = new double[data.Length]; string label = data[0]; double convertedLabel; if (!double.TryParse(label, out convertedLabel)) { convertedLabel = 0.0d; } labels.Add(convertedLabel); for (int i = 1; i < data.Length; i++) { string dataPoint = data[i]; double convertedDataPoint; if (!double.TryParse(dataPoint, out convertedDataPoint)) { convertedDataPoint = 0.0d; } rowData[i] = convertedDataPoint; } rows.Add(rowData); count++; } } var labelList = labels.Distinct().OrderBy(l => l).ToArray(); //var rowLabels = new List<double[]>(labelList.Length); //foreach (var label in labels) //{ // var expectedOutput = Enumerable.Repeat(-1.0d, labelList.Length).ToArray(); // for (int i = 0; i < labelList.Length; i++) // { // if (labelList[i] == label) // { // expectedOutput[i] = label; // } // } // rowLabels.Add(expectedOutput); //} //var possibleNeuronOutputs = new List<double[]>(labelList.Length); //for (int i = 0; i < labelList.Count(); i++) //{ // var result = Enumerable.Repeat(-1.0d, labelList.Count()).ToArray(); // for (int j = 0; j < labelList.Count(); j++) // { // if (j == i) // { // result[j] = labelList[j]; // } // } // possibleNeuronOutputs.Add(result); //} var dataGroup = new DataGroup(labelList, rows.ToArray(), labels.ToArray()); return(dataGroup); }
public void Train(DataGroup trainGroup) { var results = new List <DetectionResult>(); // Iterate through epochs and train network, keeping track of results for (int e = 0; e < Epochs; e++) { for (int i = 0; i < trainGroup.Values.Count; i++) { FeedForward(trainGroup.Values[i]); BackPropagate(trainGroup.LabelList, trainGroup.ValueLabels[i]); // var label = trainGroup.ValueLabels[i]; var outputLayer = Layers.Last(); var outputResults = outputLayer.Neurons.SelectMany(n => n.OutputDendrites.Select(d => d.Signal.Value)).ToArray(); // var isCorrect = label.Equals(outputResult); results.Add(new DetectionResult { Label = trainGroup.ValueLabels[i], OutputValues = outputResults }); if (i % 100 == 0) { var displayList = new List <string>(); for (int l = 0; l < trainGroup.LabelList.Length; l++) { var label = trainGroup.LabelList[l]; var resultsList = results.Where(r => r.Label == label).ToList(); if (resultsList.Count > 0) { int correctCount = 0; foreach (var result in resultsList) { var maxValue = result.OutputValues.Max(); var maxValueIndex = Array.IndexOf(result.OutputValues, maxValue); if (l == maxValueIndex) { correctCount++; } } var line = $"Label {label} Accuracy: {(correctCount / resultsList.Count) * 100}%"; displayList.Add(line); } else { displayList.Add($"Label {label} Accuracy: NOT YET CALCULATED"); } } Console.WriteLine(); Console.WriteLine(String.Join("\n", displayList)); } //if (i % 100 == 0) //{ // Console.WriteLine(); // foreach (var result in accuracyResults.OrderBy(r => r.RelevantKey).ToList()) // { // Console.WriteLine($"{result.RelevantKey}\tAttempts: {result.Count}\tCorrect: {result.Correct}\tIncorrect: {result.Incorrect}\tAccuracy: {(result.Count == 0 ? 0 : (result.Correct / result.Count))}"); // } //} } } }