/
Connections.cs
191 lines (137 loc) · 4.54 KB
/
Connections.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ANN
{
public class Connections : IAction
{
public Layer source, dest;
//Size of the matrix
protected int n1, n2;
//Weight matrix
public double[,] weights;
/// <summary>
/// Gets the number of the connection
/// </summary>
/// <param name="n1">Number of neurons of the input layer</param>
/// <param name="n2">Number of neurons of the output layer</param>
public Connections(int n1, int n2)
{
this.n1 = n1;
this.n2 = n2;
this.weights = new double[n2,n1];
this.source = null;
this.dest = null;
}
/// <summary>
/// Gets two layers and generate the all_to_all weigth matrix
/// </summary>
/// <param name="input">The source layer</param>
/// <param name="output">The otuput layer</param>
public Connections(Layer input, Layer output)
{
// Una neurona más por el bias
this.n1 = input.nNeurons+1;
this.n2 = output.nNeurons;
this.weights = new double[n2, n1];
this.source = input;
this.dest = output;
}
/// <summary>
/// Returns the number of neurons the input layer
/// </summary>
/// <returns></returns>
int IAction.getNInputNeurons()
{
return n1-1;
}
/// <summary>
/// Returns the number of neurons of the output layer
/// </summary>
/// <returns></returns>
int IAction.getNOutputNeurons()
{
return n2;
}
/// <summary>
/// Randomize the weights. Good for inizialiation.
/// </summary>
/// <param name="min_weight"></param>
/// <param name="max_weight"></param>
public void randomizeWeights(double min_weight, double max_weight)
{
Random rnd = new Random();
for (int i = 0; i < this.n1; i++)
{
for (int j = 0; j < this.n2; ++j)
{
this.weights[i,j] = min_weight + (rnd.NextDouble()/(max_weight-min_weight));
}
}
}
/// <summary>
/// Take a stream and read the data
/// </summary>
/// <param name="file">Corrected opened stream</param>
public void readWeightsfromStream(StreamReader file)
{
for (int i = 0; i < this.n2; i++)
{
for (int j = 0; j < this.n1; j++)
{
char aux;
string sWeight="";
do{
aux = (char)file.Read();
}while(!char.IsLetterOrDigit(aux) && aux != '-' && aux != '.');
sWeight += aux;
do{
aux = (char)file.Read();
sWeight += aux;
} while (char.IsLetterOrDigit(aux) || aux == '-' || aux == '.');
this.weights[i,j] = double.Parse(sWeight.Replace('.',','));
}
}
// while ((line = file.ReadLine()) != null)
//{
// if (line.StartsWith("#")) continue;
// int i = 0;
// foreach (string aux in line.Split())
// {
// v[i++] = Double.Parse(aux);
// };
}
public override string ToString()
{
string s;
s = "";
for (int i = 0; i < this.n1; i++)
{
for (int j = 0; j < this.n2; j++)
{
s += this.weights[i,j];
}
s += "\n";
}
return s;
}
/// <summary>
/// Forward action that computes the output values as input*weight
/// </summary>
void IAction.doFeedForward()
{
// output = input*m
for (int i = 0; i < this.n2; i++)
{
//Add bias
this.dest.weights[i] = this.weights[i,0];
for (int j = 1; j < this.n1; j++)
{
this.dest.weights[i] += this.source.weights[j-1] * this.weights[i,j];
}
}
}
}
}