-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork.cs
More file actions
173 lines (156 loc) · 4.95 KB
/
NeuralNetwork.cs
File metadata and controls
173 lines (156 loc) · 4.95 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
using ConsoleTableExt;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SlothNet
{
class NeuralNetwork // Perceptron learning based NN
{
public List<NeuralLayer> Layers { get; set; }
public NeuralNetwork()
{
Layers = new List<NeuralLayer>();
}
public void AddLayer(NeuralLayer layer)
{
int dendrites = 1;
if (Layers.Count > 0)
{
dendrites = Layers.Last().Neurons.Count();
}
foreach(Neuron n in layer.Neurons)
{
for(int i = 0; i < dendrites; i++)
{
n.Dendrites.Add(new Dendrite());
}
}
Layers.Add(layer);
}
public void Build()
{
/*int i = 0;
foreach(NeuralLayer layer in Layers)
{
if(i>=Layers.Count - 1)
{
break;
}
NeuralLayer next = Layers[i + 1];
ConnectLayers(layer, next);
i++;
}*/
if (Layers.Any())
{
for(int x = 0; x < Layers.Count-1; x++)
{
NeuralLayer from = Layers[x];
NeuralLayer to = Layers[x + 1];
ConnectLayers(from, to);
}
}
}
public void ConnectLayers(NeuralLayer from, NeuralLayer to)
{
foreach(Neuron n in from.Neurons)
{
n.Dendrites = new List<Dendrite>();
n.Dendrites.Add(new Dendrite());
}
foreach(Neuron neuron in to.Neurons)
{
neuron.Dendrites = new List<Dendrite>();
foreach(Neuron n in from.Neurons)
{
neuron.Dendrites.Add(new Dendrite()
{
Input = n.Output,
Weight = to.Weight
});
}
}
}
public void Train(NeuralData Input, NeuralData Output, int iterations = 100, double lr = 0.1)
{
int epoch = 1;
while(epoch <= iterations)
{
NeuralLayer inputLayer = Layers[0];
List<double> outputs = new List<double>();
for(int x = 0; x < Input.Data.Length; x++)
{
for(int j = 0; j < Input.Data[x].Length; j++)
{
inputLayer.Neurons[j].Output.Value = Input.Data[x][j];
}
ComputeOutput();
outputs.Add(Layers.Last().Neurons.First().Output.Value);
}
double accuracy = 0;
int count = 0;
outputs.ForEach((x) =>
{
Console.WriteLine("ANSWER: {0}, GUESSED: {1}", Output.Data[count].First(), x);
if(x == Output.Data[count].First())
{
accuracy++;
}
count++;
});
OptimiseWeights(accuracy/count, count-accuracy);
Console.WriteLine("Epoch: {0}, Accuracy {1:0.000}%", epoch, accuracy/count*100);
epoch++;
}
}
public void ComputeOutput()
{
bool first = true;
foreach(NeuralLayer layer in Layers)
{
if (first)
{
first = false;
continue;
}
layer.Forward();
}
}
public void OptimiseWeights(double accuracy, double error)
{
float lr = 0.1f;
if (accuracy >= 0.95 && accuracy <= 1)
return;
if (accuracy > 1)
lr = -lr;
foreach(NeuralLayer Layer in Layers)
{
Layer.Optimise(lr, error, 1 / (1 + Math.Exp(-Layer.Weight)));
}
}
public void DisplayNetwork()
{
DataTable table = new DataTable();
table.Columns.Add("Input");
table.Columns.Add("InputWeight");
table.Columns.Add("Output");
foreach (Neuron neuron in Layers[0].Neurons)
{
DataRow row = table.NewRow();
row[0] = neuron;
row[1] = Layers.First().Weight;
table.Rows.Add(row);
}
foreach (Neuron neuron in Layers[1].Neurons)
{
DataRow row = table.NewRow();
row[2] = neuron.Output.Value;
table.Rows.Add(row);
}
ConsoleTableBuilder CTB = ConsoleTableBuilder.From(table);
CTB.ExportAndWrite();
}
}
}