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

Implementing MISH activation; Fixes issue #191 #224

Merged
merged 2 commits into from
May 7, 2020
Merged
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
3 changes: 2 additions & 1 deletion src/enums/ActivationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export enum ActivationType {
HardTanhActivation,
AbsoluteActivation,
InverseActivation,
SELUActivation
SELUActivation,
MISHActivation,
}
39 changes: 37 additions & 2 deletions src/methods/Activation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ abstract class Activation {
return new InverseActivation();
case ActivationType.SELUActivation:
return new SELUActivation();
case ActivationType.MISHActivation:
return new MISHActivation();
}
throw new ReferenceError(activationType + " is not the name of any available activations! These are all available activations: " + ALL_ACTIVATIONS);
}
Expand Down Expand Up @@ -454,6 +456,37 @@ class SELUActivation implements Activation {
}
}

/**
* MISH: Self Regularized Non-Monotonic Activation Function
*
* @see {@link https://github.com/digantamisra98/Mish Neural Networks}
*
* @param x Input value to activation function
* @param derivative Flag to select derivative function
*
* @example
* let { methods, Node } = require("@liquid-carrot/carrot");
*
* // Changing a neuron's activation function
* let A = new Node();
* A.squash = new MISHActivation();
*/
class MISHActivation implements Activation {
public readonly type: ActivationType = ActivationType.MISHActivation;

public calc(x: number, derivative: boolean = false): number {
const ex: number = Math.exp(x);

if (derivative) {
const w: number = ex * ex * ex + 4 * (ex * ex + x * ex + x + 1) + 6 * ex;
const d: number = 2 * ex + ex * ex + 2;
return ex * w / (d * d);
} else {
return x * Math.tanh(Math.log(1 + ex));
}
}
}

const ALL_ACTIVATIONS: ActivationType[] = [
ActivationType.LogisticActivation,
ActivationType.TanhActivation,
Expand All @@ -469,7 +502,8 @@ const ALL_ACTIVATIONS: ActivationType[] = [
ActivationType.HardTanhActivation,
ActivationType.AbsoluteActivation,
ActivationType.InverseActivation,
ActivationType.SELUActivation
ActivationType.SELUActivation,
ActivationType.MISHActivation
];

export {
Expand All @@ -489,5 +523,6 @@ export {
HardTanhActivation,
AbsoluteActivation,
InverseActivation,
SELUActivation
SELUActivation,
MISHActivation
};
20 changes: 19 additions & 1 deletion test/units/methods/ActivationTest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {expect} from "chai";
import {
AbsoluteActivation,
BentIdentityActivation,
Expand All @@ -8,6 +9,7 @@ import {
IdentityActivation,
InverseActivation,
LogisticActivation,
MISHActivation,
RELUActivation,
SELUActivation,
SinusoidActivation,
Expand All @@ -16,7 +18,6 @@ import {
TanhActivation
} from "../../../src/methods/Activation";
import {randDouble} from "../../../src/methods/Utils";
import {expect} from "chai";

describe("Activation", () => {
describe("activation.LOGISTIC()", () => {
Expand Down Expand Up @@ -179,4 +180,21 @@ describe("Activation", () => {
expect(new SELUActivation().calc(x, true)).to.be.closeTo(z, 0.01);
});
});
describe("activation.MISH()", () => {
it("activation.MISH(number, derivate=false) => {number}", () => {
const x: number = randDouble(-50, 50);
const z: number = x * Math.tanh(Math.log(1 + Math.exp(x)));
expect(new MISHActivation().calc(x, false)).to.be.closeTo(z, 0.01);
});
it("activation.MISH(number, derivate=true) => {number}", () => {
const x: number = randDouble(-50, 50);

const ex: number = Math.exp(x);
const w: number = ex * ex * ex + 4 * (ex * ex + x * ex + x + 1) + 6 * ex;
const d: number = 2 * ex + ex * ex + 2;
const z: number = ex * w / (d * d);

expect(new MISHActivation().calc(x, true)).to.be.closeTo(z, 0.01);
});
});
});