├── .github
└── FUNDING.yml
├── .gitignore
├── .nuget
├── NuGet.Config
├── NuGet.exe
└── NuGet.targets
├── DeepLearning.sln
├── DeepLearning
├── App.config
├── DataManager.cs
├── DeepLearning.csproj
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
└── packages.config
├── data
└── data.txt
└── readme.md
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [primaryobjects]
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.cache
2 | *.dll
3 | *.pdb
4 | *.txt
5 | !README.txt
6 | !HELP.txt
7 | *.suo
8 | *.force
9 | */obj/Debug/*
10 | */obj/Release/*
11 | */bin/Debug/*
12 | */bin/Release/*
13 | TestResults/*
14 | packages/*
--------------------------------------------------------------------------------
/.nuget/NuGet.Config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.nuget/NuGet.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/primaryobjects/deep-learning/f4307a1720d99ba6bb7c65f3b8238608a4044f1f/.nuget/NuGet.exe
--------------------------------------------------------------------------------
/.nuget/NuGet.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildProjectDirectory)\..\
5 |
6 |
7 | false
8 |
9 |
10 | false
11 |
12 |
13 | true
14 |
15 |
16 | false
17 |
18 |
19 |
20 |
21 |
22 |
26 |
27 |
28 |
29 |
30 | $([System.IO.Path]::Combine($(SolutionDir), ".nuget"))
31 | $([System.IO.Path]::Combine($(ProjectDir), "packages.config"))
32 |
33 |
34 |
35 |
36 | $(SolutionDir).nuget
37 | packages.config
38 |
39 |
40 |
41 |
42 | $(NuGetToolsPath)\NuGet.exe
43 | @(PackageSource)
44 |
45 | "$(NuGetExePath)"
46 | mono --runtime=v4.0.30319 $(NuGetExePath)
47 |
48 | $(TargetDir.Trim('\\'))
49 |
50 | -RequireConsent
51 | -NonInteractive
52 |
53 | "$(SolutionDir) "
54 | "$(SolutionDir)"
55 |
56 |
57 | $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)
58 | $(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols
59 |
60 |
61 |
62 | RestorePackages;
63 | $(BuildDependsOn);
64 |
65 |
66 |
67 |
68 | $(BuildDependsOn);
69 | BuildPackage;
70 |
71 |
72 |
73 |
74 |
75 |
76 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
91 |
92 |
95 |
96 |
97 |
98 |
100 |
101 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
133 |
134 |
135 |
136 |
--------------------------------------------------------------------------------
/DeepLearning.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.21005.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeepLearning", "DeepLearning\DeepLearning.csproj", "{B06BDCBB-94F7-49F2-B7A6-D9D472F9179D}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{A71E9C47-BFA4-40BE-85C8-EEB764EF0D36}"
9 | ProjectSection(SolutionItems) = preProject
10 | .nuget\NuGet.Config = .nuget\NuGet.Config
11 | .nuget\NuGet.exe = .nuget\NuGet.exe
12 | .nuget\NuGet.targets = .nuget\NuGet.targets
13 | EndProjectSection
14 | EndProject
15 | Global
16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
17 | Debug|Any CPU = Debug|Any CPU
18 | Release|Any CPU = Release|Any CPU
19 | EndGlobalSection
20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
21 | {B06BDCBB-94F7-49F2-B7A6-D9D472F9179D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22 | {B06BDCBB-94F7-49F2-B7A6-D9D472F9179D}.Debug|Any CPU.Build.0 = Debug|Any CPU
23 | {B06BDCBB-94F7-49F2-B7A6-D9D472F9179D}.Release|Any CPU.ActiveCfg = Release|Any CPU
24 | {B06BDCBB-94F7-49F2-B7A6-D9D472F9179D}.Release|Any CPU.Build.0 = Release|Any CPU
25 | EndGlobalSection
26 | GlobalSection(SolutionProperties) = preSolution
27 | HideSolutionNode = FALSE
28 | EndGlobalSection
29 | EndGlobal
30 |
--------------------------------------------------------------------------------
/DeepLearning/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/DeepLearning/DataManager.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace DeepLearning
9 | {
10 | public static class DataManager
11 | {
12 | public static double[][] Load(string pathName, out double[][] outputs)
13 | {
14 | List list = new List();
15 | List output = new List();
16 |
17 | // Read data file.
18 | using (FileStream fs = File.Open(pathName, FileMode.Open, FileAccess.Read))
19 | {
20 | using (BufferedStream bs = new BufferedStream(fs))
21 | {
22 | using (StreamReader sr = new StreamReader(bs))
23 | {
24 | List row = new List();
25 |
26 | bool readOutput = false;
27 |
28 | string line;
29 | while ((line = sr.ReadLine()) != null)
30 | {
31 | // Collect each 0 and 1 from the data.
32 | foreach (char ch in line)
33 | {
34 | if (!readOutput)
35 | {
36 | // Reading input.
37 | if (ch != ' ' && ch != '\n')
38 | {
39 | // Add this digit to our input.
40 | row.Add(Double.Parse(ch.ToString()));
41 | }
42 | else if (ch == ' ')
43 | {
44 | // End of input reached. Store the input row.
45 | list.Add(row.ToArray());
46 |
47 | // Start a new input row.
48 | row = new List();
49 |
50 | // Set flag to read output label.
51 | readOutput = true;
52 | }
53 | }
54 | else
55 | {
56 | // Read output label.
57 | output.Add(FormatOutputVector(Double.Parse(ch.ToString())));
58 |
59 | // Set flag to read inputs for next row.
60 | readOutput = false;
61 | }
62 | }
63 | }
64 | }
65 | }
66 | }
67 |
68 | // Set outputs.
69 | outputs = output.ToArray();
70 |
71 | // Return inputs;
72 | return list.ToArray();
73 | }
74 |
75 | #region Utility Methods
76 |
77 | ///
78 | /// Converts a numeric output label (0, 1, 2, 3, etc) to its cooresponding array of doubles, where all values are 0 except for the index matching the label (ie., if the label is 2, the output is [0, 0, 1, 0, 0, ...]).
79 | ///
80 | /// double
81 | /// double[]
82 | public static double[] FormatOutputVector(double label)
83 | {
84 | double[] output = new double[10];
85 |
86 | for (int i = 0; i < output.Length; i++)
87 | {
88 | if (i == label)
89 | {
90 | output[i] = 1;
91 | }
92 | else
93 | {
94 | output[i] = 0;
95 | }
96 | }
97 |
98 | return output;
99 | }
100 |
101 | ///
102 | /// Finds the largest output value in an array and returns its index. This allows for sequential classification from the outputs of a neural network (ie., if output at index 2 is the largest, the classification is class "3" (zero-based)).
103 | ///
104 | /// double[]
105 | /// double
106 | public static double FormatOutputResult(double[] output)
107 | {
108 | return output.ToList().IndexOf(output.Max());
109 | }
110 |
111 | #endregion
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/DeepLearning/DeepLearning.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {B06BDCBB-94F7-49F2-B7A6-D9D472F9179D}
8 | Exe
9 | Properties
10 | DeepLearning
11 | DeepLearning
12 | v4.5
13 | 512
14 | ..\
15 | true
16 |
17 |
18 | AnyCPU
19 | true
20 | full
21 | false
22 | bin\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 |
27 |
28 | AnyCPU
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 |
36 |
37 |
38 | ..\packages\Accord.2.12.0.0\lib\Accord.dll
39 |
40 |
41 | ..\packages\Accord.MachineLearning.2.12.0.0\lib\Accord.MachineLearning.dll
42 |
43 |
44 | ..\packages\Accord.Math.2.12.0.0\lib\Accord.Math.dll
45 |
46 |
47 | ..\packages\Accord.Neuro.2.12.0.0\lib\Accord.Neuro.dll
48 |
49 |
50 | ..\packages\Accord.Statistics.2.12.0.0\lib\Accord.Statistics.dll
51 |
52 |
53 | ..\packages\AForge.2.2.5\lib\AForge.dll
54 |
55 |
56 | ..\packages\AForge.Genetic.2.2.5\lib\AForge.Genetic.dll
57 |
58 |
59 | ..\packages\AForge.Math.2.2.5\lib\AForge.Math.dll
60 |
61 |
62 | ..\packages\AForge.Neuro.2.2.5\lib\AForge.Neuro.dll
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
90 |
--------------------------------------------------------------------------------
/DeepLearning/Program.cs:
--------------------------------------------------------------------------------
1 | using Accord.Neuro;
2 | using Accord.Neuro.ActivationFunctions;
3 | using Accord.Neuro.Learning;
4 | using Accord.Neuro.Networks;
5 | using Accord.Math;
6 | using System;
7 | using System.Collections.Generic;
8 | using System.Linq;
9 | using System.Text;
10 | using System.Threading.Tasks;
11 | using AForge.Neuro.Learning;
12 | using System.IO;
13 |
14 | namespace DeepLearning
15 | {
16 | class Program
17 | {
18 | static void Main(string[] args)
19 | {
20 | double[][] inputs;
21 | double[][] outputs;
22 | double[][] testInputs;
23 | double[][] testOutputs;
24 |
25 | // Load ascii digits dataset.
26 | inputs = DataManager.Load(@"../../../data/data.txt", out outputs);
27 |
28 | // The first 500 data rows will be for training. The rest will be for testing.
29 | testInputs = inputs.Skip(500).ToArray();
30 | testOutputs = outputs.Skip(500).ToArray();
31 | inputs = inputs.Take(500).ToArray();
32 | outputs = outputs.Take(500).ToArray();
33 |
34 | // Setup the deep belief network and initialize with random weights.
35 | DeepBeliefNetwork network = new DeepBeliefNetwork(inputs.First().Length, 10, 10);
36 | new GaussianWeights(network, 0.1).Randomize();
37 | network.UpdateVisibleWeights();
38 |
39 | // Setup the learning algorithm.
40 | DeepBeliefNetworkLearning teacher = new DeepBeliefNetworkLearning(network)
41 | {
42 | Algorithm = (h, v, i) => new ContrastiveDivergenceLearning(h, v)
43 | {
44 | LearningRate = 0.1,
45 | Momentum = 0.5,
46 | Decay = 0.001,
47 | }
48 | };
49 |
50 | // Setup batches of input for learning.
51 | int batchCount = Math.Max(1, inputs.Length / 100);
52 | // Create mini-batches to speed learning.
53 | int[] groups = Accord.Statistics.Tools.RandomGroups(inputs.Length, batchCount);
54 | double[][][] batches = inputs.Subgroups(groups);
55 | // Learning data for the specified layer.
56 | double[][][] layerData;
57 |
58 | // Unsupervised learning on each hidden layer, except for the output layer.
59 | for (int layerIndex = 0; layerIndex < network.Machines.Count - 1; layerIndex++)
60 | {
61 | teacher.LayerIndex = layerIndex;
62 | layerData = teacher.GetLayerInput(batches);
63 | for (int i = 0; i < 200; i++)
64 | {
65 | double error = teacher.RunEpoch(layerData) / inputs.Length;
66 | if (i % 10 == 0)
67 | {
68 | Console.WriteLine(i + ", Error = " + error);
69 | }
70 | }
71 | }
72 |
73 | // Supervised learning on entire network, to provide output classification.
74 | var teacher2 = new BackPropagationLearning(network)
75 | {
76 | LearningRate = 0.1,
77 | Momentum = 0.5
78 | };
79 |
80 | // Run supervised learning.
81 | for (int i = 0; i < 500; i++)
82 | {
83 | double error = teacher2.RunEpoch(inputs, outputs) / inputs.Length;
84 | if (i % 10 == 0)
85 | {
86 | Console.WriteLine(i + ", Error = " + error);
87 | }
88 | }
89 |
90 | // Test the resulting accuracy.
91 | int correct = 0;
92 | for (int i = 0; i < inputs.Length; i++)
93 | {
94 | double[] outputValues = network.Compute(testInputs[i]);
95 | if (DataManager.FormatOutputResult(outputValues) == DataManager.FormatOutputResult(testOutputs[i]))
96 | {
97 | correct++;
98 | }
99 | }
100 |
101 | Console.WriteLine("Correct " + correct + "/" + inputs.Length + ", " + Math.Round(((double)correct / (double)inputs.Length * 100), 2) + "%");
102 | Console.Write("Press any key to quit ..");
103 | Console.ReadKey();
104 | }
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/DeepLearning/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("DeepLearning")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("DeepLearning")]
13 | [assembly: AssemblyCopyright("Copyright © 2014")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("f4acdd43-19b0-44b0-adc3-141ca56888b5")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/DeepLearning/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | Deep-Learning Neural Networks with Accord .NET
2 | ==============================================
3 |
4 | A simple example of using the Accord .NET C# library to implement a deep-learning neural network (ie., Deep Belief Network) with machine learning.
5 |
6 | Checkout branch "XOR" for a simple example of deep-learning with Accord .NET. This branch contains training using one of the most basic neural network cases - the XOR function.
7 |
8 | Checkout the master branch for a slightly less-basic example of training on an ASCII digit dataset. This example uses multiple layers in the neural network and has the potential to "dream" representations of data within its layers.
9 |
10 | Deep-Learning Strategy
11 | ----------------------
12 |
13 | 1. Start with a neural network with multiple RestrictedBoltzman machine layers.
14 | 2. Use unsupervised training on each layer in the network, one at a time, except for the output layer. This allows each layer to learn specific features about the input data.
15 | 3. If you ran unsupervised training on the whole network, including the output layer, add an additional (untrained) layer to the network to serve as the output layer. Otherwise, skip this step.
16 | 4. Run back-propagation on the entire network to fine-tune for classification.
17 |
--------------------------------------------------------------------------------