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

add mish activation function #1068

Merged
merged 1 commit into from
Sep 2, 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
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