forked from primaryobjects/deep-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.cs
More file actions
113 lines (98 loc) · 4.16 KB
/
Copy pathDataManager.cs
File metadata and controls
113 lines (98 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeepLearning
{
public static class DataManager
{
public static double[][] Load(string pathName, out double[][] outputs)
{
List<double[]> list = new List<double[]>();
List<double[]> output = new List<double[]>();
// Read data file.
using (FileStream fs = File.Open(pathName, FileMode.Open, FileAccess.Read))
{
using (BufferedStream bs = new BufferedStream(fs))
{
using (StreamReader sr = new StreamReader(bs))
{
List<double> row = new List<double>();
bool readOutput = false;
string line;
while ((line = sr.ReadLine()) != null)
{
// Collect each 0 and 1 from the data.
foreach (char ch in line)
{
if (!readOutput)
{
// Reading input.
if (ch != ' ' && ch != '\n')
{
// Add this digit to our input.
row.Add(Double.Parse(ch.ToString()));
}
else if (ch == ' ')
{
// End of input reached. Store the input row.
list.Add(row.ToArray());
// Start a new input row.
row = new List<double>();
// Set flag to read output label.
readOutput = true;
}
}
else
{
// Read output label.
output.Add(FormatOutputVector(Double.Parse(ch.ToString())));
// Set flag to read inputs for next row.
readOutput = false;
}
}
}
}
}
}
// Set outputs.
outputs = output.ToArray();
// Return inputs;
return list.ToArray();
}
#region Utility Methods
/// <summary>
/// Converts a numeric output label (0, 1, 2, 3, etc) to its cooresponding array of doubles, where all values are 0 except for the index matching the label (ie., if the label is 2, the output is [0, 0, 1, 0, 0, ...]).
/// </summary>
/// <param name="label">double</param>
/// <returns>double[]</returns>
public static double[] FormatOutputVector(double label)
{
double[] output = new double[10];
for (int i = 0; i < output.Length; i++)
{
if (i == label)
{
output[i] = 1;
}
else
{
output[i] = 0;
}
}
return output;
}
/// <summary>
/// Finds the largest output value in an array and returns its index. This allows for sequential classification from the outputs of a neural network (ie., if output at index 2 is the largest, the classification is class "3" (zero-based)).
/// </summary>
/// <param name="output">double[]</param>
/// <returns>double</returns>
public static double FormatOutputResult(double[] output)
{
return output.ToList().IndexOf(output.Max());
}
#endregion
}
}