This repository has been 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
/
Synapse.cs
385 lines (361 loc) · 15.1 KB
/
Synapse.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using RCNet.Extensions;
using RCNet.MathTools;
using RCNet.Neural.Activation;
using RCNet.Neural.Network.SM.Preprocessing.Neuron;
using RCNet.Queue;
using System;
namespace RCNet.Neural.Network.SM.Preprocessing.Reservoir.SynapseNS
{
/// <summary>
/// Implements the synapse.
/// </summary>
/// <remarks>
/// Supports the signal delaying and the short-term plasticity.
/// </remarks>
[Serializable]
public class Synapse
{
/// <summary>
/// The synaptic delay method.
/// </summary>
public enum SynapticDelayMethod
{
/// <summary>
/// The synaptic delay is decided randomly.
/// </summary>
Random,
/// <summary>
/// The synaptic delay depends on an Euclidean distance.
/// </summary>
Distance
}
/// <summary>
/// The synapse's role.
/// </summary>
public enum SynRole
{
/// <summary>
/// An input synapse.
/// </summary>
Input,
/// <summary>
/// An excitatory synapse.
/// </summary>
Excitatory,
/// <summary>
/// An inhibitory synapse.
/// </summary>
Inhibitory,
/// <summary>
/// An indifferent synapse.
/// </summary>
Indifferent
}
//Static attributes
/// <summary>
/// The number of the synapse roles.
/// </summary>
public static readonly int NumOfRoles = Enum.GetValues(typeof(SynRole)).Length;
//Attribute properties
/// <summary>
/// The presynaptic neuron.
/// </summary>
public INeuron PresynapticNeuron { get; }
/// <summary>
/// The postsynaptic neuron.
/// </summary>
public INeuron PostsynapticNeuron { get; }
/// <summary>
/// An Euclidean distance of presynaptic and postsynaptic neurons.
/// </summary>
public double Distance { get; }
/// <inheritdoc cref="SynRole"/>
public SynRole Role { get; }
/// <summary>
/// The weight of the synapse (the maximum achievable weight).
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// The signal delay (in computation cycles).
/// </summary>
public int Delay { get; private set; }
/// <inheritdoc cref="SynapticDelayMethod"/>
public SynapticDelayMethod DelayMethod { get; private set; }
/// <summary>
/// The synapse's efficacy statistics.
/// </summary>
public BasicStat EfficacyStat { get; }
//Attributes
private readonly NeuronOutputData _presynapticNeuronOutputData;
private readonly bool _analogPresynapticSignal;
private readonly IEfficacy _efficacyComputer;
private readonly int _maxDelay;
private SimpleQueue<Signal> _signalQueue;
//Constructor
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="presynapticNeuron">The presynaptic neuron.</param>
/// <param name="postsynapticNeuron">The postsynaptic neuron.</param>
/// <param name="role">The synapse's role.</param>
/// <param name="synapseCfg">The configuration of the synapse.</param>
/// <param name="rand">The random object to be used.</param>
public Synapse(INeuron presynapticNeuron,
INeuron postsynapticNeuron,
SynRole role,
SynapseSettings synapseCfg,
Random rand
)
{
//Neurons to be connected
PresynapticNeuron = presynapticNeuron;
PostsynapticNeuron = postsynapticNeuron;
//Synapse role
Role = role;
//Euclidean distance
Distance = EuclideanDistance.Compute(PresynapticNeuron.Location.ReservoirCoordinates, PostsynapticNeuron.Location.ReservoirCoordinates);
//The rest
_efficacyComputer = null;
if (PostsynapticNeuron.TypeOfActivation == ActivationType.Spiking)
{
//Spiking target
if (Role == SynRole.Input)
{
DelayMethod = synapseCfg.SpikingTargetCfg.InputSynCfg.DelayMethod;
_maxDelay = synapseCfg.SpikingTargetCfg.InputSynCfg.MaxDelay;
if (PresynapticNeuron.TypeOfActivation == ActivationType.Analog)
{
//Analog source
Weight = rand.NextDouble(synapseCfg.SpikingTargetCfg.InputSynCfg.AnalogSourceCfg.WeightCfg);
}
else
{
//Spiking source
Weight = rand.NextDouble(synapseCfg.SpikingTargetCfg.InputSynCfg.SpikingSourceCfg.WeightCfg);
_efficacyComputer = PlasticityCommon.GetEfficacyComputer(PresynapticNeuron,
synapseCfg.SpikingTargetCfg.InputSynCfg.SpikingSourceCfg.PlasticityCfg.DynamicsCfg
);
}
}
else if (Role == SynRole.Excitatory)
{
DelayMethod = synapseCfg.SpikingTargetCfg.ExcitatorySynCfg.DelayMethod;
_maxDelay = synapseCfg.SpikingTargetCfg.ExcitatorySynCfg.MaxDelay;
if (PresynapticNeuron.TypeOfActivation == ActivationType.Analog)
{
//Analog source
Weight = rand.NextDouble(synapseCfg.SpikingTargetCfg.ExcitatorySynCfg.AnalogSourceCfg.WeightCfg);
}
else
{
//Spiking source
Weight = rand.NextDouble(synapseCfg.SpikingTargetCfg.ExcitatorySynCfg.SpikingSourceCfg.WeightCfg);
}
_efficacyComputer = PlasticityCommon.GetEfficacyComputer(PresynapticNeuron,
synapseCfg.SpikingTargetCfg.ExcitatorySynCfg.SpikingSourceCfg.PlasticityCfg.DynamicsCfg
);
}
else if (Role == SynRole.Inhibitory)
{
DelayMethod = synapseCfg.SpikingTargetCfg.InhibitorySynCfg.DelayMethod;
_maxDelay = synapseCfg.SpikingTargetCfg.InhibitorySynCfg.MaxDelay;
if (PresynapticNeuron.TypeOfActivation == ActivationType.Analog)
{
//Analog source
Weight = -rand.NextDouble(synapseCfg.SpikingTargetCfg.InhibitorySynCfg.AnalogSourceCfg.WeightCfg);
}
else
{
//Spiking source
Weight = -rand.NextDouble(synapseCfg.SpikingTargetCfg.InhibitorySynCfg.SpikingSourceCfg.WeightCfg);
}
_efficacyComputer = PlasticityCommon.GetEfficacyComputer(PresynapticNeuron,
synapseCfg.SpikingTargetCfg.InhibitorySynCfg.SpikingSourceCfg.PlasticityCfg.DynamicsCfg
);
}
else
{
throw new ArgumentException($"Invalid synapse role {role}.", "role");
}
}
else
{
//Analog target
if (Role == SynRole.Input)
{
DelayMethod = synapseCfg.AnalogTargetCfg.InputSynCfg.DelayMethod;
_maxDelay = synapseCfg.AnalogTargetCfg.InputSynCfg.MaxDelay;
if (PresynapticNeuron.TypeOfActivation == ActivationType.Analog)
{
//Analog source
Weight = rand.NextDouble(synapseCfg.AnalogTargetCfg.InputSynCfg.AnalogSourceCfg.WeightCfg);
}
else
{
//Spiking source
Weight = rand.NextSign() * rand.NextDouble(synapseCfg.AnalogTargetCfg.InputSynCfg.SpikingSourceCfg.WeightCfg);
_efficacyComputer = PlasticityCommon.GetEfficacyComputer(PresynapticNeuron,
synapseCfg.AnalogTargetCfg.InputSynCfg.SpikingSourceCfg.PlasticityCfg.DynamicsCfg
);
}
}
else if (Role == SynRole.Indifferent)
{
DelayMethod = synapseCfg.AnalogTargetCfg.IndifferentSynCfg.DelayMethod;
_maxDelay = synapseCfg.AnalogTargetCfg.IndifferentSynCfg.MaxDelay;
if (PresynapticNeuron.TypeOfActivation == ActivationType.Analog)
{
//Analog source
Weight = rand.NextSign() * rand.NextDouble(synapseCfg.AnalogTargetCfg.IndifferentSynCfg.AnalogSourceCfg.WeightCfg);
}
else
{
//Spiking source
Weight = rand.NextSign() * rand.NextDouble(synapseCfg.AnalogTargetCfg.IndifferentSynCfg.SpikingSourceCfg.WeightCfg);
_efficacyComputer = PlasticityCommon.GetEfficacyComputer(PresynapticNeuron,
synapseCfg.AnalogTargetCfg.IndifferentSynCfg.SpikingSourceCfg.PlasticityCfg.DynamicsCfg
);
}
}
else
{
throw new ArgumentException($"Invalid synapse role {role}.", "role");
}
}
//Source neuron - output data and signal index
_presynapticNeuronOutputData = PresynapticNeuron.OutputData;
_analogPresynapticSignal = PostsynapticNeuron.TypeOfActivation == ActivationType.Analog;
//Efficacy statistics
EfficacyStat = new BasicStat(false);
Reset(true);
return;
}
//Methods
/// <summary>
/// Rescales the synapse weight.
/// </summary>
/// <param name="factor">The scale factor.</param>
public void Rescale(double factor)
{
Weight *= factor;
return;
}
/// <summary>
/// Resets the synapse.
/// </summary>
/// <param name="statistics">Specifies whether to reset also the efficacy statistics.</param>
public void Reset(bool statistics)
{
//Reset queue if it is instantiated
_signalQueue?.Reset();
_efficacyComputer?.Reset();
if (statistics)
{
EfficacyStat.Reset();
if (_efficacyComputer == null)
{
//Efficacy will be always 1
EfficacyStat.AddSample(1d);
}
}
return;
}
/// <summary>
/// Setups the signal delaying.
/// </summary>
/// <param name="distancesStat">The distance statistics to be used when the synaptic delaying depends on a distance.</param>
/// <param name="rand">The random object to be used when synaptic delaying is random.</param>
public void SetupDelay(BasicStat distancesStat, Random rand)
{
if (_maxDelay > 0)
{
//Set synapse signal delay
if (DelayMethod == SynapticDelayMethod.Distance)
{
double relDistance = (Distance - distancesStat.Min) / distancesStat.Span;
Delay = (int)Math.Round(_maxDelay * relDistance);
}
else
{
Delay = rand.Next(_maxDelay + 1);
}
if (Delay == 0)
{
//No queue will be used
_signalQueue = null;
}
else
{
//Delay queue
_signalQueue = new SimpleQueue<Signal>(Delay + 1);
}
}
return;
}
/// <summary>
/// Gets the signal to be delivered to postsynaptic neuron.
/// </summary>
/// <param name="collectStatistics">Specifies whether to update efficacy statistics.</param>
public double GetSignal(bool collectStatistics)
{
//Source neuron signal
double sourceNeuronSignal = _analogPresynapticSignal ? _presynapticNeuronOutputData._analogSignal : _presynapticNeuronOutputData._spikingSignal;
//Short-term plasticity
double efficacy = 1d;
if (_efficacyComputer != null && sourceNeuronSignal > 0)
{
//Compute synapse efficacy
efficacy = _efficacyComputer.Compute();
//Update statistics if necessary
if (collectStatistics)
{
EfficacyStat.AddSample(efficacy);
}
}
//Final signal to be delivered
double signalToTarget = sourceNeuronSignal * Weight * efficacy;
//Delayed signal?
if (_signalQueue == null)
{
//No delay
return signalToTarget;
}
else
{
//Signal to be delayed so use queue
//Enqueue
Signal sigObj = _signalQueue.GetElementAtEnqueuePosition();
if (sigObj != null)
{
sigObj._weightedSignal = signalToTarget;
}
else
{
sigObj = new Signal { _weightedSignal = signalToTarget };
}
_signalQueue.Enqueue(sigObj);
//Is there delayed signal to be delivered?
if (_signalQueue.Full)
{
//Queue is full, so synapse is ready to deliver delayed signal
sigObj = _signalQueue.Dequeue();
return sigObj._weightedSignal;
}
else
{
//No signal to be delivered, signal is still "on the road"
return 0d;
}
}
}
//Inner classes
[Serializable]
internal class Signal
{
/// <summary>
/// The weighted signal.
/// </summary>
public double _weightedSignal;
}//Signal
}//Synapse
}//Namespace