-
Notifications
You must be signed in to change notification settings - Fork 109
/
Program.cs
94 lines (79 loc) · 3.27 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
using System;
using System.Linq;
using ConvNetSharp.Core;
using ConvNetSharp.Core.Layers.Double;
using ConvNetSharp.Core.Training;
using ConvNetSharp.Volume;
namespace MnistDemo
{
internal class Program
{
private readonly CircularBuffer<double> _testAccWindow = new CircularBuffer<double>(100);
private readonly CircularBuffer<double> _trainAccWindow = new CircularBuffer<double>(100);
private Net<double> _net;
private int _stepCount;
private SgdTrainer<double> _trainer;
private static void Main()
{
var program = new Program();
program.MnistDemo();
}
private void MnistDemo()
{
var datasets = new DataSets();
if (!datasets.Load(100))
{
return;
}
// Create network
this._net = new Net<double>();
this._net.AddLayer(new InputLayer(28, 28, 1));
this._net.AddLayer(new ConvLayer(5, 5, 8) { Stride = 1, Pad = 2 });
this._net.AddLayer(new ReluLayer());
this._net.AddLayer(new PoolLayer(2, 2) { Stride = 2 });
this._net.AddLayer(new ConvLayer(5, 5, 16) { Stride = 1, Pad = 2 });
this._net.AddLayer(new ReluLayer());
this._net.AddLayer(new PoolLayer(3, 3) { Stride = 3 });
this._net.AddLayer(new FullyConnLayer(10));
this._net.AddLayer(new SoftmaxLayer(10));
this._trainer = new SgdTrainer<double>(this._net)
{
LearningRate = 0.01,
BatchSize = 20,
Momentum = 0.9
};
Console.WriteLine("Convolutional neural network learning...[Press any key to stop]");
do
{
var trainSample = datasets.Train.NextBatch(this._trainer.BatchSize);
Train(trainSample.Item1, trainSample.Item2, trainSample.Item3);
var testSample = datasets.Test.NextBatch(this._trainer.BatchSize);
Test(testSample.Item1, testSample.Item3, this._testAccWindow);
Console.WriteLine("Loss: {0} Train accuracy: {1}% Test accuracy: {2}%", this._trainer.Loss,
Math.Round(this._trainAccWindow.Items.Average() * 100.0, 2),
Math.Round(this._testAccWindow.Items.Average() * 100.0, 2));
Console.WriteLine("Example seen: {0} Fwd: {1}ms Bckw: {2}ms", this._stepCount,
Math.Round(this._trainer.ForwardTimeMs, 2),
Math.Round(this._trainer.BackwardTimeMs, 2));
} while (!Console.KeyAvailable);
}
private void Test(Volume<double> x, int[] labels, CircularBuffer<double> accuracy, bool forward = true)
{
if (forward)
{
this._net.Forward(x);
}
var prediction = this._net.GetPrediction();
for (var i = 0; i < labels.Length; i++)
{
accuracy.Add(labels[i] == prediction[i] ? 1.0 : 0.0);
}
}
private void Train(Volume<double> x, Volume<double> y, int[] labels)
{
this._trainer.Train(x, y);
Test(x, labels, this._trainAccWindow, false);
this._stepCount += labels.Length;
}
}
}