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 pathReadoutUnit.cs
246 lines (219 loc) · 8.77 KB
/
ReadoutUnit.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
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 readout unit of the readout layer.
/// </summary>
[Serializable]
public class ReadoutUnit
{
//Enums
/// <summary>
/// The type of task.
/// </summary>
public enum TaskType
{
/// <summary>
/// The forecast task.
/// </summary>
Forecast,
/// <summary>
/// The classification task.
/// </summary>
Classification
}
/// <summary>
/// This informative event occurs each time the progress of the build process takes a step forward.
/// </summary>
[field: NonSerialized]
public event ReadoutUnitBuildProgressChangedHandler ReadoutUnitBuildProgressChanged;
/// <summary>
/// The delegate of the ReadoutUnitBuildProgressChanged event handler.
/// </summary>
/// <param name="buildProgress">The current state of the build process.</param>
public delegate void ReadoutUnitBuildProgressChangedHandler(BuildProgress buildProgress);
//Attribute properties
/// <summary>
/// An index of this readout unit within the readout layer.
/// </summary>
public int Index { get; }
/// <summary>
/// The name of this readout unit.
/// </summary>
public string Name { get; }
/// <inheritdoc cref="TaskType"/>
public TaskType Task;
//Attributes
private readonly ITNRNetClusterChainSettings _clusterChainCfg;
private TNRNetClusterChain _clusterChain;
//Constructors
/// <summary>
/// Creates an itialized instance.
/// </summary>
/// <param name="index">An index of this readout unit within the readout layer.</param>
/// <param name="readoutUnitCfg">The configuration of the readout unit.</param>
/// <param name="taskDefaultsCfg">The tasks defaults configuration.</param>
public ReadoutUnit(int index,
ReadoutUnitSettings readoutUnitCfg,
TaskDefaultsSettings taskDefaultsCfg
)
{
Index = index;
Name = readoutUnitCfg.Name;
Task = readoutUnitCfg.TaskCfg.Type;
if (readoutUnitCfg.TaskCfg.GetType() == typeof(ForecastTaskSettings))
{
_clusterChainCfg = (ITNRNetClusterChainSettings)((ForecastTaskSettings)readoutUnitCfg.TaskCfg).ClusterChainCfg?.DeepClone();
if (_clusterChainCfg == null)
{
_clusterChainCfg = taskDefaultsCfg.ForecastClusterChainCfg;
}
}
else
{
_clusterChainCfg = (ITNRNetClusterChainSettings)((ClassificationTaskSettings)readoutUnitCfg.TaskCfg).ClusterChainCfg?.DeepClone();
{
if (_clusterChainCfg == null)
{
_clusterChainCfg = taskDefaultsCfg.ClassificationClusterChainCfg;
}
}
}
_clusterChain = null;
return;
}
//Properties
/// <summary>
/// Indicates the readiness to operate.
/// </summary>
public bool Ready { get { return _clusterChain != null; } }
//Methods
private void OnChainBuildProgressChanged(TNRNetClusterChainBuilder.BuildProgress chainBuildProgress)
{
//Prepare readout unit version
BuildProgress buildProgress = new BuildProgress(Name, chainBuildProgress);
//Raise event
ReadoutUnitBuildProgressChanged?.Invoke(buildProgress);
return;
}
/// <summary>
/// Builds the inner cluster chain.
/// </summary>
/// <param name="dataBundle">The data to be used for training.</param>
/// <param name="filter">The feature filter to be used to denormalize output.</param>
/// <param name="rand">The random object to be used (optional).</param>
/// <param name="controller">The build process controller (optional).</param>
public void Build(VectorBundle dataBundle,
FeatureFilterBase filter,
Random rand = null,
TNRNetBuilder.BuildControllerDelegate controller = null
)
{
rand = rand ?? new Random(0);
TNRNetClusterChainBuilder builder = new TNRNetClusterChainBuilder(Name,
_clusterChainCfg,
rand,
controller
);
builder.ChainBuildProgressChanged += OnChainBuildProgressChanged;
_clusterChain = builder.Build(dataBundle, new FeatureFilterBase[] { filter });
return;
}
/// <summary>
/// Computes the readout unit.
/// </summary>
/// <param name="predictors">The predictors.</param>
/// <returns>The readout unit's composite result.</returns>
public CompositeResult Compute(double[] predictors)
{
if (!Ready)
{
throw new InvalidOperationException($"Readout unit {Name} is not ready to operate. Call the Build method first.");
}
double[] result = _clusterChain.Compute(predictors, out List<Tuple<int, double[]>> mainClusterSubResults);
return new CompositeResult(result, NonRecurrentNetUtils.ExtractVectors(mainClusterSubResults));
}
/// <summary>
/// Gets the clone of error statistics of the readout unit.
/// </summary>
public TNRNetCluster.ClusterErrStatistics GetErrStat()
{
return _clusterChain.MainCluster.ErrorStats.DeepClone();
}
//Inner classes
/// <summary>
/// Implements the holder of the readout unit build progress information.
/// </summary>
public class BuildProgress : IBuildProgress
{
//Attribute properties
/// <summary>
/// Name of the readout unit.
/// </summary>
public string UnitName { get; }
/// <summary>
/// Information about the cluster chain build progress.
/// </summary>
public TNRNetClusterChainBuilder.BuildProgress ChainBuildProgress { get; }
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="unitName">Name of the readout unit.</param>
/// <param name="chainBuildProgress">The holder of the cluster chain build progress information.</param>
public BuildProgress(string unitName,
TNRNetClusterChainBuilder.BuildProgress chainBuildProgress
)
{
UnitName = unitName;
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(UnitName);
progressText.Append("] ");
}
progressText.Append(ChainBuildProgress.GetInfoText(0, false));
return progressText.ToString();
}
}//BuildProgress
}//ReadoutUnit
}//Namespace