Skip to content

Commit b579427

Browse files
authored
Merge pull request #78 from Sergio0694/feature_improved-APIs
Feature improved APIs
2 parents ceb73ad + 76ad93c commit b579427

Some content is hidden

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

44 files changed

+365
-286
lines changed

NeuralNetwork.NET/APIs/CuDnnNetworkLayers.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using NeuralNetworkNET.APIs.Enums;
66
using NeuralNetworkNET.APIs.Structs;
77
using NeuralNetworkNET.Extensions;
8-
using NeuralNetworkNET.Networks.Activations;
98
using NeuralNetworkNET.Networks.Layers.Cuda;
109

1110
namespace NeuralNetworkNET.APIs
@@ -45,7 +44,7 @@ public static bool IsCudaSupportAvailable
4544
[PublicAPI]
4645
[Pure, NotNull]
4746
public static LayerFactory FullyConnected(
48-
int neurons, ActivationFunctionType activation,
47+
int neurons, ActivationType activation,
4948
WeightsInitializationMode weightsMode = WeightsInitializationMode.GlorotUniform, BiasInitializationMode biasMode = BiasInitializationMode.Zero)
5049
=> input => new CuDnnFullyConnectedLayer(input, neurons, activation, weightsMode, biasMode);
5150

@@ -72,7 +71,7 @@ public static LayerFactory Softmax(
7271
[PublicAPI]
7372
[Pure, NotNull]
7473
public static LayerFactory Convolutional(
75-
(int X, int Y) kernel, int kernels, ActivationFunctionType activation,
74+
(int X, int Y) kernel, int kernels, ActivationType activation,
7675
BiasInitializationMode biasMode = BiasInitializationMode.Zero)
7776
=> input => new CuDnnConvolutionalLayer(input, ConvolutionInfo.Default, kernel, kernels, activation, biasMode);
7877

@@ -87,17 +86,32 @@ public static LayerFactory Convolutional(
8786
[PublicAPI]
8887
[Pure, NotNull]
8988
public static LayerFactory Convolutional(
90-
ConvolutionInfo info, (int X, int Y) kernel, int kernels, ActivationFunctionType activation,
89+
ConvolutionInfo info, (int X, int Y) kernel, int kernels, ActivationType activation,
9190
BiasInitializationMode biasMode = BiasInitializationMode.Zero)
9291
=> input => new CuDnnConvolutionalLayer(input, info, kernel, kernels, activation, biasMode);
9392

93+
/// <summary>
94+
/// Creates a convolutional layer with the desired number of kernels
95+
/// </summary>
96+
/// <param name="factory">The <see cref="ConvolutionInfoFactory"/> instance to create a <see cref="ConvolutionInfo"/> value to use</param>
97+
/// <param name="kernel">The volume information of the kernels used in the layer</param>
98+
/// <param name="kernels">The number of convolution kernels to apply to the input volume</param>
99+
/// <param name="activation">The desired activation function to use in the network layer</param>
100+
/// <param name="biasMode">Indicates the desired initialization mode to use for the layer bias values</param>
101+
[PublicAPI]
102+
[Pure, NotNull]
103+
public static LayerFactory Convolutional(
104+
[NotNull] ConvolutionInfoFactory factory, (int X, int Y) kernel, int kernels, ActivationType activation,
105+
BiasInitializationMode biasMode = BiasInitializationMode.Zero)
106+
=> input => new CuDnnConvolutionalLayer(input, factory(input, kernel), kernel, kernels, activation, biasMode);
107+
94108
/// <summary>
95109
/// Creates a pooling layer with a window of size 2 and a stride of 2
96110
/// </summary>
97111
/// <param name="activation">The desired activation function to use in the network layer</param>
98112
[PublicAPI]
99113
[Pure, NotNull]
100-
public static LayerFactory Pooling(ActivationFunctionType activation) => input => new CuDnnPoolingLayer(input, PoolingInfo.Default, activation);
114+
public static LayerFactory Pooling(ActivationType activation) => input => new CuDnnPoolingLayer(input, PoolingInfo.Default, activation);
101115

102116
/// <summary>
103117
/// Creates a pooling layer with a custom mode, window size and stride
@@ -106,7 +120,7 @@ public static LayerFactory Convolutional(
106120
/// <param name="activation">The desired activation function to use in the network layer</param>
107121
[PublicAPI]
108122
[Pure, NotNull]
109-
public static LayerFactory Pooling(PoolingInfo info, ActivationFunctionType activation) => input => new CuDnnPoolingLayer(input, info, activation);
123+
public static LayerFactory Pooling(PoolingInfo info, ActivationType activation) => input => new CuDnnPoolingLayer(input, info, activation);
110124

111125
/// <summary>
112126
/// Creates a new inception layer with the given features
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using NeuralNetworkNET.APIs.Structs;
2+
3+
namespace NeuralNetworkNET.APIs.Delegates
4+
{
5+
/// <summary>
6+
/// A <see langword="delegate"/> that calculates a <see cref="ConvolutionInfo"/> value from the input <see cref="TensorInfo"/> instances
7+
/// </summary>
8+
/// <param name="input">The <see cref="TensorInfo"/> instance that describes the input shape</param>
9+
/// <param name="kernel">The size info on the convolutional kernels to apply to the input data</param>
10+
public delegate ConvolutionInfo ConvolutionInfoFactory(TensorInfo input, (int X, int Y) kernel);
11+
}

NeuralNetwork.NET/Networks/Activations/ActivationFunctionType.cs renamed to NeuralNetwork.NET/APIs/Enums/ActivationType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
namespace NeuralNetworkNET.Networks.Activations
1+
namespace NeuralNetworkNET.APIs.Enums
22
{
33
/// <summary>
44
/// Indicates an activation function to use in a neural network
55
/// </summary>
6-
public enum ActivationFunctionType : byte
6+
public enum ActivationType : byte
77
{
88
/// <summary>
99
/// The sigmoid function, 1 / (1 + e^(-x))

NeuralNetwork.NET/APIs/NetworkLayers.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using NeuralNetworkNET.APIs.Delegates;
33
using NeuralNetworkNET.APIs.Enums;
44
using NeuralNetworkNET.APIs.Structs;
5-
using NeuralNetworkNET.Networks.Activations;
65
using NeuralNetworkNET.Networks.Cost;
76
using NeuralNetworkNET.Networks.Layers.Cpu;
87

@@ -23,7 +22,7 @@ public static class NetworkLayers
2322
[PublicAPI]
2423
[Pure, NotNull]
2524
public static LayerFactory FullyConnected(
26-
int neurons, ActivationFunctionType activation,
25+
int neurons, ActivationType activation,
2726
WeightsInitializationMode weightsMode = WeightsInitializationMode.GlorotUniform, BiasInitializationMode biasMode = BiasInitializationMode.Zero)
2827
=> input => new FullyConnectedLayer(input, neurons, activation, weightsMode, biasMode);
2928

@@ -38,7 +37,7 @@ public static LayerFactory FullyConnected(
3837
[PublicAPI]
3938
[Pure, NotNull]
4039
public static LayerFactory FullyConnected(
41-
int neurons, ActivationFunctionType activation, CostFunctionType cost,
40+
int neurons, ActivationType activation, CostFunctionType cost,
4241
WeightsInitializationMode weightsMode = WeightsInitializationMode.GlorotUniform, BiasInitializationMode biasMode = BiasInitializationMode.Zero)
4342
=> input => new OutputLayer(input, neurons, activation, cost, weightsMode, biasMode);
4443

@@ -65,7 +64,7 @@ public static LayerFactory Softmax(
6564
[PublicAPI]
6665
[Pure, NotNull]
6766
public static LayerFactory Convolutional(
68-
(int X, int Y) kernel, int kernels, ActivationFunctionType activation,
67+
(int X, int Y) kernel, int kernels, ActivationType activation,
6968
BiasInitializationMode biasMode = BiasInitializationMode.Zero)
7069
=> input => new ConvolutionalLayer(input, ConvolutionInfo.Default, kernel, kernels, activation, biasMode);
7170

@@ -75,6 +74,6 @@ public static LayerFactory Convolutional(
7574
/// <param name="activation">The desired activation function to use in the network layer</param>
7675
[PublicAPI]
7776
[Pure, NotNull]
78-
public static LayerFactory Pooling(ActivationFunctionType activation) => input => new PoolingLayer(input, PoolingInfo.Default, activation);
77+
public static LayerFactory Pooling(ActivationType activation) => input => new PoolingLayer(input, PoolingInfo.Default, activation);
7978
}
8079
}

NeuralNetwork.NET/APIs/AccuracyTesters.cs renamed to NeuralNetwork.NET/APIs/Settings/AccuracyTesters.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
using NeuralNetworkNET.APIs.Delegates;
44
using NeuralNetworkNET.Extensions;
55

6-
namespace NeuralNetworkNET.APIs
6+
namespace NeuralNetworkNET.APIs.Settings
77
{
88
/// <summary>
99
/// A static class exposing different <see cref="AccuracyTester"/> options to monitor the accuracy of a neural network
1010
/// </summary>
1111
public static class AccuracyTesters
1212
{
1313
/// <summary>
14-
/// Gets an <see cref="AccuracyTester"/> <see lanfword="delegate"/> that can be used classification problems with mutually-exclusive classes
14+
/// Gets an <see cref="AccuracyTester"/> <see langword="delegate"/> that can be used classification problems with mutually-exclusive classes
1515
/// </summary>
1616
[PublicAPI]
1717
[Pure, NotNull]

NeuralNetwork.NET/APIs/NetworkSettings.cs renamed to NeuralNetwork.NET/APIs/Settings/NetworkSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using JetBrains.Annotations;
44
using NeuralNetworkNET.APIs.Delegates;
55

6-
namespace NeuralNetworkNET.APIs
6+
namespace NeuralNetworkNET.APIs.Settings
77
{
88
/// <summary>
99
/// A static class with some shared settings for the library

NeuralNetwork.NET/APIs/Structs/ConvolutionInfo.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Newtonsoft.Json;
44
using System;
55
using System.Runtime.CompilerServices;
6+
using NeuralNetworkNET.APIs.Delegates;
67

78
namespace NeuralNetworkNET.APIs.Structs
89
{
@@ -78,6 +79,28 @@ public static ConvolutionInfo New(
7879
int verticalStride = 1, int horizontalStride = 1)
7980
=> new ConvolutionInfo(mode, verticalPadding, horizontalPadding, verticalStride, horizontalStride);
8081

82+
/// <summary>
83+
/// Creates a new <see cref="ConvolutionInfoFactory"/> instance that returns a <see cref="ConvolutionInfo"/> value
84+
/// with the appropriate padding to keep the input size the same after the specified convolution operation
85+
/// </summary>
86+
/// <param name="mode">The desired convolution mode to use</param>
87+
/// <param name="verticalStride">The convolution vertical stride size</param>
88+
/// <param name="horizontalStride">The convolution horizontal stride size</param>
89+
[PublicAPI]
90+
[Pure]
91+
public static ConvolutionInfoFactory Same(
92+
ConvolutionMode mode = ConvolutionMode.Convolution,
93+
int verticalStride = 1, int horizontalStride = 1)
94+
{
95+
return (input, kernels) =>
96+
{
97+
int
98+
verticalPadding = (input.Height * verticalStride - input.Height + kernels.X - verticalStride - 1) / 2 + 1,
99+
horizontalPadding = (input.Width * horizontalStride - input.Width + kernels.Y - horizontalStride - 1) / 2 + 1;
100+
return new ConvolutionInfo(mode, verticalPadding, horizontalPadding, verticalStride, horizontalStride);
101+
};
102+
}
103+
81104
#endregion
82105

83106
/// <summary>

NeuralNetwork.NET/Extensions/SpanExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public static unsafe int Argmax(this Span<float> span)
152152
/// <param name="x1">The first <see cref="Span{T}"/> instance to check</param>
153153
/// <param name="x2">The second <see cref="Span{T}"/> instance to check</param>
154154
/// <param name="threshold">The target threshold</param>
155-
/// <remarks>This method is <see langword="internal"/> as it's meant to be exposed through the <see cref="APIs.AccuracyTesters"/> class only</remarks>
155+
/// <remarks>This method is <see langword="internal"/> as it's meant to be exposed through the <see cref="APIs.Settings.AccuracyTesters"/> class only</remarks>
156156
[Pure]
157157
[CollectionAccess(CollectionAccessType.Read)]
158158
internal static unsafe bool MatchElementwiseThreshold(this Span<float> x1, Span<float> x2, float threshold)
@@ -171,7 +171,7 @@ internal static unsafe bool MatchElementwiseThreshold(this Span<float> x1, Span<
171171
/// <param name="x1">The first <see cref="Span{T}"/> instance to check</param>
172172
/// <param name="x2">The second <see cref="Span{T}"/> instance to check</param>
173173
/// <param name="threshold">The target maximum distance</param>
174-
/// <remarks>This method is <see langword="internal"/> as it's meant to be exposed through the <see cref="APIs.AccuracyTesters"/> class only</remarks>
174+
/// <remarks>This method is <see langword="internal"/> as it's meant to be exposed through the <see cref="APIs.Settings.AccuracyTesters"/> class only</remarks>
175175
[Pure]
176176
[CollectionAccess(CollectionAccessType.Read)]
177177
internal static unsafe bool IsCloseTo(this Span<float> x1, Span<float> x2, float threshold)

NeuralNetwork.NET/Helpers/ImageLoader.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.IO;
32
using JetBrains.Annotations;
43
using SixLabors.ImageSharp;
54
using SixLabors.ImageSharp.Advanced;

0 commit comments

Comments
 (0)