-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff] Add Differentiable.zeroTangentVectorInitializer
.
#28310
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
dan-zheng
merged 1 commit into
swiftlang:tensorflow
from
dan-zheng:zero-tangent-initialization
Nov 17, 2019
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,17 +36,40 @@ public protocol Differentiable { | |
mutating func move(along direction: TangentVector) | ||
|
||
// SWIFT_ENABLE_TENSORFLOW | ||
/// A tangent vector such that `move(along: zeroTangentVector)` will not | ||
/// modify `self`. | ||
/// - Note: `zeroTangentVector` can be `TangentVector.zero` in most cases, | ||
/// but types whose tangent vectors depend on instance properties of `self` | ||
/// need to provide a different implementation. For example, the tangent | ||
/// vector of an `Array` depends on the array's `count`. | ||
/// A closure that produces a zero tangent vector, capturing minimal | ||
/// necessary information from `self`. | ||
/// | ||
/// `move(along: zeroTangentVectorInitializer())` should not modify | ||
/// `self`. | ||
/// | ||
/// In some cases, the zero tangent vector of `self` is equal to | ||
/// `TangentVector.zero`. In other cases, the zero tangent vector depends on | ||
/// information in `self`, such as shape for an n-dimensional array type. | ||
/// For differentiable programming, it is more memory-efficient to define a | ||
/// custom `zeroTangentVectorInitializer` property which returns a closure | ||
/// that captures and uses only the necessary information to create a zero | ||
/// tangent vector. For example: | ||
/// | ||
/// struct Vector { | ||
/// var scalars: [Float] | ||
/// var count: Int { scalars.count } | ||
/// init(repeating repeatedElement: Float, count: Int) { ... } | ||
/// } | ||
/// | ||
/// extension Vector: Differentiable { | ||
/// typealias TangentVector = Vector | ||
/// | ||
/// @noDerivative | ||
/// var zeroTangentVectorInitializer: () -> TangentVector { | ||
/// let count = self.count | ||
/// return { TangentVector(repeating: 0, count: count) } | ||
/// } | ||
/// } | ||
@available(*, deprecated, message: """ | ||
`zeroTangentVector` derivation has not been implemented; do not use \ | ||
this property | ||
`zeroTangentVectorInitializer` derivation has not been implemented; do \ | ||
not use this property | ||
""") | ||
var zeroTangentVector: TangentVector { get } | ||
var zeroTangentVectorInitializer: () -> TangentVector { get } | ||
// SWIFT_ENABLE_TENSORFLOW END | ||
} | ||
|
||
|
@@ -59,12 +82,18 @@ public extension Differentiable where TangentVector == Self { | |
|
||
// SWIFT_ENABLE_TENSORFLOW | ||
public extension Differentiable { | ||
// This is a temporary solution that allows us to add `zeroTangentVector` | ||
// without implementing derived conformances. This property is marked | ||
// unavailable because it will produce incorrect results when tangent vectors | ||
// depend on instance properties of `self`. | ||
// FIXME: Implement derived conformance and remove this default | ||
// This is a temporary solution enabling the addition of | ||
// `zeroTangentVectorInitializer` without implementing derived conformances. | ||
// This property will produce incorrect results when tangent vectors depend | ||
// on instance-specific information from `self`. | ||
// FIXME: Implement derived conformances and remove this default | ||
// implementation. | ||
var zeroTangentVector: TangentVector { .zero } | ||
var zeroTangentVectorInitializer: () -> TangentVector { | ||
{ TangentVector.zero } | ||
} | ||
|
||
/// A tangent vector initialized using `zeroTangentVectorInitializer`. | ||
/// `move(along: zeroTangentVector)` should not modify `self`. | ||
var zeroTangentVector: TangentVector { zeroTangentVectorInitializer() } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, there's a availability warning when compiling the standard library:
I think this is acceptable for now. |
||
} | ||
// SWIFT_ENABLE_TENSORFLOW END |
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.
Eventually this property declaration (in the protocol) should have a
@noDerivative
on it, and potentially require all conforming types to specify a@noDerivative
... when we support differentiable curry.