In [ ]:
// #@title Licensed under the Apache License, Version 2.0 (the "License"); { display-mode: "form" }
// 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
//
// https://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.
Building on TensorFlow, Swift for TensorFlow takes a fresh approach to API design. APIs are carefully curated from established libraries and combined with new language idioms. This means that not all TensorFlow APIs will be directly available as Swift APIs, and our API curation needs time and dedicated effort to evolve. However, do not worry if your favorite TensorFlow operator is not available in Swift -- the TensorFlow Swift library gives you transparent access to most TensorFlow operators, under the _Raw
namespace.
Import TensorFlow
to get started.
In [ ]:
import TensorFlow
In [ ]:
_Raw.mul(Tensor([2.0, 3.0]), Tensor([5.0, 6.0]))
Multiply is already available as operator *
on Tensor
, but let us pretend that we wanted to make it available under a new name as .*
. Swift allows you to retroactively add methods or computed properties to existing types using extension
declarations.
Now, let us add .*
to Tensor
by declaring an extension and make it available when the tensor's Scalar
type conforms to Numeric
.
In [ ]:
infix operator .* : MultiplicationPrecedence
extension Tensor where Scalar: Numeric {
static func .* (_ lhs: Tensor, _ rhs: Tensor) -> Tensor {
return _Raw.mul(lhs, rhs)
}
}
let x: Tensor<Double> = [[1.0, 2.0], [3.0, 4.0]]
let y: Tensor<Double> = [[8.0, 7.0], [6.0, 5.0]]
x .* y
Not only can you easily define a Swift API for a raw TensorFlow operator, you can also make it differentiable to work with Swift's first-class automatic differentiation.
To make .*
differentiable, use the @derivative
attribute on the derivative function and specify the original function as an attribute argument under the of:
label. Since the .*
operator is defined when the generic type Scalar
conforms to Numeric
, it is not enough for making Tensor<Scalar>
conform to the Differentiable
protocol. Born with type safety, Swift will remind us to add a generic constraint on the @differentiable
attribute to require Scalar
to conform to TensorFlowFloatingPoint
protocol, which would make Tensor<Scalar>
conform to Differentiable
.
@differentiable(where Scalar: TensorFlowFloatingPoint)
In [ ]:
infix operator .* : MultiplicationPrecedence
extension Tensor where Scalar: Numeric {
@differentiable(where Scalar: TensorFlowFloatingPoint)
static func .* (_ lhs: Tensor, _ rhs: Tensor) -> Tensor {
return _Raw.mul(lhs, rhs)
}
}
extension Tensor where Scalar : TensorFlowFloatingPoint {
@derivative(of: .*)
static func multiplyDerivative(
_ lhs: Tensor, _ rhs: Tensor
) -> (value: Tensor, pullback: (Tensor) -> (Tensor, Tensor)) {
return (lhs * rhs, { v in
((rhs * v).unbroadcasted(to: lhs.shape),
(lhs * v).unbroadcasted(to: rhs.shape))
})
}
}
// Now, we can take the derivative of a function that calls `.*` that we just defined.
gradient(at: x, y) { x, y in
(x .* y).sum()
}
In [ ]:
let matrix = Tensor<Float>([[1, 2], [3, 4]])
print(_Raw.matMul(matrix, matrix, transposeA: true, transposeB: true))
print(_Raw.matMul(matrix, matrix, transposeA: true, transposeB: false))
print(_Raw.matMul(matrix, matrix, transposeA: false, transposeB: true))
print(_Raw.matMul(matrix, matrix, transposeA: false, transposeB: false))