This repository was archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAFAnalogSoftMax.cs
59 lines (52 loc) · 1.59 KB
/
AFAnalogSoftMax.cs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
using RCNet.Extensions;
using RCNet.MathTools;
using System;
namespace RCNet.Neural.Activation
{
/// <summary>
/// Implements the Soft Max activation function.
/// </summary>
[Serializable]
public class AFAnalogSoftMax : AFAnalogBase
{
//Constructor
/// <summary>
/// Creates an initialized instance.
/// </summary>
public AFAnalogSoftMax()
: base(Interval.IntZP1)
{
return;
}
//Properties
/// <inheritdoc/>
public override bool DependsOnSorround { get { return true; } }
//Methods
/// <inheritdoc/>
public override double Compute(double x)
{
throw new InvalidOperationException("The wrong version of the Compute method was called. It was called Compute(double) method instead of the Compute(double[], double[]) method. SoftMax method requires the sorroundings.");
}
/// <inheritdoc/>
public override void Compute(double[] xVector, double[] cVector)
{
double maxX = xVector.Max();
double sum = 0;
for (int i = 0; i < xVector.Length; i++)
{
cVector[i] = Math.Exp(xVector[i] - maxX);
sum += cVector[i];
}
for (int i = 0; i < xVector.Length; i++)
{
cVector[i] /= sum;
}
return;
}
/// <inheritdoc/>
public override double ComputeDerivative(double c, double x)
{
return 1d;
}
}//AFAnalogSoftMax
}//Namespace