Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Mish activation function - Ready for merge #94

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions NeuralNetwork.NET/APIs/Enums/ActivationType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace NeuralNetworkNET.APIs.Enums
namespace NeuralNetworkNET.APIs.Enums
{
/// <summary>
/// Indicates an activation function to use in a neural network
Expand Down Expand Up @@ -59,6 +59,14 @@ public enum ActivationType : byte
/// <summary>
/// A linear activation function that just returns the input value
/// </summary>
Identity
Identity,

/// <summary>
/// The Mish function, proposed by Diganta Misra (https://arxiv.org/abs/1908.08681)
/// Definition: x tanh(ln(1 + e^2))
/// Implimentation: x * Tanh(Softplus(x))
/// </summary>
Mish

}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using JetBrains.Annotations;
using NeuralNetworkNET.APIs.Enums;
using NeuralNetworkNET.Networks.Activations.Delegates;
Expand Down Expand Up @@ -28,7 +28,8 @@ public static (ActivationFunction, ActivationFunction) GetActivations(Activation
case ActivationType.Softmax: return (ActivationFunctions.Softmax, null);
case ActivationType.Softplus: return (ActivationFunctions.Softplus, ActivationFunctions.Sigmoid);
case ActivationType.ELU: return (ActivationFunctions.ELU, ActivationFunctions.ELUPrime);
case ActivationType.Identity: return (ActivationFunctions.Identity, ActivationFunctions.Identityprime);
case ActivationType.Identity: return (ActivationFunctions.Identity, ActivationFunctions.IdentityPrime);
case ActivationType.Mish: return (ActivationFunctions.Mish, ActivationFunctions.MishPrime);
default:
throw new ArgumentOutOfRangeException(nameof(ActivationType), "Unsupported activation function");
}
Expand Down
28 changes: 26 additions & 2 deletions NeuralNetwork.NET/Networks/Activations/ActivationFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;

Expand Down Expand Up @@ -217,6 +217,30 @@ public static float Softplus(float x)
[PublicAPI]
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Identityprime(float x) => 1;
public static float IdentityPrime(float x) => 1;

/// <summary>
/// Applies the Mish function
/// </summary>
/// <param name="x">The input to process</param>
[PublicAPI]
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Mish(float x) => x * Tanh(Softplus(x));

/// <summary>
/// Applies the Mish Derivative function
/// </summary>
/// <param name="x">The input to process</param>
[PublicAPI]
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float MishPrime(float x)
{
float
s = 2 * (float)Math.Exp(x) + (float)Math.Exp(2 * x) + 2,
w = 4 * (x + 1) + (4 * ((float)Math.Exp(2 * x))) + (float)Math.Exp(3 * x) + (float)Math.Exp(x) * (4 * x + 6);
return (float)Math.Exp(x) * w / (s * s);
}
}
}