-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
231 lines (187 loc) · 6.83 KB
/
Program.cs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;
namespace DigitsNet
{
class Program
{
static void Main(string[] args)
{
ReadMNIST rd = new ReadMNIST();
List<int> ls = new List<int>{ 784, 30, 10 };
Network n = new Network(ls);
n.SGD(rd.allData, 30, 10, 3.0);
Console.ReadKey();
}
}
public class Network
{
List<Matrix<double>> weights = new List<Matrix<double>>();
List<Matrix<double>> biases = new List<Matrix<double>>();
int numLayers;
public Network(List<int> sizes)
{
numLayers = sizes.Count();
for(int i = 1; i < numLayers; i++)
{
biases.Add(Matrix<double>.Build.Random(sizes[i], 1));
}
for(int i = 1;i < numLayers; i++)
{
weights.Add(Matrix<double>.Build.Random(sizes[i], sizes[i - 1]));
}
}
public Matrix<double> feedForward(Matrix<double> a)
{
for (int i = 0; i < biases.Count(); i++)
{
for(int j = 0; j < weights.Count(); j++)
{
a = sigmoid(weights[i] * a + biases[j]);
}
}
return a;
}
public double sigmoid(double z)
{
return 1.0 / (1.0 + Math.Pow(2.71828, -z));
}
public double sigmoidPrime(double z)
{
return sigmoid(z) * (1 - sigmoid(z));
}
public Matrix<double> sigmoid(Matrix<double> z)
{
Matrix<double> result = z;
for(int i =0;i< z.RowCount; i++)
{
result[i, 0] = sigmoid(z[i, 0]);
}
return result;
}
public Matrix<double> sigmoidPrime(Matrix<double> z)
{
Matrix<double> result = z;
for(int i = 0; i < z.RowCount; i++)
{
result[i, 0] = sigmoidPrime(z[i, 0]);
}
return result;
}
public void SGD(List<List<Matrix<double>>> trainingData,int reps, int miniBatchSize,
double N, List<Matrix<double>> testData = null)
{
int nTest = 0;
List<List<List<Matrix<double>>>> miniBatches = new List<List<List<Matrix<double>>>>();
if (testData != null)
{
nTest = testData.Count();
}
int n = trainingData.Count();
for(int i = 0; i < reps; i++)
{
//init mini batches
Random rand = new Random();
List<List<Matrix<double>>> miniBatch = new List<List<Matrix<double>>>();
for(int j = 0; j < miniBatchSize; j++)
{
miniBatch.Add(trainingData[rand.Next(0, n - 1)]);
}
miniBatches.Add(miniBatch);
}
for(int i = 0; i < reps; i++)
{
updateMiniBatch(miniBatches[i], N);
}
}
public void updateMiniBatch(List<List<Matrix<double>>> miniBatch,double N)
{
List<Matrix<double>> nabla_b = new List<Matrix<double>>();
List<Matrix<double>> nabla_w = new List<Matrix<double>>();
List<Matrix<double>> d_nabla_b = new List<Matrix<double>>();
List<Matrix<double>> d_nabla_w = new List<Matrix<double>>();
for (int i = 0; i < biases.Count(); i++)
{
nabla_b.Add(Matrix<double>.Build.Dense(biases[i].RowCount, 1));
}
for(int i = 0; i < weights.Count(); i++)
{
nabla_w.Add(Matrix<double>.Build.Dense(weights[i].RowCount, weights[i].ColumnCount));
}
//Acquire gradients
for(int i = 0; i < miniBatch.Count(); i++)
{
List<List<Matrix<double>>> temp = backProp(miniBatch[i][0],miniBatch[i][1]);
d_nabla_b = temp[0];
d_nabla_w = temp[1];
for(int j = 0; j < nabla_b.Count(); j++)
{
nabla_b[j] = nabla_b[j] + d_nabla_b[j];
}
for(int j = 0; j < nabla_w.Count(); j++)
{
nabla_w[j] = nabla_w[j] + d_nabla_w[j];
}
}
for(int i = 0; i < weights.Count(); i++)
{
weights[i] = weights[i] - (N / miniBatch.Count()) * nabla_w[i];
}
for(int i = 0; i < biases.Count(); i++)
{
biases[i] = biases[i] - (N / miniBatch.Count()) * nabla_b[i];
}
}
public List<List<Matrix<double>>> backProp(Matrix<double> x, Matrix<double> y)
{
List<Matrix<double>> activations = new List<Matrix<double>>();
Matrix<double> activation = x;
activations.Add(x);
List<Matrix<double>> zs = new List<Matrix<double>>();
List<Matrix<double>> nambla_b = biases;
List<Matrix<double>> nambla_w = weights;
//Forward
for (int i = 0; i < numLayers-1; i++)
{
Matrix<double> z = weights[i] * activation + biases[i];
zs.Add(z);
activation = sigmoid(z);
activations.Add(activation);
}
//Backward
Matrix<double> del = Hadamard((activations[activations.Count()-1] - y), sigmoidPrime(zs[zs.Count()-1]));
nambla_b[nambla_b.Count()-1] = del;
nambla_w[nambla_w.Count()-1] = del * activations[activations.Count()-2].Transpose();
for(int l = 2; l < numLayers; l++)
{
Matrix<double> z = zs[zs.Count()-l];
Matrix<double> sp = sigmoidPrime(z);
del = weights[weights.Count() - l + 1].Transpose() * del;
del = Hadamard(del, sp);
nambla_b[nambla_b.Count()-l] = del;
nambla_w[nambla_w.Count()-l] = del * activations[activations.Count()-l - 1].Transpose();
}
List<List<Matrix<double>>> res = new List<List<Matrix<double>>>();
res.Add(nambla_b);
res.Add(nambla_w);
return res;
}
public Matrix<double> Hadamard(Matrix<double> slf, Matrix<double> other)
{
Matrix<double> res = slf;
for (int i = 0; i < slf.RowCount; i++)
{
for(int j = 0; j < slf.ColumnCount; j++)
{
res[i, j] = slf[i, j] * other[i, j];
}
}
return res;
}
}
}