-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (99 loc) · 3.93 KB
/
Copy pathProgram.cs
File metadata and controls
110 lines (99 loc) · 3.93 KB
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
using Accord.Neuro;
using Accord.Neuro.ActivationFunctions;
using Accord.Neuro.Learning;
using Accord.Neuro.Networks;
using Accord.Math;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AForge.Neuro.Learning;
using System.IO;
namespace DeepLearning
{
class Program
{
static void Main(string[] args)
{
// We'll use a simple XOR function as input.
double[][] inputs = new double[][] {
new double[] { 0, 0 },
new double[] { 0, 1 },
new double[] { 1, 0 },
new double[] { 1, 1 }
};
// XOR output, cooresponding to the input.
double[][] outputs = new double[][] {
new double[] { 0 },
new double[] { 1 },
new double[] { 1 },
new double[] { 0 }
};
// Setup the deep belief network (2 inputs, 3 hidden, 1 output) and initialize with random weights.
DeepBeliefNetwork network = new DeepBeliefNetwork(2, 3, 1);
new GaussianWeights(network, 0.1).Randomize();
network.UpdateVisibleWeights();
// Setup the learning algorithm.
DeepBeliefNetworkLearning teacher = new DeepBeliefNetworkLearning(network)
{
Algorithm = (h, v, i) => new ContrastiveDivergenceLearning(h, v)
{
LearningRate = 0.1,
Momentum = 0.5,
Decay = 0.001,
}
};
// Setup batches of input for learning.
int batchCount = Math.Max(1, inputs.Length / 100);
// Create mini-batches to speed learning.
int[] groups = Accord.Statistics.Tools.RandomGroups(inputs.Length, batchCount);
double[][][] batches = inputs.Subgroups(groups);
// Learning data for the specified layer.
double[][][] layerData;
// Unsupervised learning on each hidden layer, except for the output.
for (int layerIndex = 0; layerIndex < network.Machines.Count - 1; layerIndex++)
{
teacher.LayerIndex = layerIndex;
layerData = teacher.GetLayerInput(batches);
for (int i = 0; i < 5000; i++)
{
double error = teacher.RunEpoch(layerData) / inputs.Length;
if (i % 10 == 0)
{
Console.WriteLine(i + ", Error = " + error);
}
}
}
// Supervised learning on entire network, to provide output classification.
var teacher2 = new BackPropagationLearning(network)
{
LearningRate = 0.1,
Momentum = 0.5
};
// Run supervised learning.
for (int i = 0; i < 5000; i++)
{
double error = teacher2.RunEpoch(inputs, outputs) / inputs.Length;
if (i % 10 == 0)
{
Console.WriteLine(i + ", Error = " + error);
}
}
// Test the resulting accuracy.
int correct = 0;
for (int i = 0; i < inputs.Length; i++)
{
double[] outputValues = network.Compute(inputs[i]);
double outputResult = outputValues.First() >= 0.5 ? 1 : 0;
if (outputResult == outputs[i].First())
{
correct++;
}
}
Console.WriteLine("Correct " + correct + "/" + inputs.Length + ", " + Math.Round(((double)correct / (double)inputs.Length * 100), 2) + "%");
Console.Write("Press any key to quit ..");
Console.ReadKey();
}
}
}