This repository was archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFeedForwardNetwork.cs
More file actions
481 lines (450 loc) · 19.7 KB
/
FeedForwardNetwork.cs
File metadata and controls
481 lines (450 loc) · 19.7 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
using RCNet.Extensions;
using RCNet.MathTools;
using RCNet.Neural.Activation;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RCNet.Neural.Network.NonRecurrent.FF
{
/// <summary>
/// Implements the Feed Forward network supporting multiple hidden layers.
/// </summary>
[Serializable]
public class FeedForwardNetwork : INonRecurrentNetwork
{
//Constants
/// <summary>
/// The dummy bias input.
/// </summary>
public const double BiasValue = 1d;
/// <summary>
/// The default minimum weight for the random initialization.
/// </summary>
public const double WeightDefaultIniMin = 0.005;
/// <summary>
/// The default maximum weight for the random initialization.
/// </summary>
public const double WeightDefaultIniMax = 0.05;
//Attribute properties
/// <inheritdoc/>
public int NumOfInputValues { get; }
/// <inheritdoc/>
public int NumOfOutputValues { get; }
/// <summary>
/// The total number of network's neurons.
/// </summary>
public int NumOfNeurons { get; private set; }
/// <summary>
/// The collection of the network's layers.
/// </summary>
public List<Layer> LayerCollection { get; }
//Attributes
private double[] _flatWeights;
private bool _isAllowedNguyenWidrowRandomization;
//Constructor
/// <summary>
/// Creates an unitialized instance.
/// </summary>
/// <param name="numOfInputValues">The number of the network's input values.</param>
/// <param name="numOfOutputValues">The number of the network's output values.</param>
public FeedForwardNetwork(int numOfInputValues, int numOfOutputValues)
{
//Input/Output counts
NumOfInputValues = numOfInputValues;
NumOfOutputValues = numOfOutputValues;
NumOfNeurons = -1;
//Network layers
LayerCollection = new List<Layer>();
//Flat weights
_flatWeights = null;
//Nguyen Widrow initial randomization
_isAllowedNguyenWidrowRandomization = false;
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="numOfInputValues">The number of the network's input values.</param>
/// <param name="numOfOutputValues">The number of the network's output values.</param>
/// <param name="cfg">The configuration of the network and associated trainer.</param>
public FeedForwardNetwork(int numOfInputValues, int numOfOutputValues, FeedForwardNetworkSettings cfg)
: this(numOfInputValues, numOfOutputValues)
{
Random rand = new Random(1);
//Initialize FF network
for (int i = 0; i < cfg.HiddenLayersCfg.HiddenLayerCfgCollection.Count; i++)
{
AddLayer(cfg.HiddenLayersCfg.HiddenLayerCfgCollection[i].NumOfNeurons,
(AFAnalogBase)ActivationFactory.CreateAF(cfg.HiddenLayersCfg.HiddenLayerCfgCollection[i].ActivationCfg, rand)
);
}
FinalizeStructure((AFAnalogBase)ActivationFactory.CreateAF(cfg.OutputActivationCfg, rand));
return;
}
//Properties
/// <summary>
/// Indicates the network structure is finalized.
/// </summary>
public bool Finalized { get { return NumOfNeurons > 0; } }
/// <inheritdoc/>
public int NumOfWeights { get { return _flatWeights.Length; } }
/// <inheritdoc/>
public Interval OutputRange { get { return LayerCollection[LayerCollection.Count - 1].Activation.OutputRange; } }
//Static methods
/// <summary>
/// Tests whether the activation function can be used as the FF network's output layer activation.
/// </summary>
/// <param name="activationCfg">The configuration of the activation function.</param>
public static bool IsAllowedOutputAF(IActivationSettings activationCfg)
{
if (activationCfg.TypeOfActivation != ActivationType.Analog)
{
return false;
}
AFAnalogBase analogAF = (AFAnalogBase)ActivationFactory.CreateAF(activationCfg, new Random(0));
if (!analogAF.SupportsDerivative)
{
return false;
}
return true;
}
/// <summary>
/// Tests whether the activation function can be used as the FF network's hidden layer activation.
/// </summary>
/// <param name="activationCfg">The configuration of the activation function.</param>
public static bool IsAllowedHiddenAF(IActivationSettings activationCfg)
{
if (activationCfg.TypeOfActivation != ActivationType.Analog)
{
return false;
}
AFAnalogBase analogAF = (AFAnalogBase)ActivationFactory.CreateAF(activationCfg, new Random(0));
if (!analogAF.SupportsDerivative || analogAF.DependsOnSorround)
{
return false;
}
return true;
}
//Methods
/// <summary>
/// Adds the new hidden layer into the network structure.
/// </summary>
/// <param name="numOfNeurons">The number of layer's neurons.</param>
/// <param name="activation">The activation function of the layer neurons.</param>
public void AddLayer(int numOfNeurons, AFAnalogBase activation)
{
if (!Finalized)
{
if (activation.DependsOnSorround)
{
throw new ArgumentException("Activation requires multiple input for the Compute method. It is not allowed for the hidden layer.", "activation");
}
//Add new layer
LayerCollection.Add(new Layer(numOfNeurons, activation));
}
else
{
throw new InvalidOperationException($"Can´t add new layer. Network structure is finalized.");
}
return;
}
/// <summary>
/// Finalizes the network internal structure and locks it against the further changes.
/// </summary>
/// <param name="outputActivation">The activation function of the output layer.</param>
public void FinalizeStructure(AFAnalogBase outputActivation)
{
if (Finalized)
{
throw new InvalidOperationException($"Network structure has been already finalized.");
}
if (outputActivation.DependsOnSorround && NumOfOutputValues < 2)
{
throw new ArgumentException("Activation requires multiple input for the Compute method but number of output values is less than 2.", "outputActivation");
}
//Add output layer
LayerCollection.Add(new Layer(NumOfOutputValues, outputActivation));
//Finalize layers
int numOfInputNodes = NumOfInputValues;
int neuronsFlatStartIdx = 0;
int weightsFlatStartIdx = 0;
_isAllowedNguyenWidrowRandomization = true;
foreach (Layer layer in LayerCollection)
{
layer.FinalizeStructure(numOfInputNodes, neuronsFlatStartIdx, weightsFlatStartIdx);
neuronsFlatStartIdx += layer.NumOfLayerNeurons;
weightsFlatStartIdx += layer.NumOfLayerNeurons * layer.NumOfInputNodes + layer.NumOfLayerNeurons;
numOfInputNodes = layer.NumOfLayerNeurons;
if (layer.Activation.GetType() != typeof(AFAnalogElliot) &&
layer.Activation.GetType() != typeof(AFAnalogTanH)
)
{
_isAllowedNguyenWidrowRandomization = false;
}
}
if (LayerCollection.Count < 2)
{
_isAllowedNguyenWidrowRandomization = false;
}
NumOfNeurons = neuronsFlatStartIdx;
_flatWeights = new double[weightsFlatStartIdx];
return;
}
/// <summary>
/// Randomizes internal weights using the Nguyen Widrow method.
/// </summary>
/// <param name="rand">The random generator to be used.</param>
private void RandomizeWeightsByNguyenWidrowMethod(Random rand)
{
foreach (Layer layer in LayerCollection)
{
int weightFlatIndex = layer.WeightsStartFlatIdx;
int biasFlatIndex = layer.BiasesStartFlatIdx;
double b = 0.35d * Math.Pow(layer.NumOfLayerNeurons, (1d / layer.NumOfInputNodes));
for (int layerNeuronIdx = 0; layerNeuronIdx < layer.NumOfLayerNeurons; layerNeuronIdx++, biasFlatIndex++)
{
for (int inputNodeIdx = 0; inputNodeIdx < layer.NumOfInputNodes; inputNodeIdx++, weightFlatIndex++)
{
_flatWeights[weightFlatIndex] = rand.NextRangedUniformDouble(0, b);
}
_flatWeights[biasFlatIndex] = rand.NextRangedUniformDouble(-b, b);
}
}
return;
}
/// <inheritdoc/>
public void RandomizeWeights(Random rand)
{
if (!Finalized)
{
throw new InvalidOperationException($"Can´t randomize weights. Network structure is not finalized.");
}
if (_isAllowedNguyenWidrowRandomization)
{
RandomizeWeightsByNguyenWidrowMethod(rand);
}
else
{
rand.FillUniform(_flatWeights, WeightDefaultIniMin, WeightDefaultIniMax, true);
}
return;
}
/// <inheritdoc/>
public INonRecurrentNetwork DeepClone()
{
FeedForwardNetwork clone = new FeedForwardNetwork(NumOfInputValues, NumOfOutputValues)
{
NumOfNeurons = NumOfNeurons
};
foreach (Layer layer in LayerCollection)
{
clone.LayerCollection.Add(layer.DeepClone());
}
clone._flatWeights = (double[])_flatWeights.Clone();
clone._isAllowedNguyenWidrowRandomization = _isAllowedNguyenWidrowRandomization;
return clone;
}
/// <inheritdoc/>
public double[] Compute(double[] input)
{
double[] result = input;
foreach (Layer layer in LayerCollection)
{
result = layer.Compute(result, _flatWeights);
}
return result;
}
/// <summary>
/// Computes the output values (slower version for training purposes).
/// </summary>
/// <param name="input">The input values to be passed into the network.</param>
/// <param name="layerInputCollection">It must be the instantiated empty collection. Function will add inputs for each network layer into this collection.</param>
/// <param name="flatDerivatives">It must be the allocated array of length = NumOfNeurons (flat structure). Function will set the activation derivatives into this array.</param>
/// <returns>The computed output values.</returns>
public double[] Compute(double[] input, List<double[]> layerInputCollection, double[] flatDerivatives)
{
double[] result = input;
foreach (Layer layer in LayerCollection)
{
layerInputCollection.Add(result);
result = layer.Compute(result, _flatWeights, flatDerivatives);
}
return result;
}
/// <inheritdoc/>
public BasicStat ComputeBatchErrorStat(List<double[]> inputCollection, List<double[]> idealOutputCollection, out List<double[]> computedOutputCollection)
{
double[][] computedOutputs = new double[idealOutputCollection.Count][];
double[] flatErrors = new double[inputCollection.Count * NumOfOutputValues];
Parallel.For(0, inputCollection.Count, row =>
{
double[] computedOutputVector = Compute(inputCollection[row]);
computedOutputs[row] = computedOutputVector;
for (int i = 0; i < NumOfOutputValues; i++)
{
flatErrors[row * NumOfOutputValues + i] = Math.Abs(idealOutputCollection[row][i] - computedOutputVector[i]);
}
});
computedOutputCollection = new List<double[]>(computedOutputs);
return new BasicStat(flatErrors);
}
/// <inheritdoc/>
public BasicStat ComputeBatchErrorStat(List<double[]> inputCollection, List<double[]> idealOutputCollection)
{
double[] flatErrors = new double[inputCollection.Count * NumOfOutputValues];
Parallel.For(0, inputCollection.Count, row =>
{
double[] computedOutputVector = Compute(inputCollection[row]);
for (int i = 0; i < NumOfOutputValues; i++)
{
flatErrors[row * NumOfOutputValues + i] = Math.Abs(idealOutputCollection[row][i] - computedOutputVector[i]);
}
});
return new BasicStat(flatErrors);
}
/// <summary>
/// Gets the copy of internal weights (in a flat format).
/// </summary>
public double[] GetWeightsCopy()
{
return (double[])_flatWeights.Clone();
}
/// <summary>
/// Sets the internal weights.
/// </summary>
/// <param name="newFlatWeights">The new weights to be adopted (in a flat format).</param>
public void SetWeights(double[] newFlatWeights)
{
newFlatWeights.CopyTo(_flatWeights, 0);
return;
}
/// <inheritdoc/>
public BasicStat ComputeWeightsStat()
{
return (new BasicStat(_flatWeights));
}
//Inner classes
/// <summary>
/// Implements the layer of the feed forward network.
/// </summary>
[Serializable]
public class Layer
{
//Attribute properties
/// <summary>
/// The activation function of the layer.
/// </summary>
public AFAnalogBase Activation { get; }
/// <summary>
/// The number of layer input nodes.
/// </summary>
public int NumOfInputNodes { get; private set; }
/// <summary>
/// The number of layer neurons.
/// </summary>
public int NumOfLayerNeurons { get; }
/// <summary>
/// The starting index of this layer weights in a flat structure.
/// </summary>
public int WeightsStartFlatIdx { get; private set; }
/// <summary>
/// The starting index of this layer biases in a flat structure.
/// </summary>
public int BiasesStartFlatIdx { get; private set; }
/// <summary>
/// The starting index of this layer neurons in a flat structure.
/// </summary>
public int NeuronsStartFlatIdx { get; private set; }
//Constructor
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="numOfNeurons">The number of layer neurons.</param>
/// <param name="activation">The activation function of the layer.</param>
public Layer(int numOfNeurons, AFAnalogBase activation)
{
//Check correctness
if (numOfNeurons < 1)
{
throw new ArgumentOutOfRangeException("numOfNeurons", $"Invalid parameter value: {numOfNeurons}");
}
//Setup
Activation = activation ?? throw new ArgumentException("activation", "Activation can't be null");
NumOfLayerNeurons = numOfNeurons;
NumOfInputNodes = -1;
WeightsStartFlatIdx = 0;
BiasesStartFlatIdx = 0;
NeuronsStartFlatIdx = 0;
return;
}
//Properties
/// <summary>
/// Indicates the layer structure is finalized.
/// </summary>
public bool Finalized { get { return NumOfInputNodes > 0; } }
//Methods
/// <summary>
/// Finalizes the layer structure.
/// </summary>
/// <param name="numOfInputNodes">The number of input nodes.</param>
/// <param name="neuronsFlatStartIdx">The starting index of this layer neurons in a flat structure.</param>
/// <param name="weightsFlatStartIdx">The starting index of this layer weights in a flat structure.</param>
internal void FinalizeStructure(int numOfInputNodes, int neuronsFlatStartIdx, int weightsFlatStartIdx)
{
NumOfInputNodes = numOfInputNodes;
WeightsStartFlatIdx = weightsFlatStartIdx;
BiasesStartFlatIdx = weightsFlatStartIdx + NumOfLayerNeurons * NumOfInputNodes;
NeuronsStartFlatIdx = neuronsFlatStartIdx;
return;
}
/// <summary>
/// Creates the deep copy instance of this layer.
/// </summary>
internal Layer DeepClone()
{
Layer clone = new Layer(NumOfLayerNeurons, Activation)
{
NumOfInputNodes = NumOfInputNodes,
WeightsStartFlatIdx = WeightsStartFlatIdx,
BiasesStartFlatIdx = BiasesStartFlatIdx,
NeuronsStartFlatIdx = NeuronsStartFlatIdx
};
return clone;
}
/// <summary>
/// Computes the layer neurons.
/// </summary>
/// <param name="inputs">The inputs for this layer.</param>
/// <param name="flatWeights">The network's weights in a flat structure.</param>
/// <param name="flatDerivatives">The network's derivatives in a flat structure.</param>
/// <returns>The layer activations.</returns>
internal double[] Compute(double[] inputs, double[] flatWeights, double[] flatDerivatives = null)
{
int weightFlatIdx = WeightsStartFlatIdx;
int biasFlatIdx = BiasesStartFlatIdx;
//Compute summed weighted inputs
double[] sums = new double[NumOfLayerNeurons];
for (int neuronIdx = 0; neuronIdx < NumOfLayerNeurons; neuronIdx++, biasFlatIdx++)
{
sums[neuronIdx] = flatWeights[biasFlatIdx] * BiasValue;
for (int inputIdx = 0; inputIdx < NumOfInputNodes; inputIdx++, weightFlatIdx++)
{
sums[neuronIdx] += flatWeights[weightFlatIdx] * inputs[inputIdx];
}
}
//Compute activations
double[] activations = new double[NumOfLayerNeurons];
Activation.Compute(sums, activations);
if (flatDerivatives != null)
{
double[] derivatives = new double[NumOfLayerNeurons];
//Compute derivatives
Activation.ComputeDerivative(activations, sums, derivatives);
//Copy derivatives to flat buffer
derivatives.CopyTo(flatDerivatives, NeuronsStartFlatIdx);
}
return activations;
}
}//Layer
}//FeedForwardNetwork
}//Namespace