Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
add mish activation function (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettkoonce authored Sep 2, 2020
1 parent 897bac9 commit c1d8225
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sources/TensorFlow/Operators/Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,17 @@ public func hardSwish<T: TensorFlowFloatingPoint>(_ x: Tensor<T>) -> Tensor<T> {
x * hardSigmoid(x)
}

/// Returns a tensor by applying the mish activation function, namely
/// `x * tanh(softplus(x))`.
///
/// Source: "Mish: A Self Regularized Non-Monotonic Neural Activation Function"
/// https://arxiv.org/abs/1908.08681
@inlinable
@differentiable
public func mish<T: TensorFlowFloatingPoint>(_ x: Tensor<T>) -> Tensor<T> {
x * tanh(softplus(x))
}

extension Tensor where Scalar: TensorFlowFloatingPoint {
/// Returns a boolean tensor indicating which elements of `x` are finite.
@inlinable public var isFinite: Tensor<Bool> { _Raw.isFinite(self) }
Expand Down
8 changes: 8 additions & 0 deletions Tests/TensorFlowTests/OperatorTests/MathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ final class MathOperatorTests: XCTestCase {
let expectedY = Tensor<Float>([0.0, -0.33333334, 0.0, 1.6666666, 4.0])
assertEqual(y, expectedY, accuracy: 1e-5)
}

func testMish() {
let x = Tensor<Float>([-4, -2, 0, 2, 4])
let y = mish(x)
let expectedY = Tensor<Float>([-0.07259174, -0.25250146, 0.0, 1.943959, 3.9974122])
assertEqual(y, expectedY, accuracy: 1e-5)
}

func testIsFinite() {
let x = Tensor<Float>([1, 2, 3, 4, -Float.infinity])
Expand Down Expand Up @@ -651,6 +658,7 @@ final class MathOperatorTests: XCTestCase {
("testSwish", testSwish),
("testHardSigmoid", testHardSigmoid),
("testHardSwish", testHardSwish),
("testMish", testMish),
("testIsFinite", testIsFinite),
("testIsInfinite", testIsInfinite),
("testIsNaN", testIsNaN),
Expand Down

0 comments on commit c1d8225

Please sign in to comment.