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

Added support for the 'log1p' op and its VJP. #145

Merged
merged 10 commits into from
May 31, 2019
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
14 changes: 14 additions & 0 deletions Sources/TensorFlow/Operators/Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,20 @@ internal func _vjpLog<T: TensorFlowFloatingPoint>(
return (log(x), { v in v / x })
}

/// Computes the logarithm of `1 + x` element-wise.
@inlinable
@differentiable(vjp: _vjpLog1p)
public func log1p<T: TensorFlowFloatingPoint>(_ x: Tensor<T>) -> Tensor<T> {
Raw.log1p(x)
}

@inlinable
func _vjpLog1p<T: TensorFlowFloatingPoint>(
_ x: Tensor<T>
) -> (Tensor<T>, (Tensor<T>) -> Tensor<T>) {
(log1p(x), { v in Raw.xdivy(v, 1 + x) })
}

/// Computes `sin` of the specified tensor element-wise.
@inlinable
@differentiable(vjp: _vjpSin(_:) where T: TensorFlowFloatingPoint)
Expand Down
7 changes: 7 additions & 0 deletions Tests/TensorFlowTests/OperatorTests/MathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import XCTest
@testable import TensorFlow

final class MathOperatorTests: XCTestCase {
func testLog1p() {
let x = Tensor<Float>([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])
let y = log1p(x)
assertEqual(y, log(1 + x), accuracy: 0.0001)
}

func testSign() {
let x = Tensor<Float>([[1, 2, -3, 4, 5], [1, 2, 3, 4, -5]])
let y = sign(x)
Expand Down Expand Up @@ -217,6 +223,7 @@ final class MathOperatorTests: XCTestCase {
}

static var allTests = [
("testLog1p", testLog1p),
("testSign", testSign),
("testReduction", testReduction),
("testArgmax", testArgmax),
Expand Down