-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff] [stdlib] Made arrays differentiable #23183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rxwei
merged 21 commits into
swiftlang:tensorflow
from
eaplatanios:array-differentiable
Apr 17, 2019
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4a5001c
Made arrays differentiable.
eaplatanios 24f026e
Addressed Marc's comment.
eaplatanios 18d3154
Added documentation for the array differentiable conformance.
eaplatanios 4711081
Addressed some of Richard's comments.
eaplatanios d5e1ed9
Had to make 'zipLongest' public due to its use in '@inlinable' functi…
eaplatanios 6ec4b8c
Minor bug fix.
eaplatanios 0aa8024
Addressed Richard's review comments.
eaplatanios 668890b
Addressed some of the code review feedback.
eaplatanios 37ebb0c
Fixed the array differentiable conformance derivation.
eaplatanios 8a278d9
Addressed Marc's comment about 'moved' and 'tangentVector'.
eaplatanios 7a56ef8
Style fixes.
eaplatanios f4b8b8c
Added some tests for array differentiation.
eaplatanios b2614c1
Added a simple array identity differentiation test.
eaplatanios cc6dc5d
Merge remote-tracking branch 'upstream/tensorflow' into array-differe…
eaplatanios da9c160
Merge remote-tracking branch 'upstream/tensorflow' into array-differe…
eaplatanios 6fcd912
Added an 'Array.subscript' VJP.
eaplatanios 0346b54
Merge branch 'array-differentiable' of https://github.com/eaplatanios…
2b656d7
update to use a single generic type for all array diffble assoc types
7d98750
Merge branch 'tensorflow' into eaplatanios-array-differentiable
d3365ea
add missing comment
7f8f273
address comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// RUN: %target-run-simple-swift | ||
|
||
import StdlibUnittest | ||
|
||
var ArrayAutodiffTests = TestSuite("ArrayAutodiff") | ||
|
||
typealias FloatArrayGrad = Array<Float>.CotangentVector | ||
|
||
ArrayAutodiffTests.test("ArrayIdentity") { | ||
func arrayIdentity(_ x: [Float]) -> [Float] { | ||
return x | ||
} | ||
|
||
let backprop = pullback(at: [5, 6, 7, 8], in: arrayIdentity) | ||
expectEqual( | ||
FloatArrayGrad([1, 2, 3, 4]), | ||
backprop(FloatArrayGrad([1, 2, 3, 4]))) | ||
} | ||
|
||
ArrayAutodiffTests.test("ArraySubscript") { | ||
func sumFirstThree(_ array: [Float]) -> Float { | ||
return array[0] + array[1] + array[2] | ||
} | ||
|
||
expectEqual( | ||
FloatArrayGrad([1, 1, 1, 0, 0, 0]), | ||
gradient(at: [2, 3, 4, 5, 6, 7], in: sumFirstThree)) | ||
} | ||
|
||
ArrayAutodiffTests.test("ArrayConcat") { | ||
struct TwoArrays : Differentiable { | ||
let a: [Float] | ||
let b: [Float] | ||
} | ||
|
||
func sumFirstThreeConcatted(_ arrs: TwoArrays) -> Float { | ||
let c = arrs.a + arrs.b | ||
return c[0] + c[1] + c[2] | ||
} | ||
|
||
expectEqual( | ||
TwoArrays.CotangentVector( | ||
a: FloatArrayGrad([1, 1]), | ||
b: FloatArrayGrad([1, 0])), | ||
gradient( | ||
at: TwoArrays(a: [0, 0], b: [0, 0]), | ||
in: sumFirstThreeConcatted)) | ||
expectEqual( | ||
TwoArrays.CotangentVector( | ||
a: FloatArrayGrad([1, 1, 1, 0]), | ||
b: FloatArrayGrad([0, 0])), | ||
gradient( | ||
at: TwoArrays(a: [0, 0, 0, 0], b: [0, 0]), | ||
in: sumFirstThreeConcatted)) | ||
expectEqual( | ||
TwoArrays.CotangentVector( | ||
a: FloatArrayGrad([]), | ||
b: FloatArrayGrad([1, 1, 1, 0])), | ||
gradient( | ||
at: TwoArrays(a: [], b: [0, 0, 0, 0]), | ||
in: sumFirstThreeConcatted)) | ||
} | ||
|
||
ArrayAutodiffTests.test("Array.DifferentiableView.init") { | ||
@differentiable | ||
func constructView(_ x: [Float]) -> Array<Float>.DifferentiableView { | ||
return Array<Float>.DifferentiableView(x) | ||
} | ||
|
||
let backprop = pullback(at: [5, 6, 7, 8], in: constructView) | ||
expectEqual( | ||
FloatArrayGrad([1, 2, 3, 4]), | ||
backprop(FloatArrayGrad([1, 2, 3, 4]))) | ||
} | ||
|
||
ArrayAutodiffTests.test("Array.DifferentiableView.base") { | ||
@differentiable | ||
func accessBase(_ x: Array<Float>.DifferentiableView) -> [Float] { | ||
return x.base | ||
} | ||
|
||
let backprop = pullback( | ||
at: Array<Float>.DifferentiableView([5, 6, 7, 8]), | ||
in: accessBase) | ||
expectEqual( | ||
FloatArrayGrad([1, 2, 3, 4]), | ||
backprop(FloatArrayGrad([1, 2, 3, 4]))) | ||
} | ||
|
||
ArrayAutodiffTests.test("Array.DifferentiableView : KeyPathIterable") { | ||
struct Container : KeyPathIterable { | ||
let a: Array<Float>.DifferentiableView | ||
} | ||
let container = Container(a: Array<Float>.DifferentiableView([1, 2, 3])) | ||
expectEqual( | ||
[1, 2, 3], | ||
container.recursivelyAllKeyPaths(to: Float.self).map { | ||
container[keyPath: $0] | ||
}) | ||
} | ||
|
||
runAllTests() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. How about renaming the current
base
to_base
, and renamingarray
/_vjpArray
tobase
/_vjpBase
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done