Skip to content

Commit e8be611

Browse files
committed
add ordered names
1 parent 221f20f commit e8be611

27 files changed

+183
-230
lines changed

MLTests/SimplestNeuralTests.cs renamed to MLTests/1-1_SingleNeuronTest.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
namespace MLTests
55
{
6+
public class SimpiestNeural
7+
{
8+
public double Weight { set; get; } = 0;
9+
10+
public double Prediction(double input) {
11+
return Weight * input;
12+
}
13+
}
14+
615
public class SimplestNeuralTests
716
{
817
[Test]
@@ -15,7 +24,6 @@ public void ShouldSimplePredict()
1524
// 10 * 0.5 = 5
1625
SimpiestNeural neuron = new SimpiestNeural() { Weight = 0.1 };
1726
neuron.Prediction(8.5).Should().BeInRange(0.84, 0.86);
18-
//gameData.GetGameState()
1927
}
2028

2129
}

MLTests/SingleInMulOutTests.cs renamed to MLTests/1-2_SingleInMulOutTest.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@
33

44
namespace MLTests
55
{
6-
public class SingleInMulOutTests
6+
public class SingleInMulOut
7+
{
8+
double[] _weights;
9+
public SingleInMulOut(double[] weights)
10+
{
11+
_weights = weights;
12+
}
13+
public double[] GetPredict(double input)
14+
{
15+
return VectorMath.ElementMul(input, _weights);
16+
}
17+
}
18+
19+
public class SingleInMulOutTest
720
{
821
[Test]
922
public void ShouldReturnInjureWinSad()

MLTests/MultipleInputNeuralTests.cs renamed to MLTests/1-3_MultipleInputNeuralTest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44

55
namespace MLTests
66
{
7-
public class MultipleInputNeuralTests
7+
public class MultipleWeightsNeural
8+
{
9+
public NDarray Weights { get; set; }
10+
11+
public NDarray Prediction(NDarray inputs)
12+
{
13+
return inputs.dot(Weights);
14+
}
15+
}
16+
17+
public class MultipleInputNeuralTest
818
{
919
[Test]
1020
public void ShouldMultiplePredict()

MLTests/MulInMulOutTests.cs renamed to MLTests/1-4_MulInMulOutTest.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44

55
namespace MLTests
66
{
7-
public class MulInMulOutTests
7+
public class MulInMulOut
8+
{
9+
double[][] _weights;
10+
public MulInMulOut(double[][] weights)
11+
{
12+
_weights = weights;
13+
}
14+
public double[] GetPrediction(double[] vec)
15+
{
16+
return VectorMath.VectMatMul(vec, _weights);
17+
}
18+
}
19+
20+
public class MulInMulOutTest
821
{
922
[Test]
1023
public void ShouldMulPredict()

MLTests/NDigitalRecognizer.cs renamed to MLTests/1-5_NDigitalRecognizerTest.cs

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
1-
namespace MLTests
1+
using FluentAssertions;
2+
using Keras.PreProcessing.Image;
3+
using Numpy;
4+
using NUnit.Framework;
5+
6+
namespace MLTests
27
{
8+
public interface IImageToVector
9+
{
10+
float[] GetVectorOnPath(string path);
11+
}
12+
13+
public class ImageToVector : IImageToVector
14+
{
15+
private const string DefaultImageType = "grayscale";
16+
private static readonly (int, int) DefaultSize = (28, 28);
17+
18+
public ImageToVector()
19+
{
20+
21+
}
22+
float[] GetNormalizedVector(float[] vector)
23+
{
24+
var max = vector.Max();
25+
var min = vector.Min();
26+
for(var i = 0; i < vector.Length; i++) vector[i] /= max;
27+
return vector;
28+
}
29+
30+
public float[] GetVectorOnPath(string path)
31+
{
32+
var image = ImageUtil.LoadImg(path, DefaultImageType, target_size: DefaultSize);
33+
NDarray imageArray = ImageUtil.ImageToArray(image);
34+
return GetNormalizedVector(imageArray.GetData<float>());
35+
}
36+
}
37+
38+
public class ImageToVectorTest
39+
{
40+
[Test]
41+
public void LoadImageTest()
42+
{
43+
var projectPath = "D:/projects.active/LearnML";
44+
var path = Path.GetFullPath(@$"{projectPath}/data/0/1.jpg");
45+
IImageToVector imgToVector = new ImageToVector();
46+
var vector = imgToVector.GetVectorOnPath(path);
47+
vector.Sum().Should().NotBe(0.0f);
48+
}
49+
}
50+
351
public class NDigitalRecognizer
452
{
553
private ImageToVector _vectorReader;
@@ -135,4 +183,48 @@ public void Learn(int iterations)
135183
return (result.Index, result.Element);
136184
}
137185
}
186+
187+
public class NDigitalRecognizerTest
188+
{
189+
[Test]
190+
public void ShouldRecognizeDigit()
191+
{
192+
var projectPath = "D:/projects.active/LearnML";
193+
var trainDataPath = Path.GetFullPath(@$"{projectPath}/data");
194+
var digitZeroPath = Path.Combine(trainDataPath, "0", "155.jpg");
195+
var digitOnePath = Path.Combine(trainDataPath, "1", "1055.jpg");
196+
var digitTwoPath = Path.Combine(trainDataPath, "2", "1055.jpg");
197+
var digitThreePath = Path.Combine(trainDataPath, "3", "1055.jpg");
198+
var digitFourPath = Path.Combine(trainDataPath, "4", "1055.jpg");
199+
var digitFivePath = Path.Combine(trainDataPath, "5", "55.jpg");
200+
var digitSixPath = Path.Combine(trainDataPath, "6", "1055.jpg");
201+
var digitSevenPath = Path.Combine(trainDataPath, "7", "1055.jpg");
202+
var digitEightPath = Path.Combine(trainDataPath, "8", "1055.jpg");
203+
var digitNinePath = Path.Combine(trainDataPath, "9", "55.jpg");
204+
205+
var inputSize = 28 * 28;
206+
var outputSize = 10;
207+
208+
var alpha = 0.001f;
209+
var iterations = 100;
210+
211+
var recognizer = new NDigitalRecognizer(new ImageToVector(), inputSize, outputSize, trainDataPath, alpha);
212+
213+
recognizer.Learn(iterations);
214+
215+
{
216+
var (digit, value) = recognizer.Predict(digitNinePath);
217+
Assert.That(digit, Is.EqualTo(9));
218+
//Assert.That(value, Is.AtLeast(0.5f));
219+
}
220+
{
221+
var (digit, value) = recognizer.Predict(digitFivePath);
222+
Assert.That(digit, Is.EqualTo(5));
223+
//Assert.That(value, Is.AtLeast(0.5f));
224+
}
225+
226+
//recognizer.Predict(digitOnePath)[zero].Should().BeInRange(0.0f, 0.6f);
227+
//recognizer.Predict(digitZeroPath)[zero].Should().BeInRange(0.8f, 1.2f);
228+
}
229+
}
138230
}

MLTests/VectorMath.cs renamed to MLTests/1_VectorMathTest.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using FluentAssertions;
2+
using NUnit.Framework;
63

74
namespace MLTests
85
{
96
public static class VectorMath
107
{
11-
128
public static double[] ElementwiseMultiplication(double[] vecA, double[] vecB)
139
{
1410
return vecA.Select((elementA, index) => elementA * vecB[index]).ToArray();
@@ -40,4 +36,20 @@ public static double ElementwiseAverage(double[] vecA)
4036
return vecA.Sum() / vecA.Length;
4137
}
4238
}
39+
40+
public class VectorMathTest
41+
{
42+
[Test]
43+
public void TestElementWiseMultiplication()
44+
{
45+
var A = new double[] { 0, 1, 0, 1 };
46+
var B = new double[] { 1, 0, 1, 0 };
47+
var C = new double[] { 0, 1, 1, 0 };
48+
var D = new double[] { 0.5, 0, 0.5, 0 };
49+
var E = new double[] { 0, 1, -1, 0 };
50+
51+
VectorMath.ElementwiseSum(VectorMath.ElementwiseMultiplication(C, E)).Should().Be(0.0);
52+
VectorMath.ElementwiseSum(VectorMath.ElementwiseMultiplication(C, C)).Should().Be(2);
53+
}
54+
}
4355
}

MLTests/GradientLearnTests.cs renamed to MLTests/2-1_GradientLearnTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace MLTests
77
{
8-
public class GradientLearnTests
8+
public class GradientLearnTest
99
{
1010
private double LearnWeightByGradient(double weight, double input, double predictionGoal, int iterations)
1111
{

MLTests/GradientMulInMulOutTests.cs renamed to MLTests/2-4_GradientMulInMulOutTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MLTests
44
{
5-
public class GradientMulInMulOutTests
5+
public class GradientMulInMulOutTest
66
{
77
private double[] MulVV(double[] vecA, double[] vecB)
88
{

MLTests/GradientLearnWithAlphaTests.cs renamed to MLTests/2-5_GradientLearnWithAlphaTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace MLTests
55
{
6-
public class GradientLearnWithAlphaTests
6+
public class GradientLearnWithAlphaTest
77
{
88
private double LearnWeightByGradientAlpha(double weight, double input, double predictionGoal, double alpha, int iterations)
99
{

MLTests/NSteerLightReluTests.cs renamed to MLTests/3-3_NSteerLightReluTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace MLTests
66
{
7-
public class NSteerLightReluTests
7+
public class NSteerLightReluTest
88
{
99
private NDarray Relu(NDarray layer)
1010
{

MLTests/NDLActivationTathDigitalRec.cs renamed to MLTests/3-4_NDLActivationTathDigitalRecTest.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Keras.Datasets;
1+
using FluentAssertions;
2+
using Keras.Datasets;
23
using Numpy;
4+
using NUnit.Framework;
35

46
namespace MLTests
57
{
@@ -41,7 +43,7 @@ public void TrainOnMNISTDataWhithTath()
4143

4244
np.random.seed(1);
4345

44-
var (alpha, iterations, hidden_size) = (2.0f, 30, 100);
46+
var (alpha, iterations, hidden_size) = (2.0f, 30, 100);
4547
var (pixels_per_image, num_labels) = (784, 10);
4648
var batch_size = 100;
4749

@@ -50,7 +52,7 @@ public void TrainOnMNISTDataWhithTath()
5052

5153
for (var i = 0; i < iterations; i++)
5254
{
53-
for (var imageId = 0; imageId < images.len/batch_size; ++imageId)
55+
for (var imageId = 0; imageId < images.len / batch_size; ++imageId)
5456
{
5557
var (batch_start, batch_end) = (imageId * batch_size, (imageId + 1) * batch_size);
5658
var layer_0 = images[$"{batch_start}:{batch_end}"];
@@ -90,4 +92,17 @@ public int PredictNumber(int testId)
9092
return result.Select((e, index) => new { e, index }).OrderBy(e => e.e).Last().index;
9193
}
9294
}
95+
96+
public class NDLActivationTathDigitalRecTest
97+
{
98+
[Test]
99+
public void ShouldPredictByUsingTathActivationFunc()
100+
{
101+
var recognizer = new NDLActivationTathDigitalRec(Path.GetFullPath(@"D:/projects.active/LearnML/data/mnist.npz"));
102+
recognizer.TrainOnMNISTDataWhithTath();
103+
var result = recognizer.PredictNumber(0);
104+
result.Should().Be(7);
105+
}
106+
107+
}
93108
}

MLTests/MatrixTests.cs renamed to MLTests/3_MatrixTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace MLTests
66
{
7-
public class MatrixTests
7+
public class MatrixTest
88
{
99
[Test]
1010
public void ShouldMatrixBe()

MLTests/ImageToVector.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

MLTests/ImageToVectorTest.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

MLTests/MulInMulOut.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)