-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReservoirHiddenLayerConfig.cs
More file actions
275 lines (248 loc) · 12.4 KB
/
ReservoirHiddenLayerConfig.cs
File metadata and controls
275 lines (248 loc) · 12.4 KB
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
using EasyMLCore.Activation;
using System;
using System.Globalization;
using System.Xml.Linq;
namespace EasyMLCore.TimeSeries
{
/// <summary>
/// Configuration of the reservoir's hidden layer.
/// </summary>
[Serializable]
public class ReservoirHiddenLayerConfig : ConfigBase
{
//Constants
/// <summary>
/// Name of an associated xsd type.
/// </summary>
public const string XsdTypeName = "ReservoirHiddenLayerConfig";
//Defult values
/// <summary>
/// Default value of the parameter specifying what proportion of all hidden neurons to connect to each hidden neuron.
/// </summary>
public const double DefaultDensity = 0.1d;
/// <summary>
/// Default value of the parameter specifying the maximum delay of data transfer through a hidden synapse.
/// </summary>
public const int DefaultMaxDelay = 0;
/// <summary>
/// Default value of the parameter specifying the activation function of hidden neurons.
/// </summary>
public const ActivationFnID DefaultActivationID = ActivationFnID.TanH;
/// <summary>
/// Default value of the parameter specifying the retainment of hidden neuron.
/// </summary>
public const double DefaultRetainment = 0d;
/// <summary>
/// Default value of spike event threshold. When the new activation is higher than the previous by this threshold, a spike event is emitted.
/// </summary>
public const double DefaultSpikeEventThreshold = 0.00125d;
/// <summary>
/// Default value of the parameter specifying the spectral radius of hidden weights matrix.
/// </summary>
public const double DefaultSpectralRadius = 0.999;
//Attributes
/// <summary>
/// Number of hidden neurons.
/// </summary>
public int NumOfNeurons { get; }
/// <summary>
/// If it is a positive fraction less than 1 then specifies what proportion
/// of all hidden neurons to connect to each hidden neuron.
/// If it is an integer value greater or equal to 1 then specifies exact number
/// of hidden neurons to connect to each hidden neuron.
/// </summary>
public double Density { get; }
/// <summary>
/// Specifies the maximum delay of data transfer through an hidden synapse (the degree of delay is evenly distributed across the synapses).
/// </summary>
public int MaxDelay { get; }
/// <summary>
/// Specifies the activation function of hidden neurons.
/// </summary>
public ActivationFnID ActivationID { get; }
/// <summary>
/// Specifies the retainment of hidden neuron.
/// </summary>
public double Retainment { get; }
/// <summary>
/// When the new activation is higher than the previous by this threshold, a spike event is emitted.
/// </summary>
public double SpikeEventThreshold { get; }
/// <summary>
/// Spectral radius of hidden weights matrix.
/// </summary>
public double SpectralRadius { get; }
//Constructors
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="numOfNeurons">Number of hidden neurons.</param>
/// <param name="density">If it is a positive fraction less than 1 then specifies what proportion of all hidden neurons to connect to each hidden neuron. If it is an integer value greater or equal to 1 then specifies exact number of hidden neurons to connect to each hidden neuron. Default is 1/10.</param>
/// <param name="maxDelay">Specifies the maximum delay of data transfer through a hidden synapse (the degree of delay is evenly distributed across the synapses). Default is 0 (no delay).</param>
/// <param name="retainment">Specifies the retainment of hidden neuron. Default is 0.</param>
/// <param name="spectralRadius">Spectral radius of hidden weights matrix. Default is 0.999.</param>
/// <param name="spikeEventThreshold">When the new activation is higher than the previous by this threshold, a spike event is emitted. Default is 0.00125.</param>
/// <param name="activationID">Specifies the activation function of hidden neurons. Default is TanH.</param>
public ReservoirHiddenLayerConfig(int numOfNeurons,
double density = DefaultDensity,
int maxDelay = DefaultMaxDelay,
double retainment = DefaultRetainment,
double spectralRadius = DefaultSpectralRadius,
double spikeEventThreshold = DefaultSpikeEventThreshold,
ActivationFnID activationID = DefaultActivationID
)
{
NumOfNeurons = numOfNeurons;
Density = density;
MaxDelay = maxDelay;
ActivationID = activationID;
Retainment = retainment;
SpikeEventThreshold = spikeEventThreshold;
SpectralRadius = spectralRadius;
Check();
return;
}
/// <summary>
/// The copy constructor.
/// </summary>
/// <param name="source">The source instance.</param>
public ReservoirHiddenLayerConfig(ReservoirHiddenLayerConfig source)
: this(source.NumOfNeurons, source.Density, source.MaxDelay,
source.Retainment, source.SpectralRadius,
source.SpikeEventThreshold, source.ActivationID)
{
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="elem">A xml element containing the configuration data.</param>
public ReservoirHiddenLayerConfig(XElement elem)
{
//Validation
XElement validatedElem = Validate(elem, XsdTypeName);
//Parsing
NumOfNeurons = int.Parse(validatedElem.Attribute("neurons").Value);
Density = double.Parse(validatedElem.Attribute("density").Value, CultureInfo.InvariantCulture);
MaxDelay = int.Parse(validatedElem.Attribute("maxDelay").Value);
ActivationID = ActivationFactory.ParseAFnID(validatedElem.Attribute("activation").Value);
Retainment = double.Parse(validatedElem.Attribute("retainment").Value, CultureInfo.InvariantCulture);
SpikeEventThreshold = double.Parse(validatedElem.Attribute("spikeEventThreshold").Value, CultureInfo.InvariantCulture);
SpectralRadius = double.Parse(validatedElem.Attribute("spectralRadius").Value, CultureInfo.InvariantCulture);
Check();
return;
}
//Properties
/// <summary>
/// Checks the defaults.
/// </summary>
public bool IsDefaultDensity { get { return Density == DefaultDensity; } }
/// <summary>
/// Checks the defaults.
/// </summary>
public bool IsDefaultMaxDelay { get { return MaxDelay == DefaultMaxDelay; } }
/// <summary>
/// Checks the defaults.
/// </summary>
public bool IsDefaultActivationID { get { return ActivationID == DefaultActivationID; } }
/// <summary>
/// Checks the defaults.
/// </summary>
public bool IsDefaultRetainment { get { return Retainment == DefaultRetainment; } }
/// <summary>
/// Checks the defaults.
/// </summary>
public bool IsDefaultSpikeEventThreshold { get { return SpikeEventThreshold == DefaultSpikeEventThreshold; } }
/// <summary>
/// Checks the defaults.
/// </summary>
public bool IsDefaultSpectralRadius { get { return SpectralRadius == DefaultSpectralRadius; } }
/// <inheritdoc/>
public override bool ContainsOnlyDefaults { get { return false; } }
//Methods
/// <inheritdoc/>
protected override void Check()
{
if (NumOfNeurons < 10)
{
throw new ArgumentException($"Invalid number of hidden neurons {NumOfNeurons.ToString(CultureInfo.InvariantCulture)}. Number of hidden neurons must be GE 10.", nameof(NumOfNeurons));
}
if (Density <= 0d)
{
throw new ArgumentException($"Invalid density {Density.ToString(CultureInfo.InvariantCulture)}. Density must be GT 0.", nameof(Density));
}
if (Density >= 1d && Math.Floor(Density) != Density)
{
throw new ArgumentException($"Invalid density {Density.ToString(CultureInfo.InvariantCulture)}. When GE 1 then it must be an integer value (not a fraction).", nameof(Density));
}
if (Density >= 1d && Density > NumOfNeurons)
{
throw new ArgumentException($"Invalid density {Density.ToString(CultureInfo.InvariantCulture)}. When GE 1 then it must be an integer value LE to number of neurons.", nameof(Density));
}
if (MaxDelay < 0)
{
throw new ArgumentException($"Invalid max delay {MaxDelay.ToString(CultureInfo.InvariantCulture)}. Max delay must be GE 0.", nameof(MaxDelay));
}
if (!ActivationFactory.IsSuitableForReservoirHiddenLayer(ActivationID))
{
throw new ArgumentException($"{ActivationID} activation function cannot be used in a reservoir's hidden layer.", nameof(ActivationID));
}
if (Retainment < 0d || Retainment >= 1d)
{
throw new ArgumentException($"Invalid retainment {Retainment.ToString(CultureInfo.InvariantCulture)}. Min retainment must be GE 0 and LT 1.", nameof(Retainment));
}
if (SpikeEventThreshold <= 0d || SpikeEventThreshold > 1d)
{
throw new ArgumentException($"Invalid spike event threshold {SpikeEventThreshold.ToString(CultureInfo.InvariantCulture)}. Spike event threshold must be GT 0 and LE 1.", nameof(SpikeEventThreshold));
}
if (SpectralRadius <= 0d)
{
throw new ArgumentException($"Invalid spectral radius {SpectralRadius.ToString(CultureInfo.InvariantCulture)}. Spectral radius must be GT 0.", nameof(SpectralRadius));
}
return;
}
/// <inheritdoc/>
public override ConfigBase DeepClone()
{
return new ReservoirHiddenLayerConfig(this);
}
/// <inheritdoc/>
public override XElement GetXml(string rootElemName, bool suppressDefaults)
{
XElement rootElem = new XElement(rootElemName,
new XAttribute("neurons", NumOfNeurons.ToString(CultureInfo.InvariantCulture))
);
if (!suppressDefaults || !IsDefaultDensity)
{
rootElem.Add(new XAttribute("density", Density.ToString(CultureInfo.InvariantCulture)));
}
if (!suppressDefaults || !IsDefaultMaxDelay)
{
rootElem.Add(new XAttribute("maxDelay", MaxDelay.ToString(CultureInfo.InvariantCulture)));
}
if (!suppressDefaults || !IsDefaultActivationID)
{
rootElem.Add(new XAttribute("activation", ActivationID.ToString()));
}
if (!suppressDefaults || !IsDefaultRetainment)
{
rootElem.Add(new XAttribute("retainment", Retainment.ToString(CultureInfo.InvariantCulture)));
}
if (!suppressDefaults || !IsDefaultSpikeEventThreshold)
{
rootElem.Add(new XAttribute("spikeEventThreshold", SpikeEventThreshold.ToString(CultureInfo.InvariantCulture)));
}
if (!suppressDefaults || !IsDefaultSpectralRadius)
{
rootElem.Add(new XAttribute("spectralRadius", SpectralRadius.ToString(CultureInfo.InvariantCulture)));
}
Validate(rootElem, XsdTypeName);
return rootElem;
}
/// <inheritdoc/>
public override XElement GetXml(bool suppressDefaults)
{
return GetXml("hiddenLayer", suppressDefaults);
}
}//ReservoirHiddenConfig
}//Namespace