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

Commit 16374ad

Browse files
eaplataniosrxwei
authored andcommitted
Added support for the 'expm1' op and its VJP. (#146)
1 parent 8b699d9 commit 16374ad

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Sources/TensorFlow/Operators/Math.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,21 @@ internal func _vjpExp<T: TensorFlowFloatingPoint>(
569569
return (value, { v in value * v })
570570
}
571571

572+
/// Computes the exponential of `x - 1` element-wise.
573+
@inlinable
574+
@differentiable(vjp: _vjpExpm1)
575+
public func expm1<T: TensorFlowFloatingPoint>(_ x: Tensor<T>) -> Tensor<T> {
576+
Raw.expm1(x)
577+
}
578+
579+
@inlinable
580+
internal func _vjpExpm1<T: TensorFlowFloatingPoint>(
581+
_ x: Tensor<T>
582+
) -> (Tensor<T>, (Tensor<T>) -> Tensor<T>) {
583+
let y = expm1(x)
584+
return (y, { v in v * y })
585+
}
586+
572587
/// Returns the values of the specified tensor rounded to the nearest integer, element-wise.
573588
@inlinable
574589
@differentiable(vjp: _vjpRound)

Tests/TensorFlowTests/OperatorTests/MathTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ final class MathOperatorTests: XCTestCase {
2222
assertEqual(y, log(1 + x), accuracy: 0.0001)
2323
}
2424

25+
func testExpm1() {
26+
let x = Tensor<Float>([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])
27+
let y = expm1(x)
28+
assertEqual(y, exp(x - 1), accuracy: 0.0001)
29+
}
30+
2531
func testSign() {
2632
let x = Tensor<Float>([[1, 2, -3, 4, 5], [1, 2, 3, 4, -5]])
2733
let y = sign(x)
@@ -224,6 +230,7 @@ final class MathOperatorTests: XCTestCase {
224230

225231
static var allTests = [
226232
("testLog1p", testLog1p),
233+
("testExpm1", testExpm1),
227234
("testSign", testSign),
228235
("testReduction", testReduction),
229236
("testArgmax", testArgmax),

0 commit comments

Comments
 (0)