-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompositeModel.cs
303 lines (285 loc) · 12.6 KB
/
CompositeModel.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
using EasyMLCore.Data;
using EasyMLCore.Extensions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace EasyMLCore.MLP
{
/// <summary>
/// Implements a model of aggregated child models where
/// child model can be any model (including other composite models).
/// Model output is weighted average of root child models outputs.
/// </summary>
[Serializable]
public class CompositeModel : MLPModelBase
{
//Constants
/// <summary>
/// Short identifier in context path.
/// </summary>
public const string ContextPathID = "Composite";
//Attributes
private readonly List<MLPModelBase> _members;
private double[][] _weights;
//Constructor
/// <summary>
/// Creates an uninitialized instance.
/// </summary>
/// <param name="modelConfig">Model configuration.</param>
/// <param name="name">Model name.</param>
/// <param name="taskType">Output task.</param>
/// <param name="outputFeatureNames">Names of output features.</param>
private CompositeModel(CompositeModelConfig modelConfig,
string name,
OutputTaskType taskType,
IEnumerable<string> outputFeatureNames
)
: base(modelConfig, name, taskType, outputFeatureNames)
{
_members = new List<MLPModelBase>();
_weights = null;
return;
}
/// <summary>
/// Deep copy constructor.
/// </summary>
/// <param name="source">The source instance.</param>
public CompositeModel(CompositeModel source)
: base(source)
{
_members = new List<MLPModelBase>();
foreach (MLPModelBase member in source._members)
{
_members.Add(member.DeepClone());
}
_weights = (double[][])source._weights.Clone();
return;
}
//Methods
/// <summary>
/// Adds a new member.
/// </summary>
/// <param name="newMember">A new member to be added.</param>
private void AddMember(MLPModelBase newMember)
{
//Checks
if (newMember.NumOfOutputFeatures != NumOfOutputFeatures)
{
throw new ArgumentException("Different number of new member outputs.", nameof(newMember));
}
if (newMember.TaskType != TaskType)
{
throw new ArgumentException("Different output task of new member.", nameof(newMember));
}
//Add new member
_members.Add(newMember);
return;
}
/// <summary>
/// Sets the model operationable.
/// </summary>
private void SetOperationable()
{
//Checks
if (_members.Count < 1)
{
throw new InvalidOperationException("At least one member must be added before the finalization.");
}
//Set weights
_weights = GetWeights(_members);
//Set metrics
//Finalize model
FinalizeModel(new MLPModelConfidenceMetrics(TaskType, (from member in _members select member.ConfidenceMetrics)));
return;
}
/// <summary>
/// Computes outputs of all members.
/// </summary>
/// <param name="inputVector">Input vector.</param>
private List<double[]> ComputeMembers(double[] inputVector)
{
List<double[]> outputVectors = new List<double[]>(_members.Count);
for (int memberIdx = 0; memberIdx < _members.Count; memberIdx++)
{
outputVectors.Add(_members[memberIdx].Compute(inputVector));
}
return outputVectors;
}
/// <inheritdoc/>
public override double[] Compute(double[] input)
{
return ComputeAggregation(ComputeMembers(input), _weights);
}
/// <inheritdoc/>
public override string GetInfoText(bool detail = false, int margin = 0)
{
margin = Math.Max(margin, 0);
StringBuilder sb = new StringBuilder($"{Name} [{GetType()}]{Environment.NewLine}");
sb.Append($" Task type : {TaskType.ToString()}{Environment.NewLine}");
sb.Append($" Output features info : {OutputFeatureNames.Count.ToString(CultureInfo.InvariantCulture)}");
int fIdx = 0;
foreach (string outputFeatureName in OutputFeatureNames)
{
sb.Append($" [{outputFeatureName}, {ConfidenceMetrics.FeatureConfidences[fIdx++].ToString("F3", CultureInfo.InvariantCulture)}]");
}
sb.Append(Environment.NewLine);
sb.Append($" Number of member models: {_members.Count.ToString(CultureInfo.InvariantCulture)}{Environment.NewLine}");
if (detail)
{
sb.Append($" Inner models one by one >>>{Environment.NewLine}");
for (int i = 0; i < _members.Count; i++)
{
sb.Append(_members[i].GetInfoText(detail, 8));
}
}
string infoText = sb.ToString();
if (margin > 0)
{
infoText = infoText.Indent(margin);
}
return infoText;
}
/// <inheritdoc/>
public override MLPModelDiagnosticData DiagnosticTest(SampleDataset testingData, ProgressChangedHandler progressInfoSubscriber = null)
{
MLPModelErrStat errStat = Test(testingData, out _, progressInfoSubscriber);
MLPModelDiagnosticData diagData = new MLPModelDiagnosticData(Name, errStat);
foreach(MLPModelBase model in _members)
{
MLPModelDiagnosticData memberDiagData = model.DiagnosticTest(testingData, progressInfoSubscriber);
diagData.AddSubModelDiagData(memberDiagData);
}
diagData.SetFinalized();
return diagData;
}
/// <inheritdoc/>
public override MLPModelBase DeepClone()
{
return new CompositeModel(this);
}
//Static methods
/// <summary>
/// Builds a CompositeModel.
/// </summary>
/// <param name="cfg">Model configuration.</param>
/// <param name="name">Model name.</param>
/// <param name="taskType">Output task type.</param>
/// <param name="outputFeatureNames">Names of output features.</param>
/// <param name="trainingData">Training samples.</param>
/// <param name="progressInfoSubscriber">Subscriber will receive notification event about progress. (Parameter can be null).</param>
/// <returns>Built model.</returns>
public static CompositeModel Build(IModelConfig cfg,
string name,
OutputTaskType taskType,
List<string> outputFeatureNames,
SampleDataset trainingData,
ProgressChangedHandler progressInfoSubscriber = null
)
{
//Checks
if (cfg == null)
{
throw new ArgumentNullException(nameof(cfg));
}
if (cfg.GetType() != typeof(CompositeModelConfig))
{
throw new ArgumentException($"Wrong type of configuration. Expected {typeof(CompositeModelConfig)} but received {cfg.GetType()}.", nameof(cfg));
}
//Composite model
CompositeModelConfig modelConfig = (CompositeModelConfig)cfg;
CompositeModel model = new CompositeModel(modelConfig,
(name + CompositeModel.ContextPathID),
taskType,
outputFeatureNames
);
//Sub models build
for (int subModelIdx = 0; subModelIdx < modelConfig.SubModelCfgCollection.Count; subModelIdx++)
{
string subModelNum = "M" + (subModelIdx + 1).ToLeftPaddedString(modelConfig.SubModelCfgCollection.Count, '0');
Type subModelCfgType = modelConfig.SubModelCfgCollection[subModelIdx].GetType();
string subModelName = $"{model.Name}.{subModelNum}-";
if (subModelCfgType == typeof(NetworkModelConfig))
{
NetworkModel subModel =
NetworkModel.Build(modelConfig.SubModelCfgCollection[subModelIdx],
subModelName,
taskType,
outputFeatureNames,
trainingData,
null,
progressInfoSubscriber
);
model.AddMember(subModel);
}
else if (subModelCfgType == typeof(CrossValModelConfig))
{
CrossValModel subModel =
CrossValModel.Build(modelConfig.SubModelCfgCollection[subModelIdx],
subModelName,
taskType,
outputFeatureNames,
trainingData,
progressInfoSubscriber
);
model.AddMember(subModel);
}
else if (subModelCfgType == typeof(StackingModelConfig))
{
StackingModel subModel =
StackingModel.Build(modelConfig.SubModelCfgCollection[subModelIdx],
subModelName,
taskType,
outputFeatureNames,
trainingData,
progressInfoSubscriber
);
model.AddMember(subModel);
}
else if (subModelCfgType == typeof(BHSModelConfig))
{
BHSModel subModel =
BHSModel.Build(modelConfig.SubModelCfgCollection[subModelIdx],
subModelName,
taskType,
outputFeatureNames,
trainingData,
progressInfoSubscriber
);
model.AddMember(subModel);
}
else if (subModelCfgType == typeof(RVFLModelConfig))
{
RVFLModel subModel =
RVFLModel.Build(modelConfig.SubModelCfgCollection[subModelIdx],
subModelName,
taskType,
outputFeatureNames,
trainingData,
out _,
progressInfoSubscriber
);
model.AddMember(subModel);
}
else if (subModelCfgType == typeof(CompositeModelConfig))
{
CompositeModel subModel =
CompositeModel.Build(modelConfig.SubModelCfgCollection[subModelIdx],
subModelName,
taskType,
outputFeatureNames,
trainingData,
progressInfoSubscriber
);
model.AddMember(subModel);
}
}//subModelIdx
//Set model operationable
model.SetOperationable();
//Return built model
return model;
}
}//CompositeModel
}//Namespace