Skip to content

Commit 7f1b8fe

Browse files
committed
Added many new layers and many new neural networks as well as redesigning some major parts of the neural network architecture
1 parent 814da8c commit 7f1b8fe

File tree

147 files changed

+17496
-269
lines changed

Some content is hidden

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

147 files changed

+17496
-269
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace AiDotNet.ActivationFunctions;
2+
3+
public class LeakyReLUActivation<T> : ActivationFunctionBase<T>
4+
{
5+
private readonly T _alpha;
6+
7+
public LeakyReLUActivation(double alpha = 0.01)
8+
{
9+
_alpha = NumOps.FromDouble(alpha);
10+
}
11+
12+
protected override bool SupportsScalarOperations() => true;
13+
14+
public override T Activate(T input)
15+
{
16+
return NumOps.GreaterThan(input, NumOps.Zero) ? input : NumOps.Multiply(_alpha, input);
17+
}
18+
19+
public override Vector<T> Activate(Vector<T> input)
20+
{
21+
return input.Transform(x => Activate(x));
22+
}
23+
24+
public override T Derivative(T input)
25+
{
26+
return NumOps.GreaterThan(input, NumOps.Zero) ? NumOps.One : _alpha;
27+
}
28+
29+
public override Matrix<T> Derivative(Vector<T> input)
30+
{
31+
int size = input.Length;
32+
Matrix<T> jacobian = new Matrix<T>(size, size);
33+
34+
for (int i = 0; i < size; i++)
35+
{
36+
for (int j = 0; j < size; j++)
37+
{
38+
if (i == j)
39+
{
40+
jacobian[i, j] = Derivative(input[i]);
41+
}
42+
else
43+
{
44+
jacobian[i, j] = NumOps.Zero;
45+
}
46+
}
47+
}
48+
49+
return jacobian;
50+
}
51+
}
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
namespace AiDotNet.ActivationFunctions;
2+
3+
public class SignActivation<T> : ActivationFunctionBase<T>
4+
{
5+
protected override bool SupportsScalarOperations() => true;
6+
7+
public override T Activate(T input)
8+
{
9+
if (NumOps.LessThan(input, NumOps.Zero))
10+
return NumOps.FromDouble(-1);
11+
else if (NumOps.GreaterThan(input, NumOps.Zero))
12+
return NumOps.One;
13+
else
14+
return NumOps.Zero;
15+
}
16+
17+
public override T Derivative(T input)
18+
{
19+
// The derivative of the sign function is 0 everywhere except at 0,
20+
// where it's undefined. We'll return 0 for all inputs.
21+
return NumOps.Zero;
22+
}
23+
24+
public override Vector<T> Activate(Vector<T> input)
25+
{
26+
Vector<T> output = new Vector<T>(input.Length);
27+
for (int i = 0; i < input.Length; i++)
28+
{
29+
output[i] = Activate(input[i]);
30+
}
31+
32+
return output;
33+
}
34+
35+
public override Matrix<T> Derivative(Vector<T> input)
36+
{
37+
int n = input.Length;
38+
Matrix<T> jacobian = new Matrix<T>(n, n);
39+
// The Jacobian matrix will be all zeros
40+
for (int i = 0; i < n; i++)
41+
{
42+
for (int j = 0; j < n; j++)
43+
{
44+
jacobian[i, j] = NumOps.Zero;
45+
}
46+
}
47+
48+
return jacobian;
49+
}
50+
51+
public override Tensor<T> Activate(Tensor<T> input)
52+
{
53+
Tensor<T> output = new Tensor<T>(input.Shape);
54+
int totalElements = input.Shape.Aggregate(1, (a, b) => a * b);
55+
56+
for (int i = 0; i < totalElements; i++)
57+
{
58+
output[i] = Activate(input[i]);
59+
}
60+
61+
return output;
62+
}
63+
64+
public override Tensor<T> Derivative(Tensor<T> input)
65+
{
66+
int[] outputShape = new int[input.Shape.Length + 1];
67+
Array.Copy(input.Shape, outputShape, input.Shape.Length);
68+
outputShape[outputShape.Length - 1] = input.Shape[input.Shape.Length - 1];
69+
70+
Tensor<T> output = new Tensor<T>(outputShape);
71+
int batchSize = input.Shape[0];
72+
int vectorLength = input.Shape[1];
73+
74+
for (int i = 0; i < batchSize; i++)
75+
{
76+
Vector<T> vector = new Vector<T>(vectorLength);
77+
for (int j = 0; j < vectorLength; j++)
78+
{
79+
vector[j] = input[i, j];
80+
}
81+
82+
Matrix<T> jacobian = Derivative(vector);
83+
84+
for (int j = 0; j < vectorLength; j++)
85+
{
86+
for (int k = 0; k < vectorLength; k++)
87+
{
88+
output[i, j, k] = jacobian[j, k];
89+
}
90+
}
91+
}
92+
93+
return output;
94+
}
95+
}

src/Enums/ActivationType.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace AiDotNet.Enums;
2+
3+
public enum ActivationType
4+
{
5+
Softmax,
6+
Sigmoid,
7+
ReLU,
8+
Tanh,
9+
Other
10+
}

src/Enums/InputType.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AiDotNet.Enums;
2+
3+
public enum InputType
4+
{
5+
OneDimensional,
6+
TwoDimensional,
7+
ThreeDimensional
8+
}

src/Enums/NetworkComplexity.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace AiDotNet.Enums;
2+
3+
/// <summary>
4+
/// Defines the complexity level of a neural network architecture.
5+
/// </summary>
6+
public enum NetworkComplexity
7+
{
8+
/// <summary>
9+
/// Simple network with minimal layers, suitable for basic tasks.
10+
/// </summary>
11+
Simple,
12+
13+
/// <summary>
14+
/// Medium complexity network with a moderate number of layers.
15+
/// </summary>
16+
Medium,
17+
18+
/// <summary>
19+
/// Deep network with many layers, suitable for complex tasks.
20+
/// </summary>
21+
Deep,
22+
23+
/// <summary>
24+
/// Very deep network with extensive layers and connections.
25+
/// </summary>
26+
VeryDeep,
27+
28+
/// <summary>
29+
/// Custom complexity defined by the user.
30+
/// </summary>
31+
Custom
32+
}

src/Enums/NeuralNetworkTaskType.cs

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
namespace AiDotNet.Enums;
2+
3+
/// <summary>
4+
/// Defines the different types of tasks that a neural network can be designed to perform.
5+
/// </summary>
6+
public enum NeuralNetworkTaskType
7+
{
8+
/// <summary>
9+
/// Binary classification task (two classes)
10+
/// </summary>
11+
BinaryClassification,
12+
13+
/// <summary>
14+
/// Multi-class classification task (more than two classes)
15+
/// </summary>
16+
MultiClassClassification,
17+
18+
/// <summary>
19+
/// Multi-label classification task (multiple labels can be assigned)
20+
/// </summary>
21+
MultiLabelClassification,
22+
23+
/// <summary>
24+
/// Regression task (predicting continuous values)
25+
/// </summary>
26+
Regression,
27+
28+
/// <summary>
29+
/// Sequence-to-sequence task (e.g., machine translation)
30+
/// </summary>
31+
SequenceToSequence,
32+
33+
/// <summary>
34+
/// Sequence-to-sequence classification
35+
/// </summary>
36+
SequenceClassification,
37+
38+
/// <summary>
39+
/// Time series forecasting task
40+
/// </summary>
41+
TimeSeriesForecasting,
42+
43+
/// <summary>
44+
/// Image classification task
45+
/// </summary>
46+
ImageClassification,
47+
48+
/// <summary>
49+
/// Object detection task
50+
/// </summary>
51+
ObjectDetection,
52+
53+
/// <summary>
54+
/// Image segmentation task
55+
/// </summary>
56+
ImageSegmentation,
57+
58+
/// <summary>
59+
/// Natural language processing task
60+
/// </summary>
61+
NaturalLanguageProcessing,
62+
63+
/// <summary>
64+
/// Text generation task
65+
/// </summary>
66+
TextGeneration,
67+
68+
/// <summary>
69+
/// Reinforcement learning task
70+
/// </summary>
71+
ReinforcementLearning,
72+
73+
/// <summary>
74+
/// Anomaly detection task
75+
/// </summary>
76+
AnomalyDetection,
77+
78+
/// <summary>
79+
/// Recommendation system task
80+
/// </summary>
81+
Recommendation,
82+
83+
/// <summary>
84+
/// Clustering task
85+
/// </summary>
86+
Clustering,
87+
88+
/// <summary>
89+
/// Dimensionality reduction task
90+
/// </summary>
91+
DimensionalityReduction,
92+
93+
/// <summary>
94+
/// Generative task (e.g., GANs, VAEs)
95+
/// </summary>
96+
Generative,
97+
98+
/// <summary>
99+
/// Speech recognition task
100+
/// </summary>
101+
SpeechRecognition,
102+
103+
/// <summary>
104+
/// Audio processing task
105+
/// </summary>
106+
AudioProcessing,
107+
108+
/// <summary>
109+
/// Language translation
110+
/// </summary>
111+
Translation,
112+
113+
/// <summary>
114+
/// Custom task type
115+
/// </summary>
116+
Custom
117+
}

src/Enums/SamplingType.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AiDotNet.Enums;
2+
3+
public enum SamplingType
4+
{
5+
Max,
6+
Average,
7+
L2Norm
8+
}

src/Enums/SpikingNeuronType.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace AiDotNet.Enums;
2+
3+
public enum SpikingNeuronType
4+
{
5+
LeakyIntegrateAndFire,
6+
IntegrateAndFire,
7+
Izhikevich,
8+
HodgkinHuxley,
9+
AdaptiveExponential
10+
}

src/Enums/TransformerTaskType.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace AiDotNet.Enums;
2+
3+
public enum TransformerTaskType
4+
{
5+
Classification,
6+
Regression,
7+
TextGeneration,
8+
SequenceTagging,
9+
Translation
10+
}

src/Extensions/SerializationExtensions.cs

+21
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,25 @@ public static object ReadValue(this BinaryReader reader, Type type)
5656
return reader.ReadBoolean();
5757
throw new ArgumentException($"Unsupported type: {type}");
5858
}
59+
60+
public static int[] ReadInt32Array(this BinaryReader reader)
61+
{
62+
int length = reader.ReadInt32();
63+
int[] array = new int[length];
64+
for (int i = 0; i < length; i++)
65+
{
66+
array[i] = reader.ReadInt32();
67+
}
68+
69+
return array;
70+
}
71+
72+
public static void WriteInt32Array(this BinaryWriter writer, int[] array)
73+
{
74+
writer.Write(array.Length);
75+
foreach (int value in array)
76+
{
77+
writer.Write(value);
78+
}
79+
}
5980
}

0 commit comments

Comments
 (0)