-
Notifications
You must be signed in to change notification settings - Fork 1
/
TripleFluentNetTraining.cs
327 lines (266 loc) · 12.5 KB
/
TripleFluentNetTraining.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
using ConvNetSharp;
using ConvNetSharp.Fluent;
using ConvNetSharp.Training;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Training
{
class TripleFluentNetTraining
{
public static void Run()
{
var random = new Random(RandomUtilities.Seed);
int normalInputWidth = 19;
int earlyInputWidth = 19;
int strongInputWidth = 19;
string NetName = $"net.dat";
string NetName_early = $"net_early.dat";
string NetName_strong = $"net_strong.dat";
var entryContainer = new EntryContainer();
var entryContainer_early = new EntryContainer();
var entryContainer_strong = new EntryContainer();
#region Load Net
INet singleNet = NetExtension.LoadOrCreateNet(NetName, () =>
{
var net = FluentNet.Create(normalInputWidth, normalInputWidth, 3)
.Conv(5, 5, 16).Stride(5).Pad(2)
.Tanh()
.Conv(3, 3, 16).Stride(1).Pad(1)
.Tanh()
.FullyConn(100)
.Relu()
.FullyConn(5)
.Softmax(5).Build();
return net;
});
INet singleNet_early = NetExtension.LoadOrCreateNet(NetName_early, () =>
{
var net = FluentNet.Create(earlyInputWidth, earlyInputWidth, 3)
.Conv(5, 5, 16).Stride(5).Pad(2)
.Tanh()
.Conv(3, 3, 16).Stride(1).Pad(1)
.Tanh()
.FullyConn(100)
.Relu()
.FullyConn(5)
.Softmax(5).Build();
return net;
});
INet singleNet_strong = NetExtension.LoadOrCreateNet(NetName_strong, () =>
{
var net = FluentNet.Create(strongInputWidth, strongInputWidth, 3)
.Conv(5, 5, 16).Stride(5).Pad(2)
.Tanh()
.Conv(3, 3, 16).Stride(1).Pad(1)
.Tanh()
.FullyConn(100)
.Relu()
.FullyConn(5)
.Softmax(5).Build();
return net;
});
#endregion
#region Load data
var hltFiles = Directory.EnumerateFiles(@"..\..\..\games\2609\", "*.hlt").ToList(); // erdman games downloaded with HltDownloader
int total = hltFiles.Count;
Console.WriteLine($"Loading {total} games...");
foreach (var file in hltFiles)
{
Console.WriteLine(total--);
HltReader reader = new HltReader(file);
var playerId = -1;
var playerToCopy = reader.PlayerNames.FirstOrDefault(o => o.StartsWith("erdman"));
if (playerToCopy != null)
{
playerId = reader.PlayerNames.IndexOf(playerToCopy) + 1;
}
if (playerId != -1)
{
var width = reader.Width;
var height = reader.Height;
int lastmoveCount = 1;
for (var frame = 0; frame < reader.FrameCount - 1; frame++)
{
bool earlyGame = lastmoveCount < 25;
var currentFrame = reader.GetFrame(frame);
var map = currentFrame.map;
var moves = currentFrame.moves;
var helper = new Helper(map, (ushort)playerId);
bool foundInFrame = false;
int moveCount = 0;
// moves
for (ushort x = 0; x < width; x++)
{
for (ushort y = 0; y < height; y++)
{
if (map[x, y].Owner == playerId)
{
bool strong = map[x, y].Strength > 200;
foundInFrame = true;
moveCount++;
if ((earlyGame && random.NextDouble() < 1.5 / lastmoveCount) || (strong && random.NextDouble() < 1.5 / lastmoveCount) || random.NextDouble() < 1.0 / lastmoveCount)
{
var w = normalInputWidth;
var container = entryContainer;
if (earlyGame)
{
w = earlyInputWidth;
container = entryContainer_early;
}
else if (strong)
{
w = strongInputWidth;
container = entryContainer_strong;
}
var convVolume = map.GetVolume(w, playerId, x, y);
var direction = moves[y][x];
var entry1 = new Entry(new[] { convVolume }, direction, x, y, frame, file.GetHashCode());
container.Add(entry1);
var entry2 = new Entry(new[] { convVolume.Flip(VolumeUtilities.FlipMode.LeftRight) }, (int)Helper.FlipLeftRight((Direction)direction), x, y, frame, file.GetHashCode());
container.Add(entry2);
var entry3 = new Entry(new[] { convVolume.Flip(VolumeUtilities.FlipMode.UpDown) }, (int)Helper.FlipUpDown((Direction)direction), x, y, frame, file.GetHashCode());
container.Add(entry3);
var entry4 = new Entry(new[] { convVolume.Flip(VolumeUtilities.FlipMode.Both) }, (int)Helper.FlipBothWay((Direction)direction), x, y, frame, file.GetHashCode());
container.Add(entry4);
}
}
}
}
lastmoveCount = moveCount;
if (!foundInFrame)
{
// player has died
break;
}
}
}
else
{
Console.WriteLine("not found");
}
}
var length = entryContainer.Shuffle();
Console.WriteLine("normal: " + entryContainer.Summary);
length = entryContainer_early.Shuffle();
Console.WriteLine("early: " + entryContainer_early.Summary);
length = entryContainer_strong.Shuffle();
Console.WriteLine("strong " + entryContainer_strong.Summary);
#endregion
#region Training
var trainer = new AdamTrainer(singleNet) { BatchSize = 1024, LearningRate = 0.01, Beta1 = 0.9, Beta2 = 0.99, Eps = 1e-8 };
var trainingScheme = new TrainingScheme(singleNet, trainer, entryContainer, "single");
var trainer_early = new AdamTrainer(singleNet_early) { BatchSize = 1024, LearningRate = 0.01, Beta1 = 0.9, Beta2 = 0.99, Eps = 1e-8 };
var trainingScheme_early = new TrainingScheme(singleNet_early, trainer_early, entryContainer_early, "single_early");
var trainer_strong = new AdamTrainer(singleNet_strong) { BatchSize = 1024, LearningRate = 0.01, Beta1 = 0.9, Beta2 = 0.99, Eps = 1e-8 };
var trainingScheme_strong = new TrainingScheme(singleNet_strong, trainer_strong, entryContainer_strong, "single_strong");
bool save = true;
double lastValidationAcc = 0.0;
double lastValidationAcc_early = 0.0;
double lastValidationAcc_strong = 0.0;
double lastTrainAcc = 0.0;
double lastTrainAcc_early = 0.0;
double lastTrainAcc_strong = 0.0;
do
{
var normal = Task.Factory.StartNew(() =>
{
for (int i = 0; i < 50; i++)
{
if (i > 5)
{
trainer.L2Decay = 0.05;
}
Console.WriteLine($"[normal] Epoch #{i + 1}");
if (i % 50 == 0)
{
trainer.LearningRate = Math.Max(trainer.LearningRate / 5.0, 0.00001);
}
trainingScheme.RunEpoch();
#region Save Nets
if (save)
{
if (trainingScheme.ValidationAccuracy > lastValidationAcc)
{
lastValidationAcc = trainingScheme.ValidationAccuracy;
lastTrainAcc = trainingScheme.TrainAccuracy;
singleNet.SaveNet(NetName);
}
}
#endregion
if (Console.KeyAvailable)
{
break;
}
}
});
var early = Task.Factory.StartNew(() =>
{
for (int i = 0; i < 50; i++)
{
if (i > 5)
{
trainer_early.L2Decay = 0.05;
}
Console.WriteLine($"[early] Epoch #{i + 1}");
if (i % 50 == 0)
{
trainer_early.LearningRate = Math.Max(trainer_early.LearningRate / 5.0, 0.00001);
}
trainingScheme_early.RunEpoch();
#region Save Nets
if (save)
{
if (trainingScheme_early.ValidationAccuracy > lastValidationAcc_early)
{
lastValidationAcc_early = trainingScheme_early.ValidationAccuracy;
lastTrainAcc_early = trainingScheme_early.TrainAccuracy;
singleNet_early.SaveNet(NetName_early);
}
}
#endregion
if (Console.KeyAvailable)
{
break;
}
}
});
var strong = Task.Factory.StartNew(() =>
{
for (int i = 0; i < 50; i++)
{
if (i > 5)
{
trainer_strong.L2Decay = 0.05;
}
Console.WriteLine($"[strong] Epoch #{i + 1}");
if (i % 50 == 0)
{
trainer_strong.LearningRate = Math.Max(trainer_strong.LearningRate / 5.0, 0.00001);
}
trainingScheme_strong.RunEpoch();
#region Save Nets
if (save)
{
if (trainingScheme_strong.ValidationAccuracy > lastValidationAcc_strong)
{
lastValidationAcc_strong = trainingScheme_strong.ValidationAccuracy;
lastTrainAcc_strong = trainingScheme_strong.TrainAccuracy;
singleNet_strong.SaveNet(NetName_strong);
}
}
#endregion
if (Console.KeyAvailable)
{
break;
}
}
});
Task.WaitAll(new[] { normal, strong, early });
}
while (!Console.KeyAvailable);
#endregion
}
}
}