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

Restructured the operators source files and added support for multiple new operators. #70

Merged
merged 60 commits into from
Apr 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
9885f38
Re-organized the operators source files.
eaplatanios Apr 1, 2019
3ce90c0
Added support for 'stacked', 'concatenated', 'gathered', 'batchGather…
eaplatanios Apr 1, 2019
19a3add
Reverted back to 4-space tabs.
eaplatanios Apr 1, 2019
b3f6281
Made some other minor changes.
eaplatanios Apr 1, 2019
111d96c
Added support or 'selecting'.
eaplatanios Apr 1, 2019
371021b
Added support for 'nonZeroIndices'.
eaplatanios Apr 1, 2019
112707b
Minor edits.
eaplatanios Apr 1, 2019
3594e0e
Addressed Richard's feedback.
eaplatanios Apr 1, 2019
adf20eb
Addressed Richard's comments.
eaplatanios Apr 1, 2019
4ae4e08
Addressed Richard's comments.
eaplatanios Apr 2, 2019
b0aba5d
Updated the convolution ops to support explicit paddings.
eaplatanios Apr 2, 2019
05704d0
Small edits.
eaplatanios Apr 2, 2019
a686a76
Updated the convolution ops to support explicit paddings.
eaplatanios Apr 2, 2019
cc46658
Small fix.
eaplatanios Apr 2, 2019
8494c04
Small fix.
eaplatanios Apr 2, 2019
976061f
Added a new tensor initializer from ranges of tensors.
eaplatanios Apr 2, 2019
5dfaaee
Added documentation string for the "explicit" padding scheme.
eaplatanios Apr 2, 2019
eda9514
More fixes.
eaplatanios Apr 3, 2019
77c1385
Merge branch 'conv-fix' of github.com:eaplatanios/swift-apis into con…
eaplatanios Apr 3, 2019
b41c8c4
Some fixes.
eaplatanios Apr 3, 2019
aed430a
Added 'zerosLike' and 'onesLike' tensor initializers.
eaplatanios Apr 12, 2019
5a093a8
Added a new 'stacking' tensor initializer and made some compatibility…
eaplatanios Apr 15, 2019
78a3eab
Merge remote-tracking branch 'upstream/master' into working
eaplatanios Apr 15, 2019
467a443
Added a new 'tiling' tensor initializer.
eaplatanios Apr 15, 2019
1faaef4
Minor edit.
eaplatanios Apr 15, 2019
94cf85f
Made some refactoring.
eaplatanios Apr 15, 2019
e0bbfc0
Bug fix.
eaplatanios Apr 15, 2019
b74079d
Merged upstream changes.
eaplatanios Apr 18, 2019
6c04368
Added support for the split op and its VJP.
eaplatanios Apr 19, 2019
ca8ce02
Added VJPs for stacking and tiling.
eaplatanios Apr 19, 2019
26e9123
Added VJP for concatenating.
eaplatanios Apr 19, 2019
87db644
Added the gathering VJP.
eaplatanios Apr 19, 2019
49bfe8d
Bug fixes.
eaplatanios Apr 19, 2019
10de441
Added an 'Optimizable' protocol.
eaplatanios Apr 19, 2019
64159f2
Merged upstream changes.
eaplatanios Apr 19, 2019
a630396
Moved some more activation functions from the stdlib.
eaplatanios Apr 19, 2019
c3243f4
Added log-softmax VJP.
eaplatanios Apr 19, 2019
4547a6d
Minor bug fix.
eaplatanios Apr 20, 2019
19cdbd9
Brought some initializers from stdlib.
eaplatanios Apr 20, 2019
a16d911
Brought some more stuff from the stdlib.
eaplatanios Apr 20, 2019
34b475a
Minor edit.
eaplatanios Apr 20, 2019
86072a4
Moved some more stuff to swift-apis.
eaplatanios Apr 20, 2019
bc0a581
Removed all the newly-added ops.
eaplatanios Apr 20, 2019
a91c00a
Moved some more stuff to swift-apis.
eaplatanios Apr 20, 2019
0ad9843
Moved some more stuff to swift-apis.
eaplatanios Apr 20, 2019
1120692
Added a README file to the 'Operators' source directory.
eaplatanios Apr 20, 2019
e7a04d2
Brought the gradient helper functions from the stdlib.
eaplatanios Apr 20, 2019
3ee21ff
Bug fixes.
eaplatanios Apr 20, 2019
ef1c73b
Brought the tensor tests from the stdlib.
eaplatanios Apr 20, 2019
0e06843
Minor bug fix.
eaplatanios Apr 20, 2019
eb407cf
Addressed Richard's comments.
eaplatanios Apr 20, 2019
f4b7e01
Minor edit.
eaplatanios Apr 20, 2019
b207e42
Reverted the change in the existing optimizer implementations.
eaplatanios Apr 20, 2019
3dcd46d
Added VJPs for some operations.
eaplatanios Apr 20, 2019
5548c56
Incorporated fix from stdlib.
eaplatanios Apr 21, 2019
89fb4e4
Addressed Richard's feedback.
eaplatanios Apr 21, 2019
61eae26
Changed the indentation in the 'PythonConversion.swift' file.
eaplatanios Apr 21, 2019
3cdd808
Changed the indentation in the 'Random.swift' file.
eaplatanios Apr 21, 2019
4b87827
Minor edit.
eaplatanios Apr 21, 2019
a5edd32
Tabs to spaces.
eaplatanios Apr 21, 2019
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
178 changes: 178 additions & 0 deletions Sources/DeepLearning/DifferentialOperators.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright 2018 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if !COMPILING_TENSORFLOW_MODULE
import TensorFlow
#endif

//===------------------------------------------------------------------------------------------===//
// Method-style Differential Operators
//===------------------------------------------------------------------------------------------===//

public extension Differentiable {
@inlinable
func gradient<R: TensorFlowFloatingPoint>(
in f: @differentiable (Self) -> Tensor<R>
) -> CotangentVector {
return self.pullback(in: f)(Tensor<R>(1))
}

@inlinable
func valueWithGradient<R: TensorFlowFloatingPoint>(
in f: @differentiable (Self) -> Tensor<R>
) -> (value: Tensor<R>, gradient: CotangentVector) {
let (y, pb) = self.valueWithPullback(in: f)
return (y, pb(Tensor<R>(1)))
}

@inlinable
func gradient<T: Differentiable, R: TensorFlowFloatingPoint>(
at x: T,
in f: @differentiable (Self, T) -> Tensor<R>
) -> (CotangentVector, T.CotangentVector) {
return self.pullback(at: x, in: f)(Tensor<R>(1))
}

@inlinable
func valueWithGradient<T: Differentiable, R: TensorFlowFloatingPoint>(
at x: T,
in f: @differentiable (Self, T) -> Tensor<R>
) -> (value: Tensor<R>, gradient: (CotangentVector, T.CotangentVector)) {
let (y, pb) = self.valueWithPullback(at: x, in: f)
return (y, pb(Tensor<R>(1)))
}
}

//===------------------------------------------------------------------------------------------===//
// Free-Function-Style Differential Operators
//===------------------------------------------------------------------------------------------===//

// Value with gradient

@inlinable
public func valueWithGradient<T, R>(
at x: T,
in f: @differentiable (T) -> Tensor<R>
) -> (value: Tensor<R>, gradient: T.CotangentVector)
where T: Differentiable, R: TensorFlowFloatingPoint {
let (y, pullback) = valueWithPullback(at: x, in: f)
return (y, pullback(Tensor<R>(1)))
}

@inlinable
public func valueWithGradient<T, U, R>(
at x: T,
_ y: U,
in f: @differentiable (T, U) -> Tensor<R>
) -> (value: Tensor<R>, gradient: (T.CotangentVector, U.CotangentVector))
where T: Differentiable, U: Differentiable, R: TensorFlowFloatingPoint {
let (y, pullback) = valueWithPullback(at: x, y, in: f)
return (y, pullback(Tensor<R>(1)))
}

@inlinable
public func valueWithGradient<T, U, V, R>(
at x: T,
_ y: U,
_ z: V,
in f: @differentiable (T, U, V) -> Tensor<R>
) -> (value: Tensor<R>, gradient: (T.CotangentVector, U.CotangentVector, V.CotangentVector))
where T: Differentiable, U: Differentiable, V: Differentiable, R: TensorFlowFloatingPoint {
let (y, pullback) = valueWithPullback(at: x, y, z, in: f)
return (y, pullback(Tensor<R>(1)))
}

// Value with gradient (curried)

@inlinable
public func valueWithGradient<T, R>(
of f: @escaping @differentiable (T) -> Tensor<R>
) -> (T) -> (value: Tensor<R>, gradient: T.CotangentVector)
where T: Differentiable, R: TensorFlowFloatingPoint {
return { x in valueWithGradient(at: x, in: f) }
}

@inlinable
public func valueWithGradient<T, U, R>(
of f: @escaping @differentiable (T, U) -> Tensor<R>
) -> (T, U) -> (value: Tensor<R>, gradient: (T.CotangentVector, U.CotangentVector))
where T: Differentiable, U: Differentiable, R: TensorFlowFloatingPoint {
return { x, y in valueWithGradient(at: x, y, in: f) }
}

@inlinable
public func valueWithGradient<T, U, V, R>(
of f: @escaping @differentiable (T, U, V) -> Tensor<R>
) -> (T, U, V) -> (
value: Tensor<R>,
gradient: (T.CotangentVector, U.CotangentVector, V.CotangentVector))
where T: Differentiable, U: Differentiable, V: Differentiable, R: TensorFlowFloatingPoint {
return { x, y, z in valueWithGradient(at: x, y, z, in: f) }
}

// Gradient

@inlinable
public func gradient<T, R>(
at x: T,
in f: @differentiable (T) -> Tensor<R>
) -> T.CotangentVector where T: Differentiable, R: TensorFlowFloatingPoint {
return pullback(at: x, in: f)(Tensor<R>(1))
}

@inlinable
public func gradient<T, U, R>(
at x: T,
_ y: U,
in f: @differentiable (T, U) -> Tensor<R>
) -> (T.CotangentVector, U.CotangentVector)
where T: Differentiable, U: Differentiable, R: TensorFlowFloatingPoint {
return pullback(at: x, y, in: f)(Tensor<R>(1))
}

@inlinable
public func gradient<T, U, V, R>(
at x: T,
_ y: U,
_ z: V,
in f: @differentiable (T, U, V) -> Tensor<R>
) -> (T.CotangentVector, U.CotangentVector, V.CotangentVector)
where T: Differentiable, U: Differentiable, V: Differentiable, R: TensorFlowFloatingPoint {
return pullback(at: x, y, z, in: f)(Tensor<R>(1))
}

// Gradient (curried)

@inlinable
public func gradient<T, R>(
of f: @escaping @differentiable (T) -> Tensor<R>
) -> (T) -> T.CotangentVector where T: Differentiable, R: TensorFlowFloatingPoint {
return { x in gradient(at: x, in: f) }
}

@inlinable
public func gradient<T, U, R>(
of f: @escaping @differentiable (T, U) -> Tensor<R>
) -> (T, U) -> (T.CotangentVector, U.CotangentVector)
where T: Differentiable, U: Differentiable, R: TensorFlowFloatingPoint {
return { x, y in gradient(at: x, y, in: f) }
}

@inlinable
public func gradient<T, U, V, R>(
of f: @escaping @differentiable (T, U, V) -> Tensor<R>
) -> (T, U, V) -> (T.CotangentVector, U.CotangentVector, V.CotangentVector)
where T: Differentiable, U: Differentiable, V: Differentiable, R: TensorFlowFloatingPoint {
return { x, y, z in gradient(at: x, y, z, in: f) }
}
12 changes: 10 additions & 2 deletions Sources/DeepLearning/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@
// limitations under the License.

#if !COMPILING_TENSORFLOW_MODULE
import TensorFlow
@_exported import TensorFlow
#endif

/// Returns a tensor with the same shape and scalars as the specified tensor.
@inlinable
@differentiable
public func identity<Scalar>(_ x: Tensor<Scalar>) -> Tensor<Scalar> {
return x
}

// `pow` is defined in Darwin/Glibc on `Float` and `Double`, but there doesn't exist a generic
// version for `FloatingPoint`.
// This is a manual definition.
func pow<T : BinaryFloatingPoint>(_ x: T, _ y: T) -> T {
@inlinable
func pow<T: BinaryFloatingPoint>(_ x: T, _ y: T) -> T {
return T(pow(Double(x), Double(y)))
}
Loading