Skip to content

Commit b6e9279

Browse files
committed
Changed name of the examples
1 parent 5fd88fc commit b6e9279

File tree

1,026 files changed

+1792
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,026 files changed

+1792
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Reference Include="NeuralNetwork">
10+
<HintPath>..\DLL is HERE\netcoreapp3.1\NeuralNetwork.dll</HintPath>
11+
</Reference>
12+
</ItemGroup>
13+
14+
</Project>
+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
using System;
2+
using System.Globalization;
3+
using System.IO;
4+
using System.Reflection;
5+
using System.Threading;
6+
using NN;
7+
8+
namespace NeuralNetworkExample
9+
{
10+
//---------------------------------------------------------------
11+
//
12+
//
13+
// This file is just an example how to use the NeuralNetwork library.
14+
// You are free to use the library in yout own needs
15+
//
16+
//
17+
//---------------------------------------------------------------
18+
19+
class NeuralNetworkPROBuilder
20+
{
21+
const double MomentTemp = 0.7;
22+
const double LearningRateTemp = 0.1;
23+
const string NeuronsAndLayers = "4+ 15+ 6 3"; //"[0]-InputNeurons, [1]-Neurons In 1-st HiddenLayer,
24+
// [2]-Neurons In 2-nd HiddenLayer,[..],[n-1]-Neurons In (n-1)-th HiddenLayer, [n]-OutputNeurons"
25+
// put + in each layer (except OUTPUT) to add bias
26+
static double terminatingErrorProcents = 0.001; //The average error procent on which we want to end training
27+
static uint refreshSpeed = 1800;
28+
public struct TrainAndTest
29+
{
30+
public double[] IN; //All Input values
31+
public double[] OUT; // All output values
32+
}
33+
34+
private static TrainAndTest[] AddTrainOrTest(string fullPath)
35+
{
36+
string[] lines = File.ReadAllLines(fullPath);
37+
//TRAIN or TEST units
38+
TrainAndTest[] Data = new TrainAndTest[lines.Length / 2];
39+
//Training and test files are .txt, where non-odd lines are INPUT values and odd lines are OUTPUT values
40+
41+
uint countOfUnit = 0;
42+
for (uint i = 1; i <= lines.Length; ++i)
43+
{
44+
if (i % 2 != 0)
45+
{
46+
string[] line = lines[i - 1].Split(' ');
47+
Data[countOfUnit].IN = new double[line.Length];
48+
for (uint j = 0; j < line.Length; ++j) //Every INPUT value in the line must be readen
49+
Data[countOfUnit].IN[j] = Convert.ToDouble(line[j]);
50+
}
51+
else
52+
{
53+
string[] line = lines[i - 1].Split(' ');
54+
Data[countOfUnit].OUT = new double[line.Length];
55+
for (int j = 0; j < line.Length; ++j) //Every OUTPUT value in the line must be readen
56+
Data[countOfUnit].OUT[j] = Convert.ToDouble(line[j]);
57+
++countOfUnit;
58+
}
59+
}
60+
61+
return Data;
62+
// return Data;
63+
}
64+
private static uint CheckForMistakes(ref NeuralNetwork network, ref TrainAndTest[] testData)
65+
{
66+
uint errCount = 0;
67+
68+
for (uint i = 0; i < testData.GetLength(0); ++i) //Run through all TEST units
69+
{
70+
Neuron[] answer = network.RunNetwork(testData[i].IN);
71+
double[] _answer = new double[answer.Length];
72+
73+
for (uint j = 0; j < answer.Length; ++j) //Normalizing answers in this unit
74+
_answer[j] = answer[j].value > 0.5 ? 1 : 0;
75+
76+
for (uint j = 0; j < _answer.Length; ++j) //Checking answers in this unit for a mistake
77+
{
78+
if (_answer[j] != testData[i].OUT[j]) //If a mistake was made then:
79+
{
80+
for (uint a = 0; a < testData[i].IN.Length; ++a) // -Write down all INPUTs
81+
Console.Write($"I{a}={testData[i].IN[a]} ");
82+
Console.WriteLine();
83+
for (uint k = 0; k < answer.Length; ++k) // -Write down all OUTPUTS + activated OUTPUTS + ideal OUTPUTS
84+
Console.WriteLine("Real answer = " + answer[k].value + " Activated answer = " + _answer[k] + " Ideal = " + testData[i].OUT[k]);
85+
++errCount;
86+
Console.WriteLine();
87+
break;
88+
}
89+
}
90+
}
91+
92+
return errCount;
93+
}
94+
95+
96+
private static bool pressedENTER = false;
97+
private static void CheckForEnter()
98+
{
99+
while (!pressedENTER)
100+
{
101+
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter)
102+
{
103+
pressedENTER = true;
104+
return;
105+
}
106+
}
107+
}
108+
static void Main()
109+
{
110+
var sepByThous = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
111+
sepByThous.NumberGroupSeparator = " ";
112+
113+
#region Training&Test initialization
114+
string firstPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
115+
116+
firstPath = Path.Combine(AppContext.BaseDirectory, @"..\..\..\TrainingAndTest\");
117+
118+
TrainAndTest[] trainingData = AddTrainOrTest(firstPath + "numbers.txt");
119+
//The same for TEST units
120+
TrainAndTest[] testData = AddTrainOrTest(firstPath + "numbersTEST.txt");
121+
122+
#endregion
123+
124+
#region Main part - network training
125+
new Thread(new ThreadStart(CheckForEnter)).Start();
126+
127+
//Creating an object of NeuralNetwork with same parameters as we described in variables
128+
//NeuralNetwork network = new NeuralNetwork(@"C:\s\Neural.aaa");
129+
NeuralNetwork network = new NeuralNetwork(NeuronsAndLayers, -1, 1)
130+
{
131+
Moment = MomentTemp,
132+
LearningRate = LearningRateTemp
133+
};
134+
135+
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
136+
sw.Start();
137+
138+
double errorSum;
139+
uint iteration = 0;
140+
uint i;
141+
uint j;
142+
double end;
143+
double error;
144+
Neuron[] endValue;
145+
do
146+
{
147+
errorSum = 0;
148+
for (i = 0; i < trainingData.Length; ++i) //Run through all TRAIN units
149+
{
150+
//Running the network with current INPUT values of this unit
151+
endValue = network.RunNetwork(trainingData[i].IN);
152+
153+
//Counting an error of current unit
154+
end = 0;
155+
for (j = 0; j < endValue.Length; ++j)
156+
end += Math.Pow(trainingData[i].OUT[j] - endValue[j].value, 2);
157+
error = end / trainingData.Length; //((i-a1)*(i1-a1)+...+(in-an)*(in-an))/n
158+
errorSum += error;
159+
160+
network.TeachNetwork(trainingData[i].OUT);
161+
}
162+
if (iteration++ % refreshSpeed == 0)
163+
Console.WriteLine(iteration.ToString("#,0", sepByThous) + " average error = " + Math.Round((errorSum / trainingData.GetLength(0)) * 100, 5) + "%" + " = " + Math.Round(errorSum * 100, 5) + "% general, " + ((double)sw.ElapsedMilliseconds / 1000).ToString("#,0.000", sepByThous) + " sec");
164+
} while (((errorSum / trainingData.GetLength(0)) * 100 > terminatingErrorProcents) && !pressedENTER); //while average error procent is greater tnah TEP - continue
165+
sw.Stop();
166+
#endregion
167+
168+
#region Output
169+
uint errCount = CheckForMistakes(ref network, ref testData);
170+
171+
Console.WriteLine("\nAccuracy = " + Math.Round((double)(testData.GetLength(0) - (errCount)) / testData.GetLength(0) * 100, 3) + "%");
172+
Console.WriteLine("Right answers from the test = " + (testData.GetLength(0) - errCount).ToString("#,0", sepByThous) + " of " + testData.GetLength(0).ToString("#,0", sepByThous));
173+
Console.WriteLine("\nNumber of iterations = " + iteration.ToString("#,0", sepByThous));
174+
Console.WriteLine("Training time = " + ((double)sw.ElapsedMilliseconds / 1000).ToString("#,0.000", sepByThous) + " sec");
175+
176+
//network.SaveNetwork(@"C:\s\Neural.aaa");
177+
#endregion
178+
}
179+
}
180+
}
Loading

0 commit comments

Comments
 (0)