/
Training.cs
159 lines (122 loc) · 5.65 KB
/
Training.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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ANN
{
/// <summary>
/// La seguente classe è corretta, ma non ottimizzata.
/// </summary>
class RPropPlus
{
const double c2 = 1.2, c1 = 0.5;
double Error = 0;
double[] desired_output;
double[][] dterr;
//Calcola la variazione dei pesi sinaptici da applicare a ciascun collegamento sinaptico.
//Per ogni neurone interno sommo gli errori delle SUE SINAPSI e basta.
private double Delta(FFANN ffann, int j, int k, int q)
{
double delta = 0;
if (j == ffann.NumLayers - 2)
delta = (-desired_output[q] + ffann.layer[j + 1].perceptron[q].getAction()) * (ffann.layer[j+1].perceptron[q].getAction()*(1 - ffann.layer[j+1].perceptron[q].getAction()));
else
delta = dterr[j+1][q]*(ffann.layer[j+1].perceptron[q].getAction()*(1 - ffann.layer[j+1].perceptron[q].getAction()));
dterr[j][k] += delta * ffann.layer[j].perceptron[k].getSynapsys(q);
return (c1 * delta * ffann.layer[j].perceptron[k].getAction());
}
//Costruttore, si occupa dell'allenamento vero e proprio.
public RPropPlus(FFANN ffann, double[][] stdDataset)
{
int epochs = 0;
double[][][] delta = new double[ffann.NumLayers-1][][]; //Matrice delta che conterrà le correzione dei pesi da applicare alla fine della propagazione.
desired_output = new double[ffann.NumPercept[ffann.NumLayers - 1]];
dterr = new double[ffann.NumLayers - 1][];
//inizializzazione di delta[][][].
for (int i = 0; i < ffann.NumLayers-1; i++)
{
delta[i] = new double[ffann.NumPercept[i]][];
}
for (int j = 0; j < delta.Length; j++)
{
for (int i = 0; i < delta[j].Length; i++) //Calcolo il numero totale di sinapsi nella rete.
{
delta[j][i] = new double[ffann.NumPercept[j + 1]];
}
}
//Fine inizializzazione delta[][][].
//Inizializzo ed azzero la matrice dterr.
for (int i = 0; i < ffann.NumLayers-1; i++)
{
dterr[i] = new double[ffann.NumPercept[i]];
for(int z = 0; z < ffann.NumPercept[i]; z++)
{
dterr[i][z] = 0;
}
}
//Fine azzeramento.
do //Scorro le epoche in cui la rete si allenerà.
{
for (int i = 0; i < stdDataset.Length; i++) //Scorro tutti i samples nel dataset.
{
Error = 0;
ffann.Predict(stdDataset[i]); //Calcolo le uscite della rete dato il sample i.
//Copio i valori desiderati in un vettore comodo e calcolo l'Errore Quadratico Medio.
for (int j = 0; j < ffann.NumPercept[ffann.NumLayers - 1]; j++)
{
desired_output[j] = stdDataset[i][ffann.NumPercept[0] + j];
Error += 0.5 * Math.Pow(ffann.layer[ffann.NumLayers - 1].perceptron[j].getAction() - desired_output[j], 2);
}
//End
//Calcolo i delta dei pesi sinaptici.
for (int j = ffann.NumLayers - 2; j >= 0; j--) //Scorro la rete dal penultimo layer al primo, per avere accesso diretto ai pesi sinaptici da correggere.
for (int k = 0; k < ffann.NumPercept[j]; k++) // Scorro i percettroni dello strato j.
for (int q = 0; q < ffann.NumPercept[j + 1]; q++) // Scorro le sinapsi.
delta[j][k][q] = Delta(ffann, j, k, q);
//Setto i nuovi pesi applicando i delta dei pesi sinaptici.
for (int j = 0; j < ffann.NumLayers - 1; j++)
for (int k = 0; k < ffann.NumPercept[j]; k++)
for (int q = 0; q < ffann.NumPercept[j + 1]; q++)
ffann.layer[j].perceptron[k].setSynapsys(ffann.layer[j].perceptron[k].getSynapsys(q) + delta[j][k][q], q);
//è necessario azzerare dterr per ogni sample analizzato.
for (int j = 0; j < dterr.Length; j++)
for (int z = 0; z < dterr[j].Length; z++)
dterr[j][z] = 0;
//Fine azzeramento.
}
//Stampa l'errore quadratico medio se if è true.
if(epochs % 1000 == 0)
Console.WriteLine("\nErrore Quadratico Medio: " + Error + "\n");
epochs++;
if (Error <= 0.00001) Console.Write("\nEpoche: " + epochs + "\n");
} while ((epochs != 100000)&&(Error >= 0.00001));
}
}
class Genetic
{
double Fitness;
public Genetic(FFANN ffann, double[][] stdDataset)
{
Random Rand = new Random();
Fitness = Rand.NextDouble();
}
private void Cross(FFANN ffann)
{
}
private void Mutate(FFANN ffann)
{
}
private void Reproduce()
{
}
}
class Particle_swarm_optimization
{
double Cohesion, Separation;
public Particle_swarm_optimization(FFANN ffann, double[][] stdDataset)
{
Random Rand = new Random();
Cohesion = Rand.NextDouble();
Separation = Rand.NextDouble();
}
}
}