-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Heaton
committed
Aug 31, 2017
1 parent
626a729
commit f49a817
Showing
7 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.DS_Store | ||
*.suo | ||
ConsoleExamples/bin/ | ||
ConsoleExamples/obj/ | ||
|
109 changes: 109 additions & 0 deletions
109
encog-core-cs/Engine/Network/Activation/ActivationReLU.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Encog.Engine.Network.Activation | ||
{ | ||
/// <summary> | ||
/// ReLU activation function. This function has a high and low threshold. If | ||
/// the high threshold is exceeded a fixed value is returned.Likewise, if the | ||
/// low value is exceeded another fixed value is returned. | ||
/// </summary> | ||
[Serializable] | ||
public class ActivationReLU: IActivationFunction | ||
{ | ||
|
||
/// <summary> | ||
/// The ramp low threshold parameter. | ||
/// </summary> | ||
public const int PARAM_RELU_LOW_THRESHOLD = 0; | ||
|
||
/// <summary> | ||
/// The ramp low parameter. | ||
/// </summary> | ||
public const int PARAM_RELU_LOW = 0; | ||
|
||
/// <summary> | ||
/// The parameters. | ||
/// </summary> | ||
private readonly double[] _params; | ||
|
||
|
||
|
||
|
||
/// <summary> | ||
/// Default constructor. | ||
/// </summary> | ||
public ActivationReLU(): | ||
this(0,0) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Construct a ramp activation function. | ||
/// </summary> | ||
/// <param name="thresholdLow">The low threshold value.</param> | ||
/// <param name="low">The low value, replaced if the low threshold is exceeded.</param> | ||
public ActivationReLU(double thresholdLow, double low) | ||
{ | ||
_params = new double[2]; | ||
_params[ActivationReLU.PARAM_RELU_LOW_THRESHOLD] = thresholdLow; | ||
_params[ActivationReLU.PARAM_RELU_LOW] = low; | ||
} | ||
|
||
|
||
|
||
/// <returns>Return true, Rectifier has a derivative.</returns> | ||
public bool HasDerivative | ||
{ | ||
get | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Clone the object. | ||
/// </summary> | ||
/// <returns>The cloned object.</returns> | ||
public object Clone() | ||
{ | ||
return new ActivationReLU(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void ActivationFunction(double[] x, int start, int size) | ||
{ | ||
for (int i = start; i < start + size; i++) | ||
{ | ||
if (x[i] <= _params[ActivationReLU.PARAM_RELU_LOW_THRESHOLD]) { | ||
x[i] = _params[ActivationReLU.PARAM_RELU_LOW]; | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public double DerivativeFunction(double b, double a) | ||
{ | ||
return 1 / (1 + Math.Pow(Math.E, -a)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual String[] ParamNames | ||
{ | ||
get | ||
{ | ||
String[] result = { }; | ||
return result; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual double[] Params | ||
{ | ||
get { return _params; } | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
encog-core-test/Engine/Network/Activation/TestActivationReLU.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Encog.Engine.Network.Activation | ||
{ | ||
[TestClass] | ||
public class TestActivatioReLU | ||
{ | ||
[TestMethod] | ||
public void TestRELU() | ||
{ | ||
var activation = new ActivationReLU(); | ||
Assert.IsTrue(activation.HasDerivative); | ||
|
||
var clone = (ActivationReLU)activation.Clone(); | ||
Assert.IsNotNull(clone); | ||
|
||
double[] input = { 0.0 }; | ||
|
||
activation.ActivationFunction(input, 0, 1); | ||
|
||
Assert.AreEqual(0.0, input[0], EncogFramework.DefaultDoubleEqual); | ||
|
||
// test derivative, wiki says this is logistic function (test may be wrong - jeroldhaas) | ||
input[0] = activation.DerivativeFunction(input[0], input[0]); | ||
Assert.AreEqual(0.5, input[0], EncogFramework.DefaultDoubleEqual); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters