Skip to content

Commit

Permalink
add mish activation function
Browse files Browse the repository at this point in the history
  • Loading branch information
dcato98 committed Jul 6, 2020
1 parent 4848931 commit 8d468ad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ <h1 class="l--page">Tinker With a <b>Neural Network</b> <span class="optional">R
<option value="sigmoid">Sigmoid</option>
<option value="linear">Linear</option>
<option value="sine">Sine</option>
<option value="mish">Mish</option>
</select>
</div>
</div>
Expand Down
23 changes: 23 additions & 0 deletions src/nn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Modified by David Cato
==============================================================================*/

/**
Expand Down Expand Up @@ -110,6 +112,19 @@ export class Errors {
}
};

/** Polyfill for SOFTPLUS */
(Math as any).softplus = (Math as any).softplus || function(x) {
let threshold = 20;
let beta = 1;
if (x * beta > threshold) {
return x;
} else if (x === -Infinity) {
return 0;
} else {
return 1 / beta * (Math as any).log(1 + (Math as any).exp(beta * x));
}
};

/** Built-in activation functions */
export class Activations {
public static TANH: ActivationFunction = {
Expand Down Expand Up @@ -138,6 +153,14 @@ export class Activations {
output: x => (Math as any).sin(x),
der: x => (Math as any).cos(x)
};
public static MISH: ActivationFunction = {
output: x => x * Activations.TANH.output((Math as any).softplus(x)),
der: x => {
let sig_x = Activations.SIGMOID.output(x);
let tanh_sp_x = Activations.TANH.output((Math as any).softplus(x));
return tanh_sp_x * x * sig_x * (1 - tanh_sp_x * tanh_sp_x);
}
};
}

/** Build-in regularization functions */
Expand Down
5 changes: 4 additions & 1 deletion src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Modified by David Cato
==============================================================================*/

import * as nn from "./nn";
Expand All @@ -25,7 +27,8 @@ export let activations: {[key: string]: nn.ActivationFunction} = {
"tanh": nn.Activations.TANH,
"sigmoid": nn.Activations.SIGMOID,
"linear": nn.Activations.LINEAR,
"sine": nn.Activations.SINE
"sine": nn.Activations.SINE,
"mish": nn.Activations.MISH
};

/** A map between names and regularization functions. */
Expand Down

0 comments on commit 8d468ad

Please sign in to comment.