-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathRelu.cs
More file actions
46 lines (40 loc) · 1.32 KB
/
Relu.cs
File metadata and controls
46 lines (40 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace Algorithms.Numeric;
/// <summary>
/// Implementation of the Rectified Linear Unit (ReLU) function.
/// ReLU is defined as: ReLU(x) = max(0, x).
/// It is commonly used as an activation function in neural networks.
/// </summary>
public static class Relu
{
/// <summary>
/// Compute the Rectified Linear Unit (ReLU) for a single value.
/// </summary>
/// <param name="input">The input real number.</param>
/// <returns>The output real number (>= 0).</returns>
public static double Compute(double input)
{
return Math.Max(0.0, input);
}
/// <summary>
/// Compute the Rectified Linear Unit (ReLU) element-wise for a vector.
/// </summary>
/// <param name="input">The input vector of real numbers.</param>
/// <returns>The output vector where each element is max(0, input[i]).</returns>
public static double[] Compute(double[] input)
{
if (input is null)
{
throw new ArgumentNullException(nameof(input));
}
if (input.Length == 0)
{
throw new ArgumentException("Array is empty.");
}
var output = new double[input.Length];
for (var i = 0; i < input.Length; i++)
{
output[i] = Math.Max(0.0, input[i]);
}
return output;
}
}