Skip to content

Commit 17b522b

Browse files
committed
Softmax layer refactoring
1 parent 46d2b64 commit 17b522b

File tree

6 files changed

+16
-45
lines changed

6 files changed

+16
-45
lines changed

NeuralNetwork.NET/APIs/TrainingAlgorithmInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class TrainingAlgorithmsInfo
1515
/// <param name="lambda">The lambda regularization parameter</param>
1616
[PublicAPI]
1717
[Pure, NotNull]
18-
public static StochasticGradientDescentInfo CreateForStochasticGradientDescent(float eta = 0.1f, float lambda = 0f) => new StochasticGradientDescentInfo(eta, lambda);
18+
public static StochasticGradientDescentInfo StochasticGradientDescent(float eta = 0.1f, float lambda = 0f) => new StochasticGradientDescentInfo(eta, lambda);
1919

2020
/// <summary>
2121
/// Gets an instance implementing <see cref="Interfaces.ITrainingAlgorithmInfo"/> for the <see cref="SupervisedLearning.Algorithms.TrainingAlgorithmType.Adadelta"/> algorithm
@@ -25,6 +25,6 @@ public static class TrainingAlgorithmsInfo
2525
/// <param name="l2">An optional L2 regularization parameter</param>
2626
[PublicAPI]
2727
[Pure, NotNull]
28-
public static AdadeltaInfo CreateForAdadelta(float rho = 0.95f, float epsilon = 1e-8f, float l2 = 0f) => new AdadeltaInfo(rho, epsilon, l2);
28+
public static AdadeltaInfo Adadelta(float rho = 0.95f, float epsilon = 1e-8f, float l2 = 0f) => new AdadeltaInfo(rho, epsilon, l2);
2929
}
3030
}

NeuralNetwork.NET/Extensions/MatrixExtensions.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,6 @@ namespace NeuralNetworkNET.Extensions
1313
/// </summary>
1414
public static class MatrixExtensions
1515
{
16-
#region Activation
17-
18-
/// <summary>
19-
/// Performs the softmax normalization on the input matrix, dividing every value by the sum of all the values
20-
/// </summary>
21-
/// <param name="m">The matrix to normalize</param>
22-
internal static unsafe void InPlaceSoftmaxNormalization(in this Tensor m)
23-
{
24-
// Setup
25-
int h = m.Entities, w = m.Length;
26-
Tensor.New(1, h, out Tensor partials);
27-
float* pp = partials, pm = m;
28-
29-
// Partial sum
30-
void PartialSum(int i)
31-
{
32-
int offset = i * w;
33-
float sum = 0;
34-
for (int j = 0; j < w; j++)
35-
sum += pm[offset + j];
36-
pp[i] = sum;
37-
}
38-
Parallel.For(0, h, PartialSum).AssertCompleted();
39-
40-
// Normalization of the matrix values
41-
void NormalizationKernel(int i)
42-
{
43-
int offset = i * w;
44-
for (int j = 0; j < w; j++)
45-
pm[offset + j] /= pp[i];
46-
}
47-
Parallel.For(0, h, NormalizationKernel).AssertCompleted();
48-
partials.Free();
49-
}
50-
51-
#endregion
52-
5316
#region Misc
5417

5518
/// <summary>

NeuralNetwork.NET/Networks/Layers/Cpu/SoftmaxLayer.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using NeuralNetworkNET.APIs.Enums;
44
using NeuralNetworkNET.APIs.Interfaces;
55
using NeuralNetworkNET.APIs.Structs;
6+
using NeuralNetworkNET.cpuDNN;
67
using NeuralNetworkNET.Extensions;
78
using NeuralNetworkNET.Networks.Activations;
89
using NeuralNetworkNET.Networks.Cost;
@@ -22,10 +23,17 @@ public SoftmaxLayer(in TensorInfo input, int outputs, WeightsInitializationMode
2223
: base(input, outputs, ActivationFunctionType.Softmax, CostFunctionType.LogLikelyhood, weightsMode, biasMode) { }
2324

2425
/// <inheritdoc/>
25-
public override void Forward(in Tensor x, out Tensor z, out Tensor a)
26+
public override unsafe void Forward(in Tensor x, out Tensor z, out Tensor a)
2627
{
27-
base.Forward(x, out z, out a);
28-
a.InPlaceSoftmaxNormalization();
28+
fixed (float* pw = Weights, pb = Biases)
29+
{
30+
Tensor.Reshape(pw, InputInfo.Size, OutputInfo.Size, out Tensor w);
31+
Tensor.Reshape(pb, 1, Biases.Length, out Tensor b);
32+
Tensor.New(x.Entities, OutputInfo.Size, out z);
33+
CpuDnn.FullyConnectedForward(x, w, b, z);
34+
Tensor.New(z.Entities, z.Length, out a);
35+
CpuDnn.SoftmaxForward(z, a);
36+
}
2937
}
3038

3139
public SoftmaxLayer(in TensorInfo input, int outputs, [NotNull] float[] weights, [NotNull] float[] biases)

Samples/DigitsCudaTest/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static async Task Main()
3636
CancellationTokenSource cts = new CancellationTokenSource();
3737
Console.CancelKeyPress += (s, e) => cts.Cancel();
3838
TrainingSessionResult result = await NetworkManager.TrainNetworkAsync(network, (training.X, training.Y), 20, 400,
39-
TrainingAlgorithmsInfo.CreateForAdadelta(), 0.5f,
39+
TrainingAlgorithmsInfo.Adadelta(), 0.5f,
4040
new Progress<BatchProgress>(p =>
4141
{
4242
Console.SetCursorPosition(0, Console.CursorTop);

Samples/DigitsTest/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static async Task Main()
2121
NetworkLayers.FullyConnected(100, ActivationFunctionType.Sigmoid),
2222
NetworkLayers.FullyConnected(10, ActivationFunctionType.Sigmoid, CostFunctionType.CrossEntropy));
2323
TrainingSessionResult result = await NetworkManager.TrainNetworkAsync(network, (training.X, training.Y), 60, 10,
24-
TrainingAlgorithmsInfo.CreateForStochasticGradientDescent(), 0.5f,
24+
TrainingAlgorithmsInfo.StochasticGradientDescent(), 0.5f,
2525
testParameters: new TestParameters(test, new Progress<BackpropagationProgressEventArgs>(p =>
2626
{
2727
Printf($"Epoch {p.Iteration}, cost: {p.Result.Cost}, accuracy: {p.Result.Accuracy}");

Unit/NeuralNetwork.NET.Unit/NetworkTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void GradientDescentTest1()
9191
NeuralNetwork network = NetworkManager.NewSequential(TensorInfo.CreateForGrayscaleImage(28, 28),
9292
NetworkLayers.FullyConnected(100, ActivationFunctionType.Sigmoid),
9393
NetworkLayers.Softmax(10)).To<INeuralNetwork, NeuralNetwork>();
94-
TrainingSessionResult result = NetworkTrainer.TrainNetwork(network, batches, 4, 0, TrainingAlgorithmsInfo.CreateForStochasticGradientDescent(), null, null, null, null, default);
94+
TrainingSessionResult result = NetworkTrainer.TrainNetwork(network, batches, 4, 0, TrainingAlgorithmsInfo.StochasticGradientDescent(), null, null, null, null, default);
9595
Assert.IsTrue(result.StopReason == TrainingStopReason.EpochsCompleted);
9696
(_, _, float accuracy) = network.Evaluate(testSet);
9797
Assert.IsTrue(accuracy > 80);

0 commit comments

Comments
 (0)