Repository navigation
Expand file tree
/
Copy pathToyNeuralNetworkSharp.cs
More file actions
450 lines (357 loc) · 14.9 KB
/
Copy pathToyNeuralNetworkSharp.cs
File metadata and controls
450 lines (357 loc) · 14.9 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace JAX_Evolution
{
[Serializable()]
class NeuralNetwork : ISerializable
{
readonly Matrix.DataFunction Sigmoid = (e, i ,j) => 1 / (1 + Math.Exp(-e));
readonly Matrix.DataFunction SigmoidDer = (e, i, j) => e * (1 - e);
public int Seed { get; set; }
Random RandGen { get; set; }
public double LearningRate { get; set; }
int[] Structure; //0...in, 1...hid0, last...out
Matrix[] Bias; //0...hid0, last...out
Matrix[] Weights; //0...in-hid0, last...hidn-out
public int Iterations { get; private set; }
//original structure would be: int[] {a, b, c}
public NeuralNetwork(int[] structure)
{
Structure = structure;
Weights = new Matrix[structure.Length - 1];
Bias = new Matrix[structure.Length - 1];
for (int i = 0; i < Weights.Length; i++)
Weights[i] = new Matrix(Structure[i+1], Structure[i]);
for (int i = 0; i < Weights.Length; i++)
Bias[i] = new Matrix(Structure[i+1], 1);
Seed = new Random().Next();
RandGen = new Random(Seed);
for (int i = 0; i < Weights.Length; i++)
Weights[i].Randomize(RandGen);
for (int i = 0; i < Weights.Length; i++)
Bias[i].Randomize(RandGen);
LearningRate = 0.1;
}
//public NeuralNetwork(int a, int b, int c) //original
//{
// //i,h,o layer node count
// Structure = new int[] { a, b, c };
// //Weights init
// Weights = new Matrix[2];
// Bias = new Matrix[2];
// //ih weights
// Weights[0] = new Matrix(Structure[1], Structure[0]);
// //ho weights
// Weights[1] = new Matrix(Structure[2], Structure[1]);
// //h bias
// Bias[0] = new Matrix(Structure[1], 1);
// //o bias
// Bias[1] = new Matrix(Structure[2], 1);
// //random Random object
// Seed = new Random().Next();
// RandGen = new Random(Seed);
// //randomize everything
// Weights[0].Randomize(RandGen);
// Weights[1].Randomize(RandGen);
// Bias[0].Randomize(RandGen);
// Bias[1].Randomize(RandGen);
// //Set learning rate of 0.5%
// SetLearningRate(0.005);
//}
public NeuralNetwork(NeuralNetwork nn)
{
Structure = nn.Structure;
Bias = nn.Bias;
Weights = nn.Weights;
LearningRate = nn.LearningRate;
Iterations = nn.Iterations;
RandGen = nn.RandGen;
Seed = nn.Seed;
}
//Seriazible stuff
public NeuralNetwork(SerializationInfo info, StreamingContext context)
{
Structure = (int[])info.GetValue("Structure", typeof(int[]));
Bias = (Matrix[])info.GetValue("Bias", typeof(Matrix[]));
Weights = (Matrix[])info.GetValue("Weights", typeof(Matrix[]));
LearningRate = (double)info.GetValue("LearningRate", typeof(double));
Iterations = (int)info.GetValue("Iterations", typeof(int));
RandGen = (Random)info.GetValue("RandGen", typeof(Random));
Seed = (int)info.GetValue("Seed", typeof(int));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Structure", Structure);
info.AddValue("Bias", Bias);
info.AddValue("Weights", Weights);
info.AddValue("LearningRate", LearningRate);
info.AddValue("Iterations", Iterations);
info.AddValue("RandGen", RandGen);
info.AddValue("Seed", Seed);
}
//public double[] Predict(double[] inputs_array) //original
//{
// Matrix inputs = Matrix.FromArray(inputs_array);
// // Generating the Hidden Outputs
// Matrix hidden = Matrix.Multiply(Weights[0], inputs);
// hidden.Add(Bias[0]);
// // activation function!
// hidden.Map(Sigmoid);
// // Generating the Output's Output
// Matrix output = Matrix.Multiply(Weights[1], hidden);
// output.Add(Bias[1]);
// output.Map(Sigmoid);
// // Sending back to the caller
// return output.ToArray();
//}
public double[] Predict(double[] inputs_array)
{
Matrix matrix = Matrix.FromArray(inputs_array);
for (int i = 0; i < Structure.Length - 1; i++)
{
// Generating the Hidden Outputs
Matrix matrix_next = Matrix.Multiply(Weights[i], matrix);
matrix_next.Add(Bias[i]);
// activation function!
matrix_next.Map(Sigmoid);
//pass to another layer
matrix = matrix_next;
}
// Sending last layer values to caller
return matrix.ToArray();
}
//public void Train(double[] inputs_array, double[] outputs_array) //original
//{
// if (Structure.Length != 3) throw new NotImplementedException();
// //CURRENT PREDICTION
// // Generating the Hidden Outputs
// Matrix inputs = Matrix.FromArray(inputs_array);
// Matrix hiddens = Matrix.Multiply(Weights[0], inputs);
// hiddens.Add(Bias[0]);
// // activation function!
// hiddens.Map(Sigmoid);
// // Generating the Output's Output
// Matrix outputs = Matrix.Multiply(Weights[1], hiddens);
// outputs.Add(Bias[1]);
// outputs.Map(Sigmoid);
// //ERROR CALCULATION
// //Desired output
// Matrix targets = Matrix.FromArray(outputs_array);
// // error = targets - outputs
// Matrix output_errors = Matrix.Substract(targets, outputs);
// //BACK PROPAGATION
// // gradient = outputs * (1 - outputs);
// // Calculate gradient
// Matrix gradients = Matrix.Map(outputs, SigmoidDer);
// gradients.MultiplyBy(output_errors);
// gradients.MultiplyBy(LearningRate);
// // Calculate deltas
// Matrix hidden_t = Matrix.Transpose(hiddens);
// Matrix weight_ho_deltas = Matrix.Multiply(gradients, hidden_t);
// // Adjust the weights by deltas
// Weights[1].Add(weight_ho_deltas);
// // Adjust the bias by its deltas (which is just the gradients)
// Bias[1].Add(gradients);
// // Calculate the hidden layer errors
// Matrix weight_ho_t = Matrix.Transpose(Weights[1]);
// Matrix hidden_errors = Matrix.Multiply(weight_ho_t, output_errors);
// // Calculate hidden gradient
// Matrix hidden_gradient = Matrix.Map(hiddens, SigmoidDer);
// hidden_gradient.MultiplyBy(hidden_errors);
// hidden_gradient.MultiplyBy(LearningRate);
// // Calcuate input->hidden deltas
// Matrix inputs_t = Matrix.Transpose(inputs);
// Matrix weight_ih_deltas = Matrix.Multiply(hidden_gradient, inputs_t);
// Weights[0].Add(weight_ih_deltas);
// // Adjust the bias by its deltas (which is just the gradients)
// Bias[0].Add(hidden_gradient);
// Iterations++;
//}
public void Train(double[] inputs_array, double[] outputs_array)
{
//CURRENT PREDICTION
Matrix[] Layers = new Matrix[Structure.Length];
// Generating the Hidden Outputs
Layers[0] = Matrix.FromArray(inputs_array);
for (int i = 1; i < Structure.Length; i++)
{
Layers[i] = Matrix.Multiply(Weights[i - 1], Layers[i - 1]);
Layers[i].Add(Bias[i - 1]);
Layers[i].Map(Sigmoid);
}
//ERROR CALCULATION
Matrix error = Matrix.Substract(Matrix.FromArray(outputs_array), Layers.Last());
//BACK PROPAGATION
for (int i = Structure.Length - 1; i >= 1; i--)
{
// gradient = outputs * (1 - outputs);
// Calculate gradient
Matrix gradients = Matrix.Map(Layers[i], SigmoidDer);
gradients.MultiplyBy(error);
gradients.MultiplyBy(LearningRate);
// Calculate deltas
Matrix hidden_t = Matrix.Transpose(Layers[i-1]);
Matrix weight_ho_deltas = Matrix.Multiply(gradients, hidden_t);
// Adjust the weights by deltas
Weights[i - 1].Add(weight_ho_deltas);
// Adjust the bias by its deltas (which is just the gradients)
Bias[i - 1].Add(gradients);
// Calculate the layer errors
if (i > 1) error = Matrix.Multiply(Matrix.Transpose(Weights[i - 1]), error);
}
Iterations++;
}
public void Serelize(string filename)
{
//Binary
Stream stream = File.Open(filename, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, this);
stream.Close();
////Xml (does not work with multidim. arrays [,])
//XmlSerializer seriliazer = new XmlSerializer(typeof(NeuralNetwork));
//using (TextWriter tw = new StreamWriter(filename))
// seriliazer.Serialize(tw, this);
}
public static NeuralNetwork Deserelize(string filename)
{
//Binary
Stream stream = File.Open(filename, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
NeuralNetwork nn = (NeuralNetwork)bf.Deserialize(stream);
stream.Close();
return nn;
////Xml (does not work with multidim. arrays [,])
//XmlSerializer deseriliazer = new XmlSerializer(typeof(NeuralNetwork));
//TextReader tr = new StreamReader(filename);
//return(NeuralNetwork) deseriliazer.Deserialize(tr);
}
// Adding function for neuro-evolution
public NeuralNetwork Copy()
{
return new NeuralNetwork(this);
}
public delegate double MutationFuction(double x, Random r);
// Accept an arbitrary function for mutation
public void Mutate(MutationFuction f)
{
Matrix.DataFunction g = (e, i, j) => f(e, RandGen);
for (int i = 0; i < Weights.Length; i++)
Weights[i].Map(g);
for (int i = 0; i < Bias.Length; i++)
Bias[i].Map(g);
}
}
[Serializable()]
class Matrix //2d matrix math
{
double[,] Data { get; set; }
int Rows { get; set; }
int Cols { get; set; }
public Matrix(int rows, int cols)
{
Rows = rows;
Cols = cols;
Data = new double[Rows, Cols];
Map((x, i, j) => 0);
}
public Matrix Randomize(Random r)
{
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
Data[i, j] = r.NextDouble() * 2 - 1;
return this;
}
public delegate double DataFunction(double e, int i, int j);
public Matrix Map(DataFunction func)
{
// Apply a function to every element of matrix data
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Cols; c++)
{
double val = Data[r,c];
Data[r,c] = func(val, r, c);
}
}
return this;
}
public static Matrix Map(Matrix matrix, DataFunction func)
{
// Apply a function to every element of matrix
return new Matrix(matrix.Rows, matrix.Cols).Map((e, i, j) => func(matrix.Data[i,j], i, j));
}
public static Matrix Multiply(Matrix a, Matrix b)
{
// Matrix product
if (a.Cols != b.Rows) throw new Exception("Columns of A must match rows of B.");
return new Matrix(a.Rows, b.Cols).Map(
(e, i, j) =>
{
// Dot product of values in col
double sum = 0;
for (int k = 0; k < a.Cols; k++)
{
sum += a.Data[i, k] * b.Data[k, j];
}
return sum;
}
);
}
//Vynásobí prvky Matrixů mezi sebou
public Matrix MultiplyBy(Matrix matrix)
{
if (Rows != matrix.Rows || Cols != matrix.Cols)
throw new Exception("Columns and Rows of A must match Columns and Rows of B.");
// hadamard product
return Map((e, i, j) => e * matrix.Data[i,j]);
}
//Vynásobí prvky matrixu konstatnou
public Matrix MultiplyBy(double n)
{
// hadamard product
return Map((e, i, j) => e * n);
}
public static Matrix Transpose(Matrix matrix)
{
return new Matrix(matrix.Cols, matrix.Rows).Map((e, i, j) => matrix.Data[j,i]);
}
public static Matrix FromArray(double[] inputs_array)
{
//One line copy array to Matrix object
return new Matrix(inputs_array.Length, 1).Map((e, i, j) => inputs_array[i]);
}
public double[] ToArray()
{
List<double> list = new List<double>();
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
list.Add(Data[i,j]);
return list.ToArray();
}
public Matrix Add(Matrix matrix)
{
if (Rows != matrix.Rows || Cols != matrix.Cols)
throw new Exception("Columns and Rows of A must match Columns and Rows of B.");
return Map((e, i, j) => e + matrix.Data[i,j]);
}
public Matrix Add(double a)
{
return this.Map((e, i, j) => e + a);
}
public static Matrix Substract(Matrix a, Matrix b)
{
if (a.Rows != b.Rows || a.Cols != b.Cols)
throw new Exception("Columns and Rows of A must match Columns and Rows of B.");
// Return a new Matrix a-b
return new Matrix(a.Rows, a.Cols).Map((e, i, j) => a.Data[i, j] - b.Data[i, j]);
}
}
}