-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
401 lines (275 loc) · 15.4 KB
/
Program.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace NeuralNetworks
{
//Dataset Order Sepal length Sepal width Petal length Petal width Species
class Program
{
const string path= @"../../../dataset/IrisDataset.txt";
static void Main(string[] args)
{
Random random = new Random();
const int InputNumber = 4;
const float alpha = 0.1f;
int epoch = 1000;
HashSet<IrisFlower> IrisFlowers = ConvertFileToIrisFlower(path);
// only one hidden layer
List<OutValueTita> OutHiddenLayer = Enumerable.Range(0, InputNumber). // new List<OutValueTita>(InputNumber)
Select(o => new OutValueTita { Value = 0, Tita = random.NextDouble() }).ToList();
List<WeightIrisFlower> WeightsInput = Enumerable.Range(0, InputNumber).//new List<WeightIrisFlower>(InputNumber).
Select(f => new WeightIrisFlower()
{
SepalLength = random.NextDouble(),
SepalWidth = random.NextDouble(),
PetalLength = random.NextDouble(),
PetalWidth = random.NextDouble(),
}).ToList();
// only one output
(double value, double tita) Output = (0, random.NextDouble());
List<double> WeightsOutput = Enumerable.Range(0, InputNumber) // new List<double>(InputNumber);
.Select(w => random.NextDouble()).ToList();
Print("Start BackPropagation");
BackPropagation(alpha, epoch, IrisFlowers, OutHiddenLayer, WeightsInput, Output, WeightsOutput);
Print("Start Elman");
Elman(alpha, epoch, IrisFlowers, OutHiddenLayer, WeightsInput, Output, WeightsOutput);
}
private static int BackPropagation(float alpha, int epoch, HashSet<IrisFlower> IrisFlowers, List<OutValueTita> OutHiddenLayer, List<WeightIrisFlower> WeightsInput, (double value, double tita) Output, List<double> WeightsOutput)
{
while (--epoch >= 0)
{
Print($"{nameof(epoch)} : {epoch}", ConsoleColor.Red);
foreach (var IrisFlower in IrisFlowers)
{
Print($"{nameof(IrisFlower)} Input data {IrisFlower.Id}: " +
$"{IrisFlower.PetalLength} , {IrisFlower.PetalWidth} , {IrisFlower.SepalLength} , {IrisFlower.SepalWidth}",
ConsoleColor.DarkYellow);
Print($"{nameof(IrisFlower)} Output data {IrisFlower.Id}: {IrisFlower.Iris}", ConsoleColor.DarkYellow);
Print("calcaulate hidden values");
// calcaulate hidden values
foreach (var hiddenZipWeight in OutHiddenLayer.Zip(WeightsInput, (h, w) => new { hidden = h, Weight = w }))
{
hiddenZipWeight.hidden.Value = Sigmoid(IrisFlower.SepalLength * hiddenZipWeight.Weight.SepalLength +
IrisFlower.SepalWidth * hiddenZipWeight.Weight.SepalWidth +
IrisFlower.PetalLength * hiddenZipWeight.Weight.PetalLength +
IrisFlower.PetalWidth * hiddenZipWeight.Weight.PetalWidth - hiddenZipWeight.hidden.Tita);
Print($"Hidden value layer: {hiddenZipWeight.hidden.Value}", ConsoleColor.Cyan);
}
Print("calcaulate output value");
// calcaulate output value
Output.value = Sigmoid(OutHiddenLayer.Zip(WeightsOutput, (h, w) => new { hidden = h, Weight = w }).Sum(x => x.hidden.Value * x.Weight) - Output.tita);
Print($"output value layer: {Output.value}", ConsoleColor.Cyan);
double error = IrisFlower.Iris - Output.value;
Print($"error : {error}", ConsoleColor.Red);
//start weight fix
double delteOutput = Output.value * (1 - Output.value) * error;
Print($"delteOutput : {delteOutput}", ConsoleColor.Red);
/// order soo important
Print($"fix hidden Weights");
// fix hidden Weights
for (int i = 0; i < WeightsInput.Count; i++)
{
double deltaInput = (OutHiddenLayer[i].Value * (1 - OutHiddenLayer[i].Value) * delteOutput * WeightsOutput[i]);
Print($"deltaInput : {delteOutput}", ConsoleColor.Red);
WeightsInput[i].SepalLength += (alpha * IrisFlower.SepalLength * deltaInput);
WeightsInput[i].SepalWidth += (alpha * IrisFlower.SepalWidth * deltaInput);
WeightsInput[i].PetalLength += (alpha * IrisFlower.PetalLength * deltaInput);
WeightsInput[i].PetalWidth += (alpha * IrisFlower.PetalWidth * deltaInput);
Print($"new Weights for input {i} : \n {WeightsInput[i].PetalLength} \n {WeightsInput[i].PetalWidth}" +
$"\n {WeightsInput[i].SepalLength} \n {WeightsInput[i].SepalWidth}", ConsoleColor.Green);
// fix hidden tita
OutHiddenLayer[i].Tita += -alpha * deltaInput;
Print($"new tita hidden for neuron {i} : {OutHiddenLayer[i].Tita}", ConsoleColor.Green);
}
// fix output tita
Output.tita += -alpha * delteOutput;
Print($"new tita output : {Output.tita}", ConsoleColor.Green);
// fix output Weights
for (int i = 0; i < WeightsOutput.Count; i++)
{
WeightsOutput[i] += (alpha * delteOutput * OutHiddenLayer[i].Value);
Print($"new Weights for hidden {i} : \n { WeightsOutput[i]}", ConsoleColor.Green);
}
Print($"Done fix Weights \n --------------------------------------------", ConsoleColor.Blue);
Print($"next data input");
}
Print($"Done round : {epoch}");
}
return epoch;
}
private static int Elman(float alpha, int epoch, HashSet<IrisFlower> IrisFlowers, List<OutValueTita> OutHiddenLayer, List<WeightIrisFlower> WeightsInput, (double value, double tita) Output, List<double> WeightsOutput)
{
Random random = new Random();
const int InputNumber = 4;
///For elman
// only one context layer
List<OutValueTita> OutcontextLayer = Enumerable.Range(0, InputNumber). // new List<OutValueTita>(InputNumber)
Select(o => new OutValueTita { Value = -1, Tita = random.NextDouble() }).ToList();
double WeightContext = random.NextDouble();
List<WeightIrisFlower> WeightsContext = Enumerable.Range(0, InputNumber).//new List<WeightIrisFlower>(InputNumber).
Select(f => new WeightIrisFlower()
{
SepalLength = WeightContext,
SepalWidth = WeightContext,
PetalLength = WeightContext,
PetalWidth = WeightContext,
}).ToList();
while (--epoch >= 0)
{
Print($"{nameof(epoch)} : {epoch}", ConsoleColor.Red);
foreach (var IrisFlower in IrisFlowers)
{
Print($"{nameof(IrisFlower)} Input data {IrisFlower.Id}: " +
$"{IrisFlower.PetalLength} , {IrisFlower.PetalWidth} , {IrisFlower.SepalLength} , {IrisFlower.SepalWidth}",
ConsoleColor.DarkYellow);
Print($"{nameof(IrisFlower)} Output data {IrisFlower.Id}: {IrisFlower.Iris}", ConsoleColor.DarkYellow);
Print("calcaulate hidden values");
// calcaulate hidden values
foreach (var hiddenZipWeight in OutHiddenLayer.Zip(OutcontextLayer,WeightsInput, WeightsContext,(oc,h, w,c) => new {contextvalue= oc, hidden = h, Weight = w , Context=c }))
{
double input = IrisFlower.SepalLength * hiddenZipWeight.Weight.SepalLength +
IrisFlower.SepalWidth * hiddenZipWeight.Weight.SepalWidth +
IrisFlower.PetalLength * hiddenZipWeight.Weight.PetalLength +
IrisFlower.PetalWidth * hiddenZipWeight.Weight.PetalWidth - hiddenZipWeight.hidden.Tita;
double context = OutcontextLayer[0].Value * hiddenZipWeight.Context.SepalLength +
OutcontextLayer[1].Value * hiddenZipWeight.Context.SepalWidth +
OutcontextLayer[2].Value * hiddenZipWeight.Context.PetalLength +
OutcontextLayer[3].Value * hiddenZipWeight.Context.PetalWidth - hiddenZipWeight.contextvalue.Tita;
hiddenZipWeight.hidden.Value = Sigmoid(input+context);
// copy value from hidden to context
hiddenZipWeight.contextvalue.Value = hiddenZipWeight.hidden.Value;
Print($"Hidden value layer: {hiddenZipWeight.hidden.Value}", ConsoleColor.Cyan);
}
Print("calcaulate output value");
// calcaulate output value
Output.value = Sigmoid(OutHiddenLayer.Zip(WeightsOutput,(h, w) => new { hidden = h, Weight = w }).Sum(x => x.hidden.Value * x.Weight) - Output.tita);
Print($"output value layer: {Output.value}", ConsoleColor.Cyan);
double error = IrisFlower.Iris - Output.value;
Print($"error : {error}", ConsoleColor.Red);
//start weight fix
double delteOutput = Output.value * (1 - Output.value) * error;
Print($"delteOutput : {delteOutput}", ConsoleColor.Red);
/// order soo important
Print($"fix hidden Weights");
// fix hidden Weights
for (int i = 0; i < WeightsInput.Count; i++)
{
double deltaInput = (OutHiddenLayer[i].Value * (1 - OutHiddenLayer[i].Value) * delteOutput * WeightsOutput[i]);
Print($"deltaInput : {delteOutput}", ConsoleColor.Red);
WeightsInput[i].PetalLength += (alpha * IrisFlower.PetalLength * deltaInput);
WeightsInput[i].PetalWidth += (alpha * IrisFlower.PetalWidth * deltaInput);
WeightsInput[i].SepalLength += (alpha * IrisFlower.SepalLength * deltaInput);
WeightsInput[i].SepalWidth += (alpha * IrisFlower.SepalWidth * deltaInput);
Print($"new Weights for input {i} : \n {WeightsInput[i].PetalLength} \n {WeightsInput[i].PetalWidth}" +
$"\n {WeightsInput[i].SepalLength} \n {WeightsInput[i].SepalWidth}", ConsoleColor.Green);
// fix hidden tita
OutHiddenLayer[i].Tita += -alpha * deltaInput;
Print($"new tita hidden for neuron {i} : {OutHiddenLayer[i].Tita}", ConsoleColor.Green);
}
// fix output tita
Output.tita += -alpha * delteOutput;
Print($"new tita output : {Output.tita}", ConsoleColor.Green);
// fix output Weights
for (int i = 0; i < WeightsOutput.Count; i++)
{
WeightsOutput[i] += (alpha * delteOutput * OutHiddenLayer[i].Value);
Print($"new Weights for hidden {i} : \n { WeightsOutput[i]}", ConsoleColor.Green);
}
Print($"Done fix Weights \n --------------------------------------------", ConsoleColor.Blue);
Print($"next data input");
}
Print($"Done round : {epoch}");
}
return epoch;
}
public static HashSet<IrisFlower> ConvertFileToIrisFlower(string fileName)
{
List<string> FlowerName = new List<string>();
string[] DataSetLine = File.ReadAllLines(fileName);
HashSet<IrisFlower> IrisFlowers = new HashSet<IrisFlower>(DataSetLine.Length + 1);
foreach (var item in DataSetLine)
{
var SplitOne = item.Split(" ").Select(s => s.Trim()).ToArray();
int Output = FlowerName.FindIndex(x => x == SplitOne[5]);
if (Output == -1)
{
FlowerName.Add(SplitOne[5]);
Output = FlowerName.Count;
}
else
{
Output = Output + 1;
}
IrisFlowers.Add(new IrisFlower()
{
Id = Convert.ToInt32(SplitOne[0]),
SepalLength = Convert.ToDouble(SplitOne[1]),
SepalWidth = Convert.ToDouble(SplitOne[2]),
PetalLength = Convert.ToDouble(SplitOne[3]),
PetalWidth = Convert.ToDouble(SplitOne[4]),
Iris = Output
});
}
return IrisFlowers;
}
public static double Sigmoid(double x, int multi=3)
{
return (1 / (1 + Math.Exp(-x)));
}
public static double Tansig(double x)
{
return Math.Exp(-x) /Math.Pow((1 + Math.Exp(-x)),2);
}
public static double Purlin(double x)
{
return x==0?0:(x>1?1:-1);
}
public static void Print(string text, ConsoleColor color = ConsoleColor.White)
{
Console.ForegroundColor = color;
Console.WriteLine(text);
Console.ResetColor();
}
}
public class IrisFlower
{
public int Id { get; set; }
public double SepalLength { get; set; } // input
public double SepalWidth { get; set; } // input
public double PetalLength { get; set; } // input
public double PetalWidth { get; set; } // input
public int Iris { get; set; } // output
}
public class WeightIrisFlower
{
public double SepalLength { get; set; }
public double SepalWidth { get; set; }
public double PetalLength { get; set; }
public double PetalWidth { get; set; }
}
public class OutValueTita
{
public double Value { get; set; }
public double Tita { get; set; }
}
public static class ExtensionsZip
{
public static IEnumerable<TResult> Zip<T1, T2, T3, T4, TResult>(
this IEnumerable<T1> source,
IEnumerable<T2> second,
IEnumerable<T3> third,
IEnumerable<T4> forth,
Func<T1, T2, T3,T4, TResult> func)
{
using (var e1 = source.GetEnumerator())
using (var e2 = second.GetEnumerator())
using (var e3 = third.GetEnumerator())
using (var e4 = forth.GetEnumerator())
{
while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext() && e4.MoveNext())
yield return func(e1.Current, e2.Current, e3.Current,e4.Current);
}
}
}
}