/
MainForm.cs
292 lines (273 loc) · 10.5 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using GraphLib;
using NeuralNetwork.src;
using System.Diagnostics;
namespace NeuralNetwork
{
public partial class MainForm : Form
{
private NNetwork network;
private GoogleParser parser = new GoogleParser();
private DataNormalizer normalizer;
private NetworkTrainer trainer;
private double[] data;
private double[] train_data;
private double[] test_data;
private bool is_hyperbolic;
private bool is_sigmoid;
public MainForm()
{
InitializeComponent();
}
private String RenderXLabel(DataSource s, int idx)
{
if (s.AutoScaleX)
{
int Value = (int)(s.Samples[idx].x);
return "" + Value;
}
else
{
int Value = (int)(s.Samples[idx].x / 200);
String Label = "" + Value + "\"";
return Label;
}
}
private String RenderYLabel(DataSource s, float value)
{
return String.Format("{0:0.0}", value);
}
private void buttonCreateNetwork_Click(object sender, EventArgs e)
{
String[] layers_string = textLayers.Text.Split(";".ToCharArray());
int[] layers = new int[layers_string.Length];
for (int i = 0; i < layers_string.Length; i++)
{
layers[i] = int.Parse(layers_string[i]);
}
if (radioHyperbolic.Checked)
{
network = NNetwork.HyperbolicNetwork(layers);
is_hyperbolic = true;
is_sigmoid = false;
}
if (radioSigmoid.Checked)
{
network = NNetwork.SigmoidNetwork(layers);
is_hyperbolic = false;
is_sigmoid = true;
}
if (radioCombined.Checked)
{
network = NNetwork.CombinedNetwork(layers);
is_hyperbolic = false;
is_sigmoid = true;
}
bool two_steps = network.OutputCount() >= 2;
bool three_steps = network.OutputCount() >= 3;
checkTrain2.Enabled = two_steps;
checkTest2.Enabled = two_steps;
checkTrain3.Enabled = three_steps;
checkTest3.Enabled = three_steps;
groupWeights.Enabled = true;
}
private void buttonRandomize_Click(object sender, EventArgs e)
{
int seed = int.Parse(textSeed.Text);
network.RandomizeWeights(seed);
groupData.Enabled = true;
}
private void buttonLoadData_Click(object sender, EventArgs e)
{
openDataFile.ShowDialog();
if (radioRelative.Checked)
{
data = parser.GetDeltas(
parser.ParseFile(openDataFile.FileName)
);
}
else
{
data = parser.ParseFile(openDataFile.FileName);
}
normalizer = is_sigmoid ? DataNormalizer.SigmoidNormalizer(data) : DataNormalizer.HyperbolicNormalizer(data);
data = normalizer.GetValues();
groupTraining.Enabled = true;
}
private void buttonTrain_Click(object sender, EventArgs e)
{
groupPlotting.Enabled = false;
trainer = new NetworkTrainer(network);
double lambda = double.Parse(textLambda.Text);
double alpha = double.Parse(textAlpha.Text);
AssignData();
double error = 1;
double delta = 1;
double required_error = double.Parse(textErrorStop.Text);
double required_delta = double.Parse(textDeltaStop.Text);
int time_limit = int.Parse(textStopTime.Text)*1000;
int j = 0;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Reset();
stopwatch.Start();
// while(error > required_error && (delta >= required_delta) || j == 1)
while (error > required_error && (delta >= 0) || j == 1)
{
trainer.TrainPrediction(train_data, lambda, alpha);
double new_error = Math.Abs(trainer.GetError());
delta = error - new_error;
error = new_error;
j++;
if (stopwatch.ElapsedMilliseconds > time_limit) break;
}
textError.Text = Math.Round(error, 5).ToString();
Double control_error = GetControlError(trainer, lambda, alpha);
textControlError.Text = Math.Round(control_error, 5).ToString();
textTimes.Text = j.ToString();
groupPlotting.Enabled = true;
}
private double GetControlError(NetworkTrainer networkTrainer, double lambda, double alpha)
{
networkTrainer.IsLearning = false;
networkTrainer.TrainPrediction(test_data, lambda, alpha);
double error = Math.Abs(trainer.GetError());
networkTrainer.IsLearning = true;
return error;
}
private void buttonPlot_Click(object sender, EventArgs e)
{
DataSource prediction_source = PredictionSource(1);
DataSource initial_source = InitialSource();
DataSource test_source = TestSource(1);
display.DataSources.Clear();
int y_resolution = int.Parse(textYResolution.Text);
display.SetDisplayRangeX(0, y_resolution);
display.PanelLayout = PlotterGraphPaneEx.LayoutMode.NORMAL;
if (checkNetwork.Checked) AddDataSource(initial_source, Color.Crimson);
if (checkTrain.Checked) AddDataSource(prediction_source, Color.RoyalBlue);
if (checkTest.Checked) AddDataSource(test_source, Color.SeaGreen);
if (network.OutputCount() >= 2)
{
if (checkTrain2.Checked) AddDataSource(PredictionSource(2), Color.Blue);
if (checkTest2.Checked) AddDataSource(TestSource(2), Color.SeaGreen);
}
if (network.OutputCount() >= 3)
{
if (checkTrain3.Checked) AddDataSource(PredictionSource(3), Color.Cyan);
if (checkTest3.Checked) AddDataSource(TestSource(3), Color.SeaGreen);
}
display.Refresh();
display.PerformAutoScale();
}
private void AddDataSource(DataSource initial_source, Color color)
{
float min_y = 0;
float max_y = 0;
if (is_sigmoid)
{
min_y = 0;
max_y = 1;
}
if (is_hyperbolic)
{
min_y = -1;
max_y = 1;
}
display.DataSources.Add(initial_source);
initial_source.AutoScaleY = false;
initial_source.SetGridDistanceY(100);
initial_source.OnRenderYAxisLabel = RenderYLabel;
initial_source.GraphColor = color;
// initial_source.AutoScaleY = true;
initial_source.SetDisplayRangeY(min_y, max_y);
initial_source.XAutoScaleOffset = 0;
}
private DataSource PredictionSource(int step)
{
double[] prediction = trainer.GetPrediction(train_data, step);
DataSource prediction_source = new DataSource();
prediction_source.Length = data.Length;
float acc = 0;
for (int i = 0; i < data.Length; i++)
{
if (i < network.InputCount() + step - 1)
{
// prediction_source.Samples[i].x = i;
// prediction_source.Samples[i].y = 0.5f;
prediction_source.Samples[i].x = 0;
prediction_source.Samples[i].y = 0;
}
else if (i < network.InputCount() + prediction.Length + step - 1)
{
prediction_source.Samples[i].x = i;
prediction_source.Samples[i].y = (float) prediction[i - network.InputCount() - step + 1];
// acc += (float) normalizer.Denormalize(prediction[i - network.InputCount()]);
// prediction_source.Samples[i].y = acc;
}
else
{
prediction_source.Samples[i].x = 0;
prediction_source.Samples[i].y = 0;
}
}
return prediction_source;
}
private DataSource TestSource(int step)
{
double[] prediction_on_test = trainer.GetPrediction(test_data, step);
DataSource prediction_source = new DataSource();
prediction_source.Length = data.Length;
float acc = 0;
for (int i = 0; i < data.Length; i++)
{
if (i < network.InputCount() + train_data.Length + step - 1)
{
prediction_source.Samples[i].x = 0;
prediction_source.Samples[i].y = 0;
}
else
{
prediction_source.Samples[i].x = i;
prediction_source.Samples[i].y = (float)prediction_on_test[i - network.InputCount() - train_data.Length - step + 1];
}
}
return prediction_source;
}
private DataSource InitialSource()
{
DataSource initial_source;
initial_source = new DataSource();
initial_source.Length = data.Length;
for (int i = 0; i < data.Length; i++)
{
initial_source.Samples[i].x = i;
initial_source.Samples[i].y = (float) data[i];
}
return initial_source;
}
private void AssignData()
{
int trainset_count = data.Length*(int) numericTrainPercent.Value/100;
train_data = new double[trainset_count];
test_data = new double[data.Length - trainset_count];
for (int i = 0; i < trainset_count; i++)
{
train_data[i] = data[i];
}
for (int i = trainset_count; i < data.Length; i++)
{
test_data[i - trainset_count] = data[i];
}
}
}
}