-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRVFLModel.cs
268 lines (252 loc) · 10.8 KB
/
RVFLModel.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
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 RVFL (Random vector functional link network) model.
/// </summary>
[Serializable]
public class RVFLModel : MLPModelBase
{
//Constants
/// <summary>
/// Short identifier in context path.
/// </summary>
public const string ContextPathID = "RVFL";
//Attribute properties
/// <summary>
/// Preprocessor.
/// </summary>
public RVFLPreprocessor Preprocessor { get; private set; }
/// <summary>
/// End MLP model.
/// </summary>
public MLPModelBase EndModel { get; private set; }
//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 RVFLModel(RVFLModelConfig modelConfig,
string name,
OutputTaskType taskType,
IEnumerable<string> outputFeatureNames
)
: base(modelConfig, name, taskType, outputFeatureNames)
{
Preprocessor = null;
EndModel = null;
return;
}
/// <summary>
/// Deep copy constructor.
/// </summary>
/// <param name="source">The source instance.</param>
public RVFLModel(RVFLModel source)
: base(source)
{
Preprocessor = source.Preprocessor.DeepClone();
EndModel = source.EndModel.DeepClone();
return;
}
//Methods
private void OnRVFLInitProgressChanged(ProgressInfoBase progressInfo)
{
progressInfo.ExtendContextPath(Name);
ModelBuildProgressInfo trainProgressInfo =
new ModelBuildProgressInfo(Name, progressInfo, null);
InvokeProgressChanged(trainProgressInfo);
return;
}
private void OnModelBuildProgressChanged(ProgressInfoBase progressInfo)
{
InvokeProgressChanged(progressInfo);
return;
}
/// <summary>
/// Sets the model operationable.
/// </summary>
private void SetOperationable(MLPModelBase endModel)
{
EndModel = endModel;
//Finalize model
FinalizeModel(EndModel.ConfidenceMetrics);
return;
}
/// <inheritdoc/>
public override double[] Compute(double[] input)
{
return EndModel.Compute(Preprocessor.Compute(input));
}
/// <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($" Preprocessor outputs : {Preprocessor.NumOfOutputFeatures.ToString(CultureInfo.InvariantCulture)}{Environment.NewLine}");
sb.Append($" End model{Environment.NewLine}");
sb.Append(EndModel.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);
diagData.SetFinalized();
return diagData;
}
/// <inheritdoc/>
public override MLPModelBase DeepClone()
{
return new RVFLModel(this);
}
//Static methods
/// <summary>
/// Builds a RVFLModel.
/// </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="preprocessorStat">RVFL preprocessor's statistics.</param>
/// <param name="progressInfoSubscriber">Subscriber will receive notification event about progress. (Parameter can be null).</param>
/// <returns>Built model.</returns>
public static RVFLModel Build(IModelConfig cfg,
string name,
OutputTaskType taskType,
List<string> outputFeatureNames,
SampleDataset trainingData,
out RVFLPreprocessorStat preprocessorStat,
ProgressChangedHandler progressInfoSubscriber = null
)
{
//Checks
if (cfg == null)
{
throw new ArgumentNullException(nameof(cfg));
}
if (cfg.GetType() != typeof(RVFLModelConfig))
{
throw new ArgumentException($"Wrong type of configuration. Expected {typeof(RVFLModelConfig)} but received {cfg.GetType()}.", nameof(cfg));
}
//Composite model
RVFLModelConfig modelConfig = (RVFLModelConfig)cfg;
RVFLModel model = new RVFLModel(modelConfig,
(name + RVFLModel.ContextPathID),
taskType,
outputFeatureNames
);
if(progressInfoSubscriber != null)
{
model.ProgressChanged += progressInfoSubscriber;
}
try
{
//Preprocessor
model.Preprocessor = new RVFLPreprocessor(trainingData.FirstInputVectorLength,
modelConfig);
SampleDataset rvflTrainingData =
model.Preprocessor.Init(trainingData,
new Random(GetRandomSeed()),
out preprocessorStat,
model.OnRVFLInitProgressChanged
);
//Build end model
MLPModelBase endModel = null;
string endModelName = $"{model.Name}.EndModel-";
Type endModelCfgType = modelConfig.EndModelCfg.GetType();
if (endModelCfgType == typeof(NetworkModelConfig))
{
endModel =
NetworkModel.Build(modelConfig.EndModelCfg,
endModelName,
taskType,
outputFeatureNames,
rvflTrainingData,
null,
model.OnModelBuildProgressChanged
);
}
else if (endModelCfgType == typeof(CrossValModelConfig))
{
endModel =
CrossValModel.Build(modelConfig.EndModelCfg,
endModelName,
taskType,
outputFeatureNames,
rvflTrainingData,
model.OnModelBuildProgressChanged
);
}
else if (endModelCfgType == typeof(StackingModelConfig))
{
endModel =
StackingModel.Build(modelConfig.EndModelCfg,
endModelName,
taskType,
outputFeatureNames,
rvflTrainingData,
model.OnModelBuildProgressChanged
);
}
else if (endModelCfgType == typeof(BHSModelConfig))
{
endModel =
BHSModel.Build(modelConfig.EndModelCfg,
endModelName,
taskType,
outputFeatureNames,
rvflTrainingData,
model.OnModelBuildProgressChanged
);
}
else if (endModelCfgType == typeof(CompositeModelConfig))
{
endModel =
CompositeModel.Build(modelConfig.EndModelCfg,
endModelName,
taskType,
outputFeatureNames,
rvflTrainingData,
model.OnModelBuildProgressChanged
);
}
//Set model operationable
model.SetOperationable(endModel);
//Return built model
return model;
}
finally
{
if(progressInfoSubscriber != null)
{
model.ProgressChanged -= progressInfoSubscriber;
}
}
}
}//RVFLModel
}//Namespace