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 pathStateMachine.cs
More file actions
702 lines (652 loc) · 34.1 KB
/
StateMachine.cs
File metadata and controls
702 lines (652 loc) · 34.1 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
using RCNet.Extensions;
using RCNet.MathTools;
using RCNet.Neural.Data;
using RCNet.Neural.Network.NonRecurrent;
using RCNet.Neural.Network.SM.PM;
using RCNet.Neural.Network.SM.Preprocessing;
using RCNet.Neural.Network.SM.Preprocessing.Neuron.Predictor;
using RCNet.Neural.Network.SM.Preprocessing.Reservoir;
using RCNet.Neural.Network.SM.Readout;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Linq;
namespace RCNet.Neural.Network.SM
{
/// <summary>
/// Implements the state machine.
/// </summary>
[Serializable]
public class StateMachine
{
//Delegates
/// <summary>
/// The delegate of VerificationProgressChanged event handler.
/// </summary>
/// <param name="totalNumOfInputs">The total number of inputs to be processed.</param>
/// <param name="numOfProcessedInputs">The number of already processed inputs.</param>
public delegate void VerificationProgressChangedHandler(int totalNumOfInputs, int numOfProcessedInputs);
//Events
/// <summary>
/// This informative event occurs every time the progress of verification has changed.
/// </summary>
[field: NonSerialized]
public event VerificationProgressChangedHandler VerificationProgressChanged;
//Attribute properties
/// <summary>
/// The configuration.
/// </summary>
public StateMachineSettings Config { get; }
/// <summary>
/// The neural preprocessor.
/// </summary>
public NeuralPreprocessor NP { get; private set; }
/// <summary>
/// The readout layer.
/// </summary>
public ReadoutLayer RL { get; private set; }
//Constructors
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="cfg">The state machine configuration.</param>
public StateMachine(StateMachineSettings cfg)
{
Config = (StateMachineSettings)cfg.DeepClone();
//Neural preprocessor instance
NP = Config.NeuralPreprocessorCfg == null ? null : new NeuralPreprocessor(Config.NeuralPreprocessorCfg, Config.RandomizerSeek);
//Readout layer instance
RL = new ReadoutLayer(Config.ReadoutLayerCfg);
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="xmlFileName">The name of xml file where the root element matches the state machine configuration.</param>
public StateMachine(string xmlFileName)
{
XDocument xmlDoc = XDocument.Load(xmlFileName);
Config = new StateMachineSettings(xmlDoc.Root);
//Neural preprocessor instance
NP = Config.NeuralPreprocessorCfg == null ? null : new NeuralPreprocessor(Config.NeuralPreprocessorCfg, Config.RandomizerSeek);
//Readout layer instance
RL = new ReadoutLayer(Config.ReadoutLayerCfg);
return;
}
//Static methods
/// <summary>
/// Deserializes the state machine instance from the specified stream.
/// </summary>
/// <param name="stream">The stream to be used.</param>
/// <returns>The instance of the state machine.</returns>
public static StateMachine Deserialize(Stream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
return (StateMachine)formatter.Deserialize(stream);
}
/// <summary>
/// Deserializes the state machine instance from the specified file.
/// </summary>
/// <param name="fileName">The name of the file.</param>
/// <returns>The instance of the state machine.</returns>
public static StateMachine Deserialize(string fileName)
{
using (Stream stream = File.Open(fileName, FileMode.Open))
{
return Deserialize(stream);
}
}
//Methods
/// <summary>
/// Serializes this instance into the specified stream.
/// </summary>
/// <param name="stream">The stream to be used.</param>
public void Serialize(Stream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
return;
}
/// <summary>
/// Serializes this instance into the specified file.
/// </summary>
/// <param name="fileName">The name of the file.</param>
public void Serialize(string fileName)
{
using (Stream stream = File.Create(fileName))
{
Serialize(stream);
}
return;
}
/// <summary>
/// Resets the state machine to its initial state.
/// </summary>
public void Reset()
{
//Neural preprocessor reset
NP?.Reset();
//ReadoutLayer reset
RL.Reset();
return;
}
/// <summary>
/// Builds the predictors mapper.
/// </summary>
private PredictorsMapper BuildPredictorsMapper()
{
if (NP == null)
{
//Neural preprocessor is bypassed -> no mapper
return null;
}
else
{
//Create empty instance of the mapper
PredictorsMapper mapper = new PredictorsMapper(NP.OutputFeatureGeneralSwitchCollection);
if (Config.MapperCfg != null)
{
//Routed input field names
string[] routedInputFieldNames = Config.NeuralPreprocessorCfg.InputEncoderCfg.GetRoutedFieldNames().ToArray();
//Iterate all readout units
foreach (string readoutUnitName in Config.ReadoutLayerCfg.OutputFieldNameCollection)
{
bool[] switches = new bool[NP.OutputFeatureGeneralSwitchCollection.Length];
//Exists specific mapping?
ReadoutUnitMapSettings unitMap = Config.MapperCfg.GetMapCfg(readoutUnitName, false);
if (unitMap != null)
{
//Initially disable all predictors
switches.Populate(false);
for (int i = 0; i < NP.PredictorDescriptorCollection.Count; i++)
{
if (!NP.PredictorDescriptorCollection[i].IsInputValue)
{
string reservoirInstanceName = Config.NeuralPreprocessorCfg.ReservoirInstancesCfg.ReservoirInstanceCfgCollection[NP.PredictorDescriptorCollection[i].ReservoirID].Name;
ReservoirStructureSettings rss = Config.NeuralPreprocessorCfg.ReservoirStructuresCfg.GetReservoirStructureCfg(Config.NeuralPreprocessorCfg.ReservoirInstancesCfg.ReservoirInstanceCfgCollection[NP.PredictorDescriptorCollection[i].ReservoirID].StructureCfgName);
string poolName = rss.PoolsCfg.PoolCfgCollection[NP.PredictorDescriptorCollection[i].PoolID].Name;
switches[i] = unitMap.IsAllowedPredictor(reservoirInstanceName, poolName, (PredictorsProvider.PredictorID)NP.PredictorDescriptorCollection[i].PredictorID);
}
else
{
switches[i] = unitMap.IsAllowedInputField(NP.PredictorDescriptorCollection[i].InputFieldName);
}
}
}
else
{
//Allow all valid predictors
NP.OutputFeatureGeneralSwitchCollection.CopyTo(switches, 0);
}
//Add mapping to mapper
mapper.Add(readoutUnitName, switches);
}
}
return mapper;
}
}
/// <summary>
/// Preprocesses the data and computes the readout layer.
/// </summary>
/// <param name="inputVector">The input vector.</param>
/// <param name="readoutData">The detailed data computed by the readout layer.</param>
/// <returns>The computed output values in the natural form.</returns>
public double[] Compute(double[] inputVector, out ReadoutLayer.ReadoutData readoutData)
{
if (!RL.Trained)
{
throw new InvalidOperationException($"Readout layer is not trained.");
}
if (NP == null)
{
//Neural preprocessor is bypassed
return RL.Compute(inputVector, out readoutData);
}
else
{
//Compute and return output
return RL.Compute(NP.Preprocess(inputVector), out readoutData);
}
}
/// <summary>
/// Performs the training of the state machine.
/// </summary>
/// <param name="trainingData">The training data bundle.</param>
/// <param name="controller">The build process controller (optional).</param>
/// <returns>The training results.</returns>
public TrainingResults Train(VectorBundle trainingData, TNRNetBuilder.BuildControllerDelegate controller = null)
{
//StateMachine reset
Reset();
VectorBundle readoutTrainingData;
NeuralPreprocessor.PreprocessingOverview preprocessingOverview = null;
if (NP == null)
{
//Neural preprocessor is bypassed
readoutTrainingData = trainingData;
}
else
{
//Neural preprocessing
readoutTrainingData = NP.InitializeAndPreprocessBundle(trainingData, out preprocessingOverview);
}
//Training of the readout layer
ReadoutLayer.RegressionOverview regressionOverview = RL.Build(readoutTrainingData, BuildPredictorsMapper(), controller, Config.RandomizerSeek);
//Return the training results
return new TrainingResults(preprocessingOverview, regressionOverview);
}
/// <summary>
/// Verifies the state machine's accuracy.
/// </summary>
/// <remarks>
/// Evaluates the computed data against the ideal data.
/// </remarks>
/// <param name="verificationData">The verification data bundle.</param>
/// <returns>The verification results.</returns>
public VerificationResults Verify(VectorBundle verificationData)
{
VerificationResults verificationResults = new VerificationResults(Config.ReadoutLayerCfg);
for (int sampleIdx = 0; sampleIdx < verificationData.InputVectorCollection.Count; sampleIdx++)
{
double[] predictors;
if (NP == null)
{
//Neural preprocessor is bypassed
predictors = verificationData.InputVectorCollection[sampleIdx];
}
else
{
//Neural preprocessing
predictors = NP.Preprocess(verificationData.InputVectorCollection[sampleIdx]);
}
double[] outputVector = RL.Compute(predictors, out ReadoutLayer.ReadoutData readoutData);
verificationResults.Update(predictors, readoutData, verificationData.OutputVectorCollection[sampleIdx]);
VerificationProgressChanged?.Invoke(verificationData.InputVectorCollection.Count, sampleIdx + 1);
}
return verificationResults;
}
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
//Inner classes
/// <summary>
/// Implements the holder of the state machine training results.
/// </summary>
[Serializable]
public class TrainingResults
{
/// <summary>
/// The preprocessing overview.
/// </summary>
public NeuralPreprocessor.PreprocessingOverview PreprocessingResults { get; }
/// <summary>
/// The regression overview.
/// </summary>
public ReadoutLayer.RegressionOverview RegressionResults { get; }
//Constructor
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="preprocessingResults">The preprocessing overview.</param>
/// <param name="regressionResults">The regression overview.</param>
public TrainingResults(NeuralPreprocessor.PreprocessingOverview preprocessingResults,
ReadoutLayer.RegressionOverview regressionResults
)
{
PreprocessingResults = preprocessingResults;
RegressionResults = regressionResults;
return;
}
}//TrainingResults
/// <summary>
/// Implements the holder of the verification results.
/// </summary>
[Serializable]
public class VerificationResults
{
/// <summary>
/// The configuration of the readout layer.
/// </summary>
public ReadoutLayerSettings ReadoutLayerConfig { get; }
/// <summary>
/// The computation result data bundle.
/// </summary>
public ResultBundle ComputationResultBundle { get; }
/// <summary>
/// The collection of the readout units statistics.
/// </summary>
public List<ReadoutUnitErrorStat> ReadoutUnitStatCollection { get; }
/// <summary>
/// The collection of "One Takes All" groups statistics.
/// </summary>
public List<OneTakesAllGroupErrorStat> OneTakesAllGroupStatCollection { get; }
//Constructor
/// <summary>
/// Creates an uninitialized instance.
/// </summary>
/// <param name="readoutLayerConfig">The configuration of the readout layer.</param>
public VerificationResults(ReadoutLayerSettings readoutLayerConfig)
{
ReadoutLayerConfig = (ReadoutLayerSettings)readoutLayerConfig.DeepClone();
ComputationResultBundle = new ResultBundle();
ReadoutUnitStatCollection = new List<ReadoutUnitErrorStat>(ReadoutLayerConfig.ReadoutUnitsCfg.ReadoutUnitCfgCollection.Count);
for (int i = 0; i < ReadoutLayerConfig.ReadoutUnitsCfg.ReadoutUnitCfgCollection.Count; i++)
{
ReadoutUnitStatCollection.Add(new ReadoutUnitErrorStat(i, ReadoutLayerConfig.ReadoutUnitsCfg.ReadoutUnitCfgCollection[i]));
}
OneTakesAllGroupStatCollection = new List<OneTakesAllGroupErrorStat>();
if (ReadoutLayerConfig.OneTakesAllGroupsCfg != null)
{
foreach (OneTakesAllGroupSettings groupCfg in ReadoutLayerConfig.OneTakesAllGroupsCfg.OneTakesAllGroupCfgCollection)
{
int[] unitIndexes = ReadoutLayerConfig.GetOneTakesAllGroupMemberRUnitIndexes(groupCfg.Name).ToArray();
OneTakesAllGroupStatCollection.Add(new OneTakesAllGroupErrorStat(groupCfg.Name, unitIndexes, ReadoutLayerConfig.ReadoutUnitsCfg.ReadoutUnitCfgCollection));
}
}
return;
}
//Methods
/// <summary>
/// Updates the statistics.
/// </summary>
/// <param name="inputValues">The input values</param>
/// <param name="readoutData">The computed readout data.</param>
/// <param name="idealValues">The ideal values.</param>
public void Update(double[] inputValues, ReadoutLayer.ReadoutData readoutData, double[] idealValues)
{
//Store input, computed and ideal values
ComputationResultBundle.InputVectorCollection.Add(inputValues);
ComputationResultBundle.ComputedVectorCollection.Add(readoutData.NatDataVector);
ComputationResultBundle.IdealVectorCollection.Add(idealValues);
//Update statistics
foreach (ReadoutUnitErrorStat ruStat in ReadoutUnitStatCollection)
{
ruStat.Update(readoutData.NatDataVector, idealValues);
}
foreach (OneTakesAllGroupErrorStat grStat in OneTakesAllGroupStatCollection)
{
grStat.Update(readoutData, idealValues);
}
return;
}
/// <summary>
/// Gets the text report.
/// </summary>
/// <param name="margin">Specifies the left text margin.</param>
/// <returns>The built text report.</returns>
public string GetReport(int margin)
{
string leftMargin = margin == 0 ? string.Empty : new string(' ', margin);
StringBuilder sb = new StringBuilder();
//Report
//Readout units separatelly
foreach (ReadoutUnitErrorStat ruStat in ReadoutUnitStatCollection)
{
sb.Append(leftMargin + $"Output field [{ruStat.Name}]" + Environment.NewLine);
if (ruStat.Task == ReadoutUnit.TaskType.Classification)
{
//Classification task report
sb.Append(leftMargin + $" Classification of negative samples" + Environment.NewLine);
sb.Append(leftMargin + $" Number of samples: {ruStat.BinErrorStat.BinValErrStat[0].NumOfSamples}" + Environment.NewLine);
sb.Append(leftMargin + $" Number of errors: {ruStat.BinErrorStat.BinValErrStat[0].Sum.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Error rate: {ruStat.BinErrorStat.BinValErrStat[0].ArithAvg.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Accuracy: {(1 - ruStat.BinErrorStat.BinValErrStat[0].ArithAvg).ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Classification of positive samples" + Environment.NewLine);
sb.Append(leftMargin + $" Number of samples: {ruStat.BinErrorStat.BinValErrStat[1].NumOfSamples}" + Environment.NewLine);
sb.Append(leftMargin + $" Number of errors: {ruStat.BinErrorStat.BinValErrStat[1].Sum.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Error rate: {ruStat.BinErrorStat.BinValErrStat[1].ArithAvg.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Accuracy: {(1 - ruStat.BinErrorStat.BinValErrStat[1].ArithAvg).ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Overall classification results" + Environment.NewLine);
sb.Append(leftMargin + $" Number of samples: {ruStat.BinErrorStat.TotalErrStat.NumOfSamples}" + Environment.NewLine);
sb.Append(leftMargin + $" Number of errors: {ruStat.BinErrorStat.TotalErrStat.Sum.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Error rate: {ruStat.BinErrorStat.TotalErrStat.ArithAvg.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Accuracy: {(1 - ruStat.BinErrorStat.TotalErrStat.ArithAvg).ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
}
else
{
//Forecast task report
sb.Append(leftMargin + $" Number of samples: {ruStat.ErrorStat.NumOfSamples}" + Environment.NewLine);
sb.Append(leftMargin + $" Biggest error: {ruStat.ErrorStat.Max.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Smallest error: {ruStat.ErrorStat.Min.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Average error: {ruStat.ErrorStat.ArithAvg.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
}
sb.Append(Environment.NewLine);
}
//One-takes-all groups
foreach (OneTakesAllGroupErrorStat grStat in OneTakesAllGroupStatCollection)
{
sb.Append(leftMargin + $"One Takes All group [{grStat.Name}]" + Environment.NewLine);
foreach (OneTakesAllGroupErrorStat.MemberErrorStat memberErrStat in grStat.MemberErrorStatCollection)
{
sb.Append(leftMargin + $" Class [{memberErrStat.UnitCfg.Name}]" + Environment.NewLine);
sb.Append(leftMargin + $" Analytics" + Environment.NewLine);
sb.Append(leftMargin + $" {memberErrStat.NumOfCorrectButBellowBorderSelections.ToString(CultureInfo.InvariantCulture)}x correctly selected as a winner but the probability distributed within the group is below-border." + Environment.NewLine);
sb.Append(leftMargin + $" {memberErrStat.NumOfOverbeatedAboveBorderRawProbabilities.ToString(CultureInfo.InvariantCulture)}x correctly computed above-border raw probability but overbeated by the raw probability of another class." + Environment.NewLine);
sb.Append(leftMargin + $" Totals" + Environment.NewLine);
sb.Append(leftMargin + $" Number of samples: {memberErrStat.ErrStat.NumOfSamples}" + Environment.NewLine);
sb.Append(leftMargin + $" Number of errors: {memberErrStat.ErrStat.Sum.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Error rate: {memberErrStat.ErrStat.ArithAvg.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Accuracy: {(1 - memberErrStat.ErrStat.ArithAvg).ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
}
sb.Append(leftMargin + $" Group total" + Environment.NewLine);
sb.Append(leftMargin + $" Number of samples: {grStat.GroupErrorStat.NumOfSamples}" + Environment.NewLine);
sb.Append(leftMargin + $" Number of errors: {grStat.GroupErrorStat.Sum.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Error rate: {grStat.GroupErrorStat.ArithAvg.ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(leftMargin + $" Accuracy: {(1 - grStat.GroupErrorStat.ArithAvg).ToString(CultureInfo.InvariantCulture)}" + Environment.NewLine);
sb.Append(Environment.NewLine);
}
return sb.ToString();
}
//Inner classes
/// <summary>
/// Implements the holder of the readout unit error statistics.
/// </summary>
[Serializable]
public class ReadoutUnitErrorStat
{
/// <summary>
/// The name of the readout unit.
/// </summary>
public string Name { get; }
/// <summary>
/// The zero-based index of the readout unit.
/// </summary>
public int Index { get; }
/// <inheritdoc cref="ReadoutUnit.TaskType"/>
public ReadoutUnit.TaskType Task { get; }
/// <summary>
/// The precision error statistics.
/// </summary>
public BasicStat ErrorStat { get; }
/// <summary>
/// The binary error statistics.
/// </summary>
public BinErrStat BinErrorStat { get; }
//Constructor
/// <summary>
/// Creates an unitialized instance
/// </summary>
/// <param name="index">The zero-based index of the readout unit.</param>
/// <param name="readoutUnitCfg">The configuration of the readout unit.</param>
public ReadoutUnitErrorStat(int index, ReadoutUnitSettings readoutUnitCfg)
{
Name = readoutUnitCfg.Name;
Index = index;
Task = readoutUnitCfg.TaskCfg.Type;
ErrorStat = new BasicStat();
if (Task == ReadoutUnit.TaskType.Classification)
{
BinErrorStat = new BinErrStat(0.5d);
}
return;
}
//Methods
/// <summary>
/// Updates the statistics.
/// </summary>
/// <param name="computedValues">The computed values.</param>
/// <param name="idealValues">The ideal values.</param>
public void Update(double[] computedValues, double[] idealValues)
{
ErrorStat.AddSample(Math.Abs(computedValues[Index] - idealValues[Index]));
if (Task == ReadoutUnit.TaskType.Classification)
{
BinErrorStat.Update(computedValues[Index], idealValues[Index]);
}
return;
}
}//ReadoutUnitErrorStat
/// <summary>
/// Implements the holder of the "One Takes All" group error statistics.
/// </summary>
[Serializable]
public class OneTakesAllGroupErrorStat
{
/// <summary>
/// The name of the group.
/// </summary>
public string Name { get; }
/// <summary>
/// The binary error statistics.
/// </summary>
public BasicStat GroupErrorStat { get; }
/// <summary>
/// The collection of the group member error statistics.
/// </summary>
public List<MemberErrorStat> MemberErrorStatCollection { get; }
//Constructor
/// <summary>
/// Creates an unitialized instance
/// </summary>
/// <param name="groupName">The name of the group.</param>
/// <param name="unitIndexes">The member unit indexes.</param>
/// <param name="unitCfgCollection">The collection of all readout unit configurations.</param>
public OneTakesAllGroupErrorStat(string groupName, int[] unitIndexes, List<ReadoutUnitSettings> unitCfgCollection)
{
Name = groupName;
GroupErrorStat = new BasicStat();
MemberErrorStatCollection = new List<MemberErrorStat>();
for (int memberIdx = 0; memberIdx < unitIndexes.Length; memberIdx++)
{
MemberErrorStatCollection.Add(new MemberErrorStat(unitCfgCollection[unitIndexes[memberIdx]], unitIndexes[memberIdx], memberIdx));
}
return;
}
//Methods
/// <summary>
/// Updates the statistics.
/// </summary>
/// <param name="readoutData">The computed readout data.</param>
/// <param name="idealValues">The ideal data.</param>
public void Update(ReadoutLayer.ReadoutData readoutData, double[] idealValues)
{
ReadoutLayer.ReadoutData.OneTakesAllGroupData groupData = readoutData.GetOneTakesAllGroupData(Name);
//Determine correct member
int memberCorrectIndex = 0;
double idealMaxP = double.MinValue;
double[] memberRawProbabilities = new double[groupData.MemberReadoutUnitIndexes.Length];
for (int i = 0; i < groupData.MemberReadoutUnitIndexes.Length; i++)
{
memberRawProbabilities[i] = readoutData.ReadoutUnitDataCollection[groupData.MemberReadoutUnitIndexes[i]].RawNrmDataValue;
if (idealValues[groupData.MemberReadoutUnitIndexes[i]] > idealMaxP)
{
memberCorrectIndex = i;
idealMaxP = idealValues[groupData.MemberReadoutUnitIndexes[i]];
}
}
//Group error
double err = (groupData.MemberWinningGroupIndex == memberCorrectIndex ? 0d : 1d);
GroupErrorStat.AddSample(err);
//Member errors
MemberErrorStatCollection[memberCorrectIndex].Update(memberRawProbabilities,
groupData.MemberProbabilities,
groupData.MemberWinningGroupIndex
);
return;
}
//Inner classes
/// <summary>
/// Implements the holder of the "One Takes All" group member error statistics.
/// </summary>
[Serializable]
public class MemberErrorStat
{
//Attribute properties
/// <summary>
/// The configuration of the readout unit.
/// </summary>
public ReadoutUnitSettings UnitCfg { get; }
/// <summary>
/// An index of the readout unit within the readout layer.
/// </summary>
public int UnitIndex { get; }
/// <summary>
/// An index within the "One Takes All" group.
/// </summary>
public int MemberIndex { get; }
/// <summary>
/// The precision error statistics.
/// </summary>
public BasicStat ErrStat { get; }
/// <summary>
/// The number of correct bellow-border selections.
/// </summary>
public int NumOfCorrectButBellowBorderSelections;
/// <summary>
/// The number of overbeated correct classifications.
/// </summary>
public int NumOfOverbeatedAboveBorderRawProbabilities;
//Constructors
/// <summary>
/// Creates an uninitialized instance.
/// </summary>
/// <param name="unitCfg">The configuration of the readout unit.</param>
/// <param name="unitIndex">An index of the readout unit within the readout layer.</param>
/// <param name="memberIndex">An index within the "One Takes All" group.</param>
public MemberErrorStat(ReadoutUnitSettings unitCfg, int unitIndex, int memberIndex)
{
UnitCfg = (ReadoutUnitSettings)unitCfg.DeepClone();
UnitIndex = unitIndex;
MemberIndex = memberIndex;
ErrStat = new BasicStat();
NumOfCorrectButBellowBorderSelections = 0;
NumOfOverbeatedAboveBorderRawProbabilities = 0;
return;
}
//Methods
/// <summary>
/// Updates the statistics.
/// </summary>
/// <param name="memberRawProbabilities">The raw member probabilities.</param>
/// <param name="memberProbabilities">The member probabilities.</param>
/// <param name="memberWinningIndex">The index of the winning member.</param>
public void Update(double[] memberRawProbabilities, double[] memberProbabilities, int memberWinningIndex)
{
//Error stat
double err = (MemberIndex == memberWinningIndex ? 0d : 1d);
ErrStat.AddSample(err);
//Counters
if (MemberIndex == memberWinningIndex)
{
if (memberProbabilities[MemberIndex] < 0d)
{
++NumOfCorrectButBellowBorderSelections;
}
}
else
{
if (memberRawProbabilities[MemberIndex] >= 0d)
{
++NumOfOverbeatedAboveBorderRawProbabilities;
}
}
return;
}
}//MemberErrorStat
}//OneTakesAllGroupErrorStat
}//VerificationResult
}//StateMachine
}//Namespace