/
Network.cs
86 lines (72 loc) · 2.12 KB
/
Network.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NN
{
public class Network
{
private List<Layer> layers = new List<Layer>();
public void Setup(NetFileData data)
{
Neuron.Temperature = data.Temperature;
Neuron.Threshold = data.Threshold;
for (int i = 0; i < data.LayerCount; i++)
{
layers.Add(new Layer(i, data));
}
SetWeights(data);
}
public void ApplyVector(byte [] vector)
{
for(int nindex = 0; nindex < layers[0].Neurons.Count; nindex++)
{
double value;
byte mask;
if (!(layers[0].Neurons[nindex] is BiasNode))
{
for (int i = 0; i < vector.Length; i++)
{
mask = 0x80;
for (int j = 0; j < 8; j++) // cycle through bit positions
{
if ((vector[nindex] & mask) != 0)
{
value = 1.0;
}
else
{
value = 0.0;
}
layers[0].Neurons[nindex].Output = value;
nindex++;
}
}
}
}
}
public double GetOutputValue(int index)
{
return layers[layers.Count - 1].Neurons[index].Output;
}
public void RunNetwork()
{
for (int i = 1; i < layers.Count; i++)
{
layers[i].Calculate();
}
}
private void SetWeights(NetFileData data)
{
for (int i = 0; i < layers.Count - 1; i++)
{
layers[i + 1].SetWeights(layers[i].Neurons, data);
}
}
public bool Alive
{
get;
private set;
}
}
}