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

Add derivative for 'padded(forSizes:with:)'. #184

Merged
merged 13 commits into from
Jun 10, 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
21 changes: 21 additions & 0 deletions Sources/TensorFlow/Operators/Basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ extension Tensor where Scalar: TensorFlowFloatingPoint {
public extension Tensor where Scalar: Numeric {
/// Returns a padded tensor according to the specified padding sizes.
@inlinable
@differentiable(wrt: self, vjp: _vjpPadded(forSizes:with:) where Scalar: TensorFlowFloatingPoint)
func padded(forSizes sizes: [(before: Int, after: Int)], with value: Scalar = 0) -> Tensor {
let paddings = Tensor<Int32>(
shape: [sizes.count, 2],
Expand All @@ -682,6 +683,26 @@ public extension Tensor where Scalar: Numeric {
}
}

internal extension Tensor where Scalar: TensorFlowFloatingPoint {
@inlinable
func _vjpPadded(
forSizes sizes: [(before: Int, after: Int)],
with value: Scalar
) -> (Tensor, (Tensor) -> Tensor) {
let result = padded(forSizes: sizes, with: value)
return (result, { [rank = rankTensor, shape = shapeTensor] v in
let paddings = Tensor<Int32>(
shape: [sizes.count, 2],
scalars: sizes.flatMap { [Int32($0.before), Int32($0.after)] })
let padBefore = Raw.slice(paddings,
begin: Tensor<Int32>([0, 0]),
size: Tensor<Int32>(stacking: [rank, Tensor<Int32>(1)]))
let begin = padBefore.reshaped(to: [-1])
return Raw.slice(v, begin: begin, size: shape)
})
}
}

//===------------------------------------------------------------------------------------------===//
// Indexing and Slicing
//===------------------------------------------------------------------------------------------===//
Expand Down
19 changes: 19 additions & 0 deletions Tests/TensorFlowTests/OperatorTests/BasicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ final class BasicOperatorTests: XCTestCase {
XCTAssertEqual(y, Tensor<Float>([3.0, 6.0]))
}

func testPadded() {
let x = Tensor<Float>(ones: [2, 2])
let target = Tensor<Float>([[3, 3, 3], [1, 1, 3], [1, 1, 3]])
let paddedTensor = x.padded(forSizes: [(1, 0), (0, 1)], with: 3.0)
XCTAssertEqual(paddedTensor, target)
}

func testVJPPadded() {
let x = Tensor<Float>(ones: [3, 2])
let target = Tensor<Float>([[2, 2], [2, 2], [2, 2]])
let grads = x.gradient { a -> Tensor<Float> in
let paddedTensor = a.padded(forSizes: [(1, 0), (0, 1)], with: 3.0)
return (paddedTensor * paddedTensor).sum()
}
XCTAssertEqual(grads, target)
}

func testElementIndexing() {
// NOTE: cannot test multiple `Tensor.shape` or `Tensor.scalars` directly
// until send and receive are implemented (without writing a bunch of mini
Expand Down Expand Up @@ -468,6 +485,8 @@ final class BasicOperatorTests: XCTestCase {

static var allTests = [
("testGathering", testGathering),
("testPadded", testPadded),
("testVJPPadded", testVJPPadded),
("testElementIndexing", testElementIndexing),
("testElementIndexingAssignment", testElementIndexingAssignment),
("testNestedElementIndexing", testNestedElementIndexing),
Expand Down