-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sloi.cs
178 lines (174 loc) · 6.91 KB
/
Sloi.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
using System;
using System.Collections.Generic;
namespace NeuralClassLibrary
{
//Êëàññ Ñëîÿ
public class Sloi
{
//Äàííûå:
List<Perceptron> perceplist; //Ñïèñîê ïåðñåïòðîíîâ
//List<double> inputsignals;
List<double> outputsignals; //Ñïèñîê âûõîäíûõ ñèãíàëîâ
List<double> dSigmoidList; //Ñïèñîê äèôôåðåíöèàëîâ îò Ñèãìîéäà
List<double> delta; //Ñïèñîê äåëüòà**
//Ñâîéñòâà:
public int Size //Êîë-âî ïåðñåïòðîíîâ
{
set;
//{
// for (int i = 0; i < value; i++)
// perceplist.Add(new Perceptron());
//}
get;
}
//get { if( perceplist.Count == null)
// return; } }
public double Learning_rate //Ñêîðîñòü îáó÷åíèÿ
{
get { return perceplist[0].Learning_rate; }
set
{
foreach (Perceptron p in perceplist)
p.Learning_rate = value;
}
}
public List<double> Deltas //**
{
set { delta = value; }
get { return delta; }
}
public List<double> Outputsignals //Ñïèñîê âûõîäíûõ ñèãíàëîâ
{
set{outputsignals = value;}
get{return outputsignals;}
}
public List<List<double>> Weights //Ñïèñîê ñïèñêîâ âåñîâ
{
get
{
List<List<double>> weights = new List<List<double>>();
foreach (Perceptron p in perceplist)
weights.Add(p.Weights);
return weights;
}
}
public List<double> DSigmoidList //Ñïèñîê äèôôåðåíöèàëîâ îò Ñèãìîéäà
{
set{dSigmoidList = value;}
get{return dSigmoidList;}
}
//Ôóíêöèè:
public Sloi() //Êîíñòðóêòîð
{
perceplist = new List<Perceptron>(/*Size*/); //ñïèñîê ïåðñåïòðîíîâ
//inputsignals = new List<double>(); //
outputsignals = new List<double>(); //ñïèñîê âûõîäíûõ ñèãíàëîâ
dSigmoidList = new List<double>(); //ñïèñîê äèôôåðåíöèàëîâ îò Ñèãìîéäà
delta = new List<double>(); //îøèáêà
}
//Èíèöèëèçàöèÿ ñïèñêîâ
private void LoadLists()
{
if (perceplist.Count == 0) //åñëè ñïèñîê ïóñòîé
{
for (int i = 0; i < Size; i++)
perceplist.Add(new Perceptron()); //èíèöèëèçàöèÿ ñïèñêà
}
if(dSigmoidList.Count == 0) //åñëè ñïèñîê ïóñòîé
{
for (int i = 0; i < Size; i++)
dSigmoidList.Add(new double()); //èíèöèëèçàöèÿ ñïèñêà
}
if(outputsignals.Count ==0) //åñëè ñïèñîê ïóñòîé
{
for(int i=0;i<perceplist.Count;i++)
outputsignals.Add(new double()); //èíèöèëèçàöèÿ ñïèñêà
}
if (delta.Count == 0) //åñëè ñïèñîê ïóñòîé
{
for (int i = 0; i < perceplist.Count; i++)
delta.Add(new double()); //èíèöèëèçàöèÿ ñïèñêà
}
}
//Ïðÿìîå ðàñïðîñòðàíåíèå ñèãíàëîâ
public /*List<double>*/void Feedforward(List<double> inputs)
{
LoadLists();
for (int i = 0;i<perceplist.Count; i++)
{
perceplist[i].FeedForward(inputs);
outputsignals[i] = perceplist[i].FNet;
}
for (int i = 0; i < perceplist.Count; i++)
{
dSigmoidList[i] = perceplist[i].DSigmoid;
}
}
/// <summary>
/// Backpropagation of errors
/// </summary>
/// <param name="ErrorList">Cummilative error up to this layer starting from the end</param>
/// <param name="inputs">This is the list of activations of the previous layer</param>
// Îáðàòíîå ðàñïðîñòðàíåíèå îøèáîê
public void /*List<double>*/ BackProp(List<double> ErrorList, List<double> inputs)
{
//for (int i = 0; i < ErrorList.Count; i++)
//{
// dSigmoidList[i] = perceplist[i].Delta;
//}
double delta_weight, delta_bais;
for (int i = 0;i< perceplist.Count;i++) //Äëÿ êàæäîãî ïåðñåïòðîíà
{
delta[i] = (ErrorList[i] * dSigmoidList[i]); //âû÷èñëåíèå îøèáêè
delta_weight = delta [i]* inputs[i]; //âû÷èñëåíèå äåëüòó âåñà
delta_bais = delta[i]; //âû÷èñëåíèå äåëüòó ñìåùåíèÿ
perceplist[i].BackProp(delta_weight,delta_bais); //Îáðàòíîå ðàñïðîñòðàíåíèå äåëüòà
}
//return delta;
}
//Îáðàòíîå ðàñïðîñòðàíåíèå îøèáîê
public void BackProp(List<double> ErrorList, List<double> inputs,List<List<double>> nextweights)
{
for (int i = 0; i < perceplist.Count; i++)
dSigmoidList[i] = perceplist[i].DSigmoid;
for (int i = 0; i < /*perceplist*/nextweights.Count;i++)
{
double delta_value = 0;
for (int j = 0; j< /*perceplist.Countnextweights*/ErrorList.Count; j++)
{
delta_value += nextweights/*[i][j]*/[j][i] * ErrorList[j];
}
delta_value = delta_value * dSigmoidList[i];
delta.Add(delta_value);
perceplist[i].BackProp(delta[i], inputs);
}
/*for (int i = 0; i < ErrorList.Count; i++)
dSigmoidList[i] = perceplist[i].Delta;
//double delta_weight, delta_bais;
double delta_value=0;
for (int i = 0; i < perceplist.Count; i++)
{
List<double> listOfW = GetWeights(nextweights, i);
for (int j = 0; j < listOfW.Count/*nextweights[i].Count*//*; j++)
{
delta_value =+ ErrorList[i] * dSigmoidList[i] * listOfW[j];//nextweights[j][i];
}
//delta.Add(ErrorList[i] * dSigmoidList[i] * nextweights[i][j]); //needs work
delta.Add(delta_value);
//delta_weight = delta[i] * inputs[i];
//delta_bais = delta[i];
//perceplist[i].BackProp(delta_weight, delta_bais);
perceplist[i].BackProp(delta[i], inputs);
}*/
}
private List<double> GetWeights(List<List<double>> ListOfWeights, int index)
{
List<double> W = new List<double>();
foreach(List<double> l in ListOfWeights)
{
W.Add(l[index]);
}
return W;
}
}
}