-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResCompTask.cs
274 lines (252 loc) · 11.3 KB
/
ResCompTask.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
using EasyMLCore.Data;
using EasyMLCore.Extensions;
using EasyMLCore.MLP;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace EasyMLCore.TimeSeries
{
/// <summary>
/// Implements a reservoir computer's single task.
/// </summary>
[Serializable]
public class ResCompTask : SerializableObject, IComputableTaskSpecific
{
//Constants
public const string ContextPathID = "RCTask";
//Attribute properties
/// <summary>
/// Configuration of the reservoir computer task.
/// </summary>
public ResCompTaskConfig TaskCfg { get; }
/// <summary>
/// Inner MLP model.
/// </summary>
public MLPModelBase Model {get; private set;}
/// <inheritdoc/>
public List<string> OutputFeatureNames { get; }
//Constructors
/// <summary>
/// Creates an uninitialized instance.
/// </summary>
/// <param name="cfg">Configuration of the reservoir computer task.</param>
private ResCompTask(ResCompTaskConfig cfg)
{
OutputFeatureNames = cfg.OutputFeaturesCfg.GetFeatureNames();
TaskCfg = (ResCompTaskConfig)cfg.DeepClone();
Model = null;
return;
}
/// <summary>
/// Copy constructor.
/// </summary>
/// <param name="source">The source instance.</param>
public ResCompTask(ResCompTask source)
{
OutputFeatureNames = new List<string>(source.OutputFeatureNames);
TaskCfg = (ResCompTaskConfig)source.TaskCfg.DeepClone();
Model = source.Model?.DeepClone();
return;
}
//Properties
/// <inheritdoc cref="ResCompTaskConfig.Name"/>
public string Name { get { return TaskCfg.Name; } }
/// <inheritdoc/>
public OutputTaskType TaskType { get { return TaskCfg.TaskType; } }
/// <inheritdoc/>
public int NumOfOutputFeatures { get { return OutputFeatureNames.Count; } }
//Methods
/// <summary>
/// Builds inner model and gets the ResCompTask ready.
/// </summary>
/// <remarks>
/// Data of samples can be in any range. Method always does data standardization.
/// </remarks>
/// <param name="cfg">Configuration of the RC's task.</param>
/// <param name="trainingData">Training samples.</param>
/// <param name="progressInfoSubscriber">Subscriber will receive notification event about progress. (Parameter can be null).</param>
/// <returns>Confidence metrics of built model.</returns>
public static ResCompTask Build(ResCompTaskConfig cfg,
SampleDataset trainingData,
ProgressChangedHandler progressInfoSubscriber = null
)
{
if (!trainingData.IsUniform || trainingData.Count < 1)
{
throw new ArgumentException($"Invalid or insufficient data.", nameof(trainingData));
}
//Build
ResCompTask resCompTask = new ResCompTask(cfg);
Type modelCfgType = cfg.ModelCfg.GetType();
string modelNamePrefix = $"({resCompTask.Name}){ContextPathID}-";
MLPModelBase model = null;
if (modelCfgType == typeof(NetworkModelConfig))
{
model = NetworkModel.Build(cfg.ModelCfg,
modelNamePrefix,
resCompTask.TaskType,
resCompTask.OutputFeatureNames,
trainingData,
null,
progressInfoSubscriber
);
}
else if (modelCfgType == typeof(CrossValModelConfig))
{
model = CrossValModel.Build(cfg.ModelCfg,
modelNamePrefix,
resCompTask.TaskType,
resCompTask.OutputFeatureNames,
trainingData,
progressInfoSubscriber
);
}
else if (modelCfgType == typeof(StackingModelConfig))
{
model = StackingModel.Build(cfg.ModelCfg,
modelNamePrefix,
resCompTask.TaskType,
resCompTask.OutputFeatureNames,
trainingData,
progressInfoSubscriber
);
}
else if (modelCfgType == typeof(BHSModelConfig))
{
model = BHSModel.Build(cfg.ModelCfg,
modelNamePrefix,
resCompTask.TaskType,
resCompTask.OutputFeatureNames,
trainingData,
progressInfoSubscriber
);
}
else if (modelCfgType == typeof(RVFLModelConfig))
{
model = RVFLModel.Build(cfg.ModelCfg,
modelNamePrefix,
resCompTask.TaskType,
resCompTask.OutputFeatureNames,
trainingData,
out _,
progressInfoSubscriber
);
}
else if (modelCfgType == typeof(CompositeModelConfig))
{
model = CompositeModel.Build(cfg.ModelCfg,
modelNamePrefix,
resCompTask.TaskType,
resCompTask.OutputFeatureNames,
trainingData,
progressInfoSubscriber
);
}
else
{
throw new ApplicationException($"Unsupported model configuration {modelCfgType}.");
}
resCompTask.Model = model;
return resCompTask;
}
/// <inheritdoc/>
/// <remarks>
/// Both input and computed values are in the same ranges as were previously
/// submited into the Build method.
/// </remarks>
public double[] Compute(double[] input)
{
if (input == null)
{
throw new ArgumentNullException(nameof(input));
}
return Model.Compute(input);
}
/// <summary>
/// Computes an output.
/// </summary>
/// <param name="input">Input vector.</param>
/// <param name="detailedOutput">The appropriate instance of task specific detailed output.</param>
/// <returns>Computed output vector.</returns>
public double[] Compute(double[] input, out TaskOutputDetailBase detailedOutput)
{
detailedOutput = GetOutputDetail(Compute(input));
return detailedOutput.RawData;
}
/// <summary>
/// Performs RC's single task test.
/// </summary>
/// <remarks>
/// Data of samples can be in any range. Data standardization is always performed internally.
/// </remarks>
/// <param name="testingData">Testing samples.</param>
/// <param name="resultDataset">Result dataset containing original samples together with computed data.</param>
/// <param name="progressInfoSubscriber">Subscriber will receive notification event about progress. (Parameter can be null).</param>
/// <returns>Resulting error statistics.</returns>
public MLPModelErrStat Test(SampleDataset testingData,
out ResultDataset resultDataset,
ProgressChangedHandler progressInfoSubscriber = null
)
{
return Model.Test(testingData, out resultDataset, progressInfoSubscriber);
}
/// <summary>
/// Performs diagnostic test of the RC task's model and all its inner sub-models.
/// </summary>
/// <remarks>
/// Samples can be in any range. Data standardization is always performed internally.
/// </remarks>
/// <param name="testingData">Testing samples.</param>
/// <param name="progressInfoSubscriber">Subscriber will receive notification event about progress. (Parameter can be null).</param>
/// <returns>Resulting diagnostics data of the RC task's model and all its inner sub-models.</returns>
public MLPModelDiagnosticData DiagnosticTest(SampleDataset testingData,
ProgressChangedHandler progressInfoSubscriber = null
)
{
return Model.DiagnosticTest(testingData, progressInfoSubscriber);
}
/// <inheritdoc/>
public TaskOutputDetailBase GetOutputDetail(double[] outputData)
{
return TaskType switch
{
OutputTaskType.Regression => new RegressionOutputDetail(OutputFeatureNames, outputData),
OutputTaskType.Binary => new BinaryOutputDetail(OutputFeatureNames, outputData),
OutputTaskType.Categorical => new CategoricalOutputDetail(OutputFeatureNames, outputData),
_ => null,
};
}
/// <summary>
/// Gets an informative text about this reservoir computer's output task instance.
/// </summary>
/// <param name="detail">Specifies whether to include details about inner model.</param>
/// <param name="margin">Specifies left margin.</param>
/// <returns></returns>
public string GetInfoText(bool detail = false, int margin = 0)
{
StringBuilder sb = new StringBuilder($"Task ({Name}){Environment.NewLine}");
sb.Append($" Task type : {TaskType.ToString()}{Environment.NewLine}");
sb.Append($" Output features: {NumOfOutputFeatures.ToString(CultureInfo.InvariantCulture)}");
foreach (string outputFeatureName in OutputFeatureNames)
{
sb.Append($" [{outputFeatureName}]");
}
sb.Append(Environment.NewLine);
sb.Append(Model.GetInfoText(detail, 4));
string infoText = sb.ToString();
if( margin > 0)
{
infoText = infoText.Indent(margin);
}
return infoText;
}
/// <summary>
/// Creates a deep clone.
/// </summary>
public ResCompTask DeepClone()
{
return new ResCompTask(this);
}
}//ResCompTask
}//Namespace