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

Added support for the 'sign' op and its VJP. #144

Merged
merged 13 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from 10 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
13 changes: 13 additions & 0 deletions Sources/TensorFlow/Operators/Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,19 @@ internal func _vjpFloor<T: TensorFlowFloatingPoint>(
return (floor(x), { _ in Tensor(0).broadcasted(like: x) })
}

@inlinable
@differentiable(vjp: _vjpSign(_:) where T: TensorFlowFloatingPoint)
public func sign<T: TensorFlowNumeric>(_ x: Tensor<T>) -> Tensor<T> {
return Raw.sign(x)
}

@inlinable
internal func _vjpSign<T: TensorFlowFloatingPoint>(
_ x: Tensor<T>
) -> (Tensor<T>, (Tensor<T>) -> Tensor<T>) {
return (sign(x), { v in Tensor<T>(zerosLike: x) })
}

/// Computes the sigmoid of the specified tensor element-wise.
/// Specifically, computes `1 / (1 + exp(-x))`.
@inlinable
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 testSign() {
let x = Tensor<Float>([[1, 2, -3, 4, 5], [1, 2, 3, 4, -5]])
let y = sign(x)
assertEqual(y, Tensor<Float>([[1, 1, -1, 1, 1], [1, 1, 1, 1, -1]]))
}

func testReduction() {
// 2 x 5
let x = Tensor<Float>([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])
Expand Down Expand Up @@ -200,6 +206,7 @@ final class MathOperatorTests: XCTestCase {
}

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