Skip to content

Commit 221f20f

Browse files
committed
add tath activation example
1 parent dc4f9c5 commit 221f20f

File tree

5 files changed

+130
-7
lines changed

5 files changed

+130
-7
lines changed

DigitalRecognizer/NDLDigitalRecognizer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Keras.Datasets;
22
using Numpy;
33
using System.Linq;
4+
using System.Text.RegularExpressions;
45

56
namespace DigitalRecognizer
67
{
@@ -35,21 +36,26 @@ public void TrainOnMNISTData()
3536

3637
np.random.seed(1);
3738

38-
var (alpha, iterations, hidden_size, pixels_per_image, num_labels) = (0.005f, 30, 40, 784, 10);
39+
var (alpha, iterations, hidden_size, pixels_per_image, num_labels) = (0.005f, 30, 10, 784, 10);
3940

4041
_weights01 = 0.2f * np.random.rand(pixels_per_image, hidden_size) - 0.1f;
4142
_weights12 = 0.2f * np.random.rand(hidden_size, num_labels) - 0.1f;
4243

44+
//Regex.IsMatch("))))dfdf",@"(;|:)(~|-)?(\)|D)");
4345
for (var i = 0; i < iterations; i++)
4446
{
4547
for (var imageId = 0; imageId < images.len; ++imageId)
4648
{
4749
var layer_0 = images[$"{imageId}:{imageId + 1}"];
4850
var layer_1 = Relu(np.dot(layer_0, _weights01));
51+
var dropout_mask = np.random.randint(2, null, layer_1.shape.Dimensions);
52+
layer_1 *= dropout_mask * 2;
4953
var layer_2 = np.dot(layer_1, _weights12);
5054

5155
var layer_2_delta = labels[$"{imageId}:{imageId + 1}"] - layer_2;
5256
var layer_1_delta = layer_2_delta.dot(_weights12.T) * ReluToDerivative(layer_1);
57+
layer_1_delta *= dropout_mask;
58+
5359

5460
_weights12 += alpha * layer_1.T.dot(layer_2_delta);
5561
_weights01 += alpha * layer_0.T.dot(layer_1_delta);

LearnML.sln

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ VisualStudioVersion = 17.3.32922.545
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MLTests", "MLTests\MLTests.csproj", "{8B0D33BC-74F2-4E4A-8415-07026E3624E4}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalRecognizer", "DigitalRecognizer\DigitalRecognizer.csproj", "{72FC2029-F95B-4059-A156-55A1BB113AFB}"
9-
EndProject
108
Global
119
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1210
Debug|Any CPU = Debug|Any CPU
@@ -17,10 +15,6 @@ Global
1715
{8B0D33BC-74F2-4E4A-8415-07026E3624E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
1816
{8B0D33BC-74F2-4E4A-8415-07026E3624E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
1917
{8B0D33BC-74F2-4E4A-8415-07026E3624E4}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{72FC2029-F95B-4059-A156-55A1BB113AFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{72FC2029-F95B-4059-A156-55A1BB113AFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{72FC2029-F95B-4059-A156-55A1BB113AFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{72FC2029-F95B-4059-A156-55A1BB113AFB}.Release|Any CPU.Build.0 = Release|Any CPU
2418
EndGlobalSection
2519
GlobalSection(SolutionProperties) = preSolution
2620
HideSolutionNode = FALSE
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using Keras.Datasets;
2+
using Numpy;
3+
4+
namespace MLTests
5+
{
6+
public class NDLActivationTathDigitalRec
7+
{
8+
private NDarray _weights01;
9+
private NDarray _weights12;
10+
private string _dataPath;
11+
12+
private static NDarray Relu(NDarray layer) => layer * (layer > 0);
13+
private static NDarray ReluToDerivative(NDarray layer) => np.where(layer > 0, np.array(1), np.array(0));
14+
private static NDarray Tanh(NDarray layer) => np.tanh(layer);
15+
private static NDarray TanhToDerivative(NDarray layer) => 1 - np.power(layer, np.array(2));
16+
private static NDarray Softmax(NDarray layer)
17+
{
18+
var temp = np.exp(layer);
19+
return temp / np.sum(temp, axis: 1, keepdims: true);
20+
}
21+
22+
public NDLActivationTathDigitalRec(string dataPath)
23+
{
24+
_weights01 = np.array(0f);
25+
_weights12 = np.array(0f);
26+
_dataPath = dataPath;
27+
}
28+
29+
public void TrainOnMNISTDataWhithTath()
30+
{
31+
var ((x_train, y_train), (_, _)) = MNIST.LoadData(_dataPath);
32+
33+
var (images, labels) = (x_train["0:1000"].reshape(1000, 28 * 28) / 255, y_train["0:1000"]);
34+
var one_hot_labels = np.zeros(labels.len, 10);
35+
for (var id = 0; id < labels.len; id++)
36+
{
37+
var label = labels[id];
38+
one_hot_labels[id][label] = np.array(1);
39+
}
40+
labels = one_hot_labels;
41+
42+
np.random.seed(1);
43+
44+
var (alpha, iterations, hidden_size) = (2.0f, 30, 100);
45+
var (pixels_per_image, num_labels) = (784, 10);
46+
var batch_size = 100;
47+
48+
_weights01 = 0.02f * np.random.rand(pixels_per_image, hidden_size) - 0.01f;
49+
_weights12 = 0.2f * np.random.rand(hidden_size, num_labels) - 0.1f;
50+
51+
for (var i = 0; i < iterations; i++)
52+
{
53+
for (var imageId = 0; imageId < images.len/batch_size; ++imageId)
54+
{
55+
var (batch_start, batch_end) = (imageId * batch_size, (imageId + 1) * batch_size);
56+
var layer_0 = images[$"{batch_start}:{batch_end}"];
57+
var layer_1 = Tanh(np.dot(layer_0, _weights01));
58+
var dropout_mask = np.random.randint(2, null, layer_1.shape.Dimensions);
59+
layer_1 *= dropout_mask * 2;
60+
var layer_2 = Softmax(np.dot(layer_1, _weights12));
61+
62+
var layer_2_delta = (labels[$"{batch_start}:{batch_end}"] - layer_2) / (batch_size * layer_2.shape[0]);
63+
var layer_1_delta = layer_2_delta.dot(_weights12.T) * TanhToDerivative(layer_1);
64+
layer_1_delta *= dropout_mask;
65+
66+
67+
_weights12 += alpha * layer_1.T.dot(layer_2_delta);
68+
_weights01 += alpha * layer_0.T.dot(layer_1_delta);
69+
}
70+
}
71+
}
72+
73+
public int PredictNumber(int testId)
74+
{
75+
var ((x_train, y_train), (x_test, y_test)) = MNIST.LoadData(_dataPath);
76+
77+
var test_images = x_test.reshape(x_test.len, 28 * 28) / 255;
78+
var test_labels = np.zeros(y_test.len, 10);
79+
for (var id = 0; id < y_test.len; id++)
80+
{
81+
var test = y_test[id];
82+
test_labels[id][test] = np.array(1);
83+
}
84+
85+
var layer_0 = test_images[$"{testId}:{testId + 1}"];
86+
var layer_1 = Tanh(np.dot(layer_0, _weights01));
87+
var layer_2 = np.dot(layer_1, _weights12);
88+
var result = layer_2.GetData<double>();
89+
var max = result.Max();
90+
return result.Select((e, index) => new { e, index }).OrderBy(e => e.e).Last().index;
91+
}
92+
}
93+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using FluentAssertions;
2+
using NUnit.Framework;
3+
4+
namespace MLTests
5+
{
6+
public class NDLActivationTathDigitalRecTests
7+
{
8+
[Test]
9+
public void ShouldPredictByUsingTathActivationFunc()
10+
{
11+
var recognizer = new NDLActivationTathDigitalRec(Path.GetFullPath(@"D:/projects.active/LearnML/data/mnist.npz"));
12+
recognizer.TrainOnMNISTDataWhithTath();
13+
var result = recognizer.PredictNumber(0);
14+
result.Should().Be(7);
15+
}
16+
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MLTests
8+
{
9+
internal class NDLConvolutionDigitalRecTest
10+
{
11+
}
12+
}

0 commit comments

Comments
 (0)