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 7
/
Copy pathOneTakesAllGroup.cs
321 lines (288 loc) · 13.2 KB
/
OneTakesAllGroup.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
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
using RCNet.Extensions;
using RCNet.MathTools;
using RCNet.Neural.Data;
using RCNet.Neural.Data.Filter;
using RCNet.Neural.Network.NonRecurrent;
using System;
using System.Collections.Generic;
using System.Text;
namespace RCNet.Neural.Network.SM.Readout
{
/// <summary>
/// Implements the "One Takes All" group of readout units.
/// </summary>
/// <remarks>
/// Supports basic decision-making based directly on the results of readout units and also more advanced decision-making based on the result of a dedicated chain of network clusters.
/// </remarks>
[Serializable]
public class OneTakesAllGroup
{
//Enums
/// <summary>
/// The decision method of the "One Takes All" group.
/// </summary>
public enum OneTakesAllDecisionMethod
{
/// <summary>
/// The basic ad-hoc decision.
/// </summary>
Basic,
/// <summary>
/// The decision makes the trained cluster chain.
/// </summary>
ClusterChain
}
/// <summary>
/// This informative event occurs each time the progress of the build process takes a step forward.
/// </summary>
[field: NonSerialized]
public event OTAGBuildProgressChangedHandler OTAGBuildProgressChanged;
/// <summary>
/// The delegate of the OTAGBuildProgressChanged event handler.
/// </summary>
/// <param name="buildProgress">The current state of the build process.</param>
public delegate void OTAGBuildProgressChangedHandler(BuildProgress buildProgress);
//Attribute properties
/// <summary>
/// An index of this group within "One Takes All" groups of the readout layer.
/// </summary>
public int Index { get; }
/// <summary>
/// The name of the "One Takes All" group.
/// </summary>
public string Name { get; }
/// <summary>
/// The indexes of the member readout units.
/// </summary>
public List<int> MemberReadoutUnitIndexCollection { get; }
/// <inheritdoc cref="OneTakesAllDecisionMethod"/>
public OneTakesAllDecisionMethod DecisionMethod;
/// <summary>
/// The probabilistic cluster chain.
/// </summary>
public TNRNetClusterChain ProbabilisticClusterChain { get; private set; }
//Attributes
private readonly OneTakesAllGroupSettings _groupCfg;
//Constructors
/// <summary>
/// Creates an itialized instance ready for build
/// </summary>
/// <param name="index">An index of this group within "One Takes All" groups of the readout layer.</param>
/// <param name="groupCfg">The configuration of the "One Takes All" group.</param>
/// <param name="memberReadoutUnitIndexes">The indexes of the member readout units.</param>
public OneTakesAllGroup(int index,
OneTakesAllGroupSettings groupCfg,
IEnumerable<int> memberReadoutUnitIndexes
)
{
Index = index;
Name = groupCfg.Name;
_groupCfg = (OneTakesAllGroupSettings)groupCfg.DeepClone();
DecisionMethod = _groupCfg.DecisionCfg.DecisionMethod;
MemberReadoutUnitIndexCollection = new List<int>(memberReadoutUnitIndexes);
ProbabilisticClusterChain = null;
return;
}
//Properties
/// <summary>
/// Gets the number of classes within the group.
/// </summary>
public int NumOfMemberClasses { get { return MemberReadoutUnitIndexCollection.Count; } }
//Methods
private void OnChainBuildProgressChanged(TNRNetClusterChainBuilder.BuildProgress chainBuildProgress)
{
//Prepare group version
BuildProgress buildProgress = new BuildProgress(Name, chainBuildProgress);
//Raise event
OTAGBuildProgressChanged?.Invoke(buildProgress);
return;
}
private double[] CreateInputVector(CompositeResult[] allReadoutUnitResults)
{
OneTakesAllClusterChainDecisionSettings clusterCfg = (OneTakesAllClusterChainDecisionSettings)_groupCfg.DecisionCfg;
List<double[]> components = new List<double[]>();
foreach (int unitIdx in MemberReadoutUnitIndexCollection)
{
if (clusterCfg.UseReadoutUnitsFinalResult)
{
components.Add(allReadoutUnitResults[unitIdx].Result);
}
if (clusterCfg.UseReadoutUnitsSubResults)
{
foreach (double[] subResult in allReadoutUnitResults[unitIdx].SubResults)
{
components.Add(subResult);
}
}
}
return NonRecurrentNetUtils.Flattenize(components);
}
private double[] CreateOutputVector(double[] allReadoutUnitsIdealValues, BinFeatureFilter[] filters)
{
double[] outputVector = new double[MemberReadoutUnitIndexCollection.Count];
for (int i = 0; i < MemberReadoutUnitIndexCollection.Count; i++)
{
outputVector[i] = filters[i].ApplyReverse(allReadoutUnitsIdealValues[MemberReadoutUnitIndexCollection[i]]);
}
return outputVector;
}
/// <summary>
/// Builds the internal probabilistic cluster chain and makes the "One Takes All" group operable.
/// </summary>
/// <param name="readoutUnitsResultsCollection">The collection of the collections of all readout units composite results.</param>
/// <param name="readoutUnitsIdealValuesCollection">The collection of the collections of all readout units ideal values.</param>
/// <param name="filters">The feature filters to be used to denormalize output data.</param>
/// <param name="rand">The random object to be used.</param>
/// <param name="controller">The build process controller (optional).</param>
public void Build(List<CompositeResult[]> readoutUnitsResultsCollection,
List<double[]> readoutUnitsIdealValuesCollection,
BinFeatureFilter[] filters,
Random rand,
TNRNetBuilder.BuildControllerDelegate controller = null
)
{
if (DecisionMethod != OneTakesAllDecisionMethod.ClusterChain)
{
throw new InvalidOperationException("Wrong call of the Build method.");
}
OneTakesAllClusterChainDecisionSettings decisionCfg = (OneTakesAllClusterChainDecisionSettings)_groupCfg.DecisionCfg;
//Prepare the training data bundle for the cluster chain
VectorBundle trainingDataBundle = new VectorBundle(readoutUnitsIdealValuesCollection.Count);
for (int sampleIdx = 0; sampleIdx < readoutUnitsIdealValuesCollection.Count; sampleIdx++)
{
double[] inputVector = CreateInputVector(readoutUnitsResultsCollection[sampleIdx]);
double[] outputVector = CreateOutputVector(readoutUnitsIdealValuesCollection[sampleIdx], filters);
trainingDataBundle.AddPair(inputVector, outputVector);
}
//Cluster chain builder
TNRNetClusterChainBuilder builder = new TNRNetClusterChainBuilder(Name,
decisionCfg.ClusterChainCfg,
rand,
controller
);
builder.ChainBuildProgressChanged += OnChainBuildProgressChanged;
ProbabilisticClusterChain = builder.Build(trainingDataBundle, filters);
return;
}
/// <summary>
/// Computes the "One Takes All" group.
/// </summary>
/// <param name="allReadoutUnitResults">The collection of all readout units composite results.</param>
/// <param name="groupResult">The composite result of the group's probabilistic cluster chain.</param>
/// <param name="outputVector">The output vector.</param>
/// <returns>An index of the winning unit within the "One Takes All" group.</returns>
public int Compute(CompositeResult[] allReadoutUnitResults, out CompositeResult groupResult, out double[] outputVector)
{
int winnerIdx;
groupResult = new CompositeResult();
outputVector = new double[MemberReadoutUnitIndexCollection.Count];
if (DecisionMethod == OneTakesAllDecisionMethod.Basic)
{
for (int i = 0; i < MemberReadoutUnitIndexCollection.Count; i++)
{
//Store rescaled member units results
outputVector[i] = Interval.IntZP1.Rescale(allReadoutUnitResults[MemberReadoutUnitIndexCollection[i]].Result[0], Interval.IntN1P1);
}
//Make the sum equal to 1
outputVector.ScaleToNewSum(1d);
groupResult.Result = new double[outputVector.Length];
outputVector.CopyTo(groupResult.Result, 0);
//Rescale output vector back to -1 and 1
for (int i = 0; i < MemberReadoutUnitIndexCollection.Count; i++)
{
outputVector[i] = Interval.IntN1P1.Rescale(outputVector[i], Interval.IntZP1);
}
winnerIdx = outputVector.MaxIdx();
}
else
{
double[] inputVector = CreateInputVector(allReadoutUnitResults);
outputVector = ProbabilisticClusterChain.Compute(inputVector, out List<Tuple<int, double[]>> memberNetOuputs);
groupResult.Result = new double[outputVector.Length];
outputVector.CopyTo(groupResult.Result, 0);
groupResult.SubResults = new List<double[]>();
foreach (Tuple<int, double[]> tuple in memberNetOuputs)
{
groupResult.SubResults.Add(tuple.Item2);
}
//Rescale output to -1 and 1
for (int i = 0; i < MemberReadoutUnitIndexCollection.Count; i++)
{
outputVector[i] = Interval.IntN1P1.Rescale(outputVector[i], Interval.IntZP1);
}
winnerIdx = outputVector.MaxIdx();
}
return winnerIdx;
}
//Inner classes
/// <summary>
/// Implements the holder of the One Takes All group build progress information.
/// </summary>
public class BuildProgress : IBuildProgress
{
//Attribute properties
/// <summary>
/// Name of the One Takes All group.
/// </summary>
public string GroupName { get; }
/// <summary>
/// Information about the cluster chain build progress.
/// </summary>
public TNRNetClusterChainBuilder.BuildProgress ChainBuildProgress { get; }
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="groupName">Name of the One Takes All group.</param>
/// <param name="chainBuildProgress">The holder of the cluster chain build progress information.</param>
public BuildProgress(string groupName,
TNRNetClusterChainBuilder.BuildProgress chainBuildProgress
)
{
GroupName = groupName;
ChainBuildProgress = chainBuildProgress;
return;
}
//Properties
/// <inheritdoc/>
public bool NewEndNetwork
{
get
{
return ChainBuildProgress.NewEndNetwork;
}
}
/// <inheritdoc/>
public bool ShouldBeReported
{
get
{
return ChainBuildProgress.ShouldBeReported;
}
}
/// <inheritdoc/>
public int EndNetworkEpochNum
{
get
{
return ChainBuildProgress.EndNetworkEpochNum;
}
}
//Methods
/// <inheritdoc/>
public string GetInfoText(int margin = 0, bool includeName = true)
{
//Build the progress text message
StringBuilder progressText = new StringBuilder();
progressText.Append(new string(' ', margin));
if (includeName)
{
progressText.Append("[");
progressText.Append(GroupName);
progressText.Append("] ");
}
progressText.Append(ChainBuildProgress.GetInfoText(0, false));
return progressText.ToString();
}
}//BuildProgress
}//OneTakesAllGroup
}//Namespace