Skip to content
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
2 changes: 1 addition & 1 deletion stdlib/public/TensorFlow/Gradients.swift
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ extension Tensor where Scalar : Differentiable & FloatingPoint {
) -> (Tensor, (Tensor) -> Tensor) {
let value = reshaped(toShape: newShape)
return (value, { v in
return v.reshaped(toShape: newShape)
return v.reshaped(toShape: self.shapeTensor)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

  • We generally don't use self. unless it's required.
  • It'd be more efficient if you make it capture self.shapeTensor explicitly instead of capturing self. I'd write this as
{ [shape = shapeTensor] v in
  return v.reshaped(toShape: shape)
}

Copy link
Contributor

@dan-zheng dan-zheng Jan 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: there are definitely more places where we could use explicit captures instead of capturing self for efficiency. We should change those places at some point!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends on whether self is practically a larger tensor that the result you get from operations on self :)

})
}

Expand Down
12 changes: 10 additions & 2 deletions test/TensorFlowRuntime/tensor_autodiff_runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,24 @@ TensorADTests.testAllBackends("mean") {
expectTrue(meanGradAlongAxes(input) == expected)
}

TensorADTests.testAllBackends("reshaped") {
let shapeTensor = Tensor<Int32>([2, 2, 2])
let input = Tensor<Float>(ones: [2, 4])
let reshapedPullback = pullback(at: input) { (a: Tensor<Float>) in a.reshaped(toShape: shapeTensor) }
let reshaped = Tensor<Float>(ones: [2, 2, 2])
expectTrue(reshapedPullback(reshaped) == input)
}

TensorADTests.testAllBackends("transposed") {
let input = Tensor<Float>(ones: [2, 3])
let transposed = Tensor<Float>(ones: [3, 2])
let transposedPullback = pullback(at: input) { (a: Tensor<Float>) in a.transposed() }
let transposedPermutationsPullback = pullback(at: input) { (a: Tensor<Float>) in a.transposed(withPermutations: [1, 0]) }
let transposedVariadiicsPullback = pullback(at: input) { (a: Tensor<Float>) in a.transposed(withPermutations: 1, 0) }
let transposedVariadicsPullback = pullback(at: input) { (a: Tensor<Float>) in a.transposed(withPermutations: 1, 0) }

expectTrue(transposedPullback(transposed) == input)
expectTrue(transposedPermutationsPullback(transposed) == input)
expectTrue(transposedVariadiicsPullback(transposed) == input)
expectTrue(transposedVariadicsPullback(transposed) == input)
}

TensorADTests.testAllBackends("relu") {
Expand Down