-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
39 lines (29 loc) · 990 Bytes
/
Program.cs
File metadata and controls
39 lines (29 loc) · 990 Bytes
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
using NeuralNetwork;
const int dataVectorCount = 3;
var mlp = new MultiLayerPerceptron(dataVectorCount, new [] {4,4,1 });
const int totalItemsInBatch = 4;
var xs = new double[ , ] {
{2.0, 3.0, -1.0},
{3.0, -1.0, 0.5},
{0.5, 1.0, 1.0},
{1.0, 1.0, -1.0},
};
var ys = new double [totalItemsInBatch] {1.0, -1.0, -1.0, 1.0};
var itemsToTrain = new List<List<Value>>();
var truth = ys.Select(x => new Value(x));
for(int i = 0; i < totalItemsInBatch; i++){
var list = new List<Value>();
for(int j = 0; j < dataVectorCount; j++){
var z = xs[i,j];
list.Add(new Value(z));
}
itemsToTrain.Add(list);
}
foreach(var epoch in Enumerable.Range(0,20)){
var batchResult = itemsToTrain.Select( x => mlp.Call(x.ToArray()).First() );
var loss = Enumerable.Zip(batchResult, truth).Select(x => (x.First - x.Second).Pow(2)).Sum();
mlp.ZeroGradient();
loss.Backward();
mlp.Step();
Console.WriteLine($"Epoch: {epoch}, loss: {loss.Data}");
}