1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | |
---|
6 | namespace BCMToolbox |
---|
7 | { |
---|
8 | public class SynchronousOjaNetwork : FixedTraceableNetwork |
---|
9 | { |
---|
10 | double[] __intermediaryOutputs = null; |
---|
11 | double __inertia = 0.005; |
---|
12 | public SynchronousOjaNetwork(int neuronCount, int newID) |
---|
13 | : base(neuronCount, newID) |
---|
14 | { |
---|
15 | __intermediaryOutputs = new double[neuronCount]; |
---|
16 | } |
---|
17 | |
---|
18 | |
---|
19 | public override string Identification |
---|
20 | { |
---|
21 | get |
---|
22 | { |
---|
23 | return String.Format("Oja Layer #{0}", this.UID); |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | public double Inertia |
---|
28 | { |
---|
29 | get |
---|
30 | { |
---|
31 | return __inertia; |
---|
32 | } |
---|
33 | set |
---|
34 | { |
---|
35 | __inertia = value; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | public override void ApplyInputs(double[] inputs) |
---|
42 | { |
---|
43 | lock (SyncRoot) |
---|
44 | { |
---|
45 | if (inputs == null) |
---|
46 | throw new ArgumentNullException(); |
---|
47 | |
---|
48 | if (inputs.Length != this.Count) |
---|
49 | throw new ArgumentException("Invalid input size. Expected: ", this.Count.ToString()); |
---|
50 | |
---|
51 | for (int i = 0; i < this.Count; i++) |
---|
52 | __potentials[i] += inputs[i]; |
---|
53 | } |
---|
54 | |
---|
55 | base.ApplyInputs(inputs); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | public override void Propagate() |
---|
60 | { |
---|
61 | // calculate outputs |
---|
62 | for (int i = 0; i < this.Count; i++) |
---|
63 | { |
---|
64 | __intermediaryOutputs[i] = 0.0; |
---|
65 | for (int j = 0; j < this.Count; j++) |
---|
66 | if ((i != j) && !double.IsNaN(__weights[j, i])) |
---|
67 | __intermediaryOutputs[i] += __weights[j, i] * __potentials[j]; |
---|
68 | } |
---|
69 | |
---|
70 | // update weights and lock outputs |
---|
71 | lock (SyncRoot) |
---|
72 | { |
---|
73 | for (int i = 0; i < this.Count; i++) |
---|
74 | for (int j = 0; j < this.Count; j++) |
---|
75 | { |
---|
76 | if ((i == j) || double.IsNaN(__weights[i, j])) |
---|
77 | continue; |
---|
78 | |
---|
79 | double delta = __intermediaryOutputs[j]; |
---|
80 | delta *= __inertia; |
---|
81 | delta *= __potentials[i] - __intermediaryOutputs[j] * __weights[i, j]; |
---|
82 | |
---|
83 | System.Diagnostics.Debug.Assert(!double.IsNaN(delta)); |
---|
84 | System.Diagnostics.Debug.Assert(!double.IsInfinity(delta)); |
---|
85 | |
---|
86 | __weights[i, j] += delta; |
---|
87 | } |
---|
88 | //NormalizeWeights(2.0); |
---|
89 | |
---|
90 | for (int i = 0; i < this.Count; i++) |
---|
91 | __potentials[i] = __intermediaryOutputs[i]; |
---|
92 | } |
---|
93 | |
---|
94 | base.Propagate(); |
---|
95 | } |
---|
96 | |
---|
97 | public override void Initialize(Func<int, int, double> initializerWeights, Func<int, double> initializerPotentials) |
---|
98 | { |
---|
99 | base.Initialize(initializerWeights, initializerPotentials); |
---|
100 | NormalizeWeights(2.0); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | } |
---|